Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
use same semantic as fastly to deliver static files #51
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan authored Sep 9, 2018
1 parent bb8a763 commit 17dce3c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,23 @@ const utils = {
* @return {Promise} A promise that resolves to the request context.
*/
async fetchStatic(ctx) {
let uri;
if (ctx.path.startsWith('/dist/')) {
uri = path.resolve(ctx.config.distDir, ctx.path.substring(6));
} else {
uri = ctx.config.contentRepo.raw + ctx.path;
}
ctx.logger.debug(`fetching static resource from ${uri}`);
const data = await utils.fetch(uri);
if (data === null) {
const error = new Error('Resource not found.');
error.code = 404;
throw error;
const uris = [
ctx.config.contentRepo.raw + ctx.path,
path.resolve(ctx.config.distDir, ctx.path.substring(1)),
];
for (let i = 0; i < uris.length; i += 1) {
const uri = uris[i];
ctx.logger.debug(`fetching static resource from ${uri}`);
// eslint-disable-next-line no-await-in-loop
const data = await utils.fetch(uri);
if (data != null) {
ctx.content = Buffer.from(data, 'utf8');
return ctx;
}
}
ctx.content = Buffer.from(data, 'utf8');
return ctx;
const error = new Error('Resource not found.');
error.code = 404;
throw error;
},

};
Expand Down
2 changes: 1 addition & 1 deletion test/hlx_server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('Helix Server', () => {
await project.init();
try {
await project.start();
await assertHttp(`http://localhost:${project.server.port}/dist/styles.css`, 200, 'expected_styles.css');
await assertHttp(`http://localhost:${project.server.port}/styles.css`, 200, 'expected_styles.css');
} finally {
await project.stop();
}
Expand Down

0 comments on commit 17dce3c

Please sign in to comment.