Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/static-website/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ function rewriteFunctionCode(): string {
return `function handler(event) {
const request = event.request;
const uri = request.uri;
const hasExtension = /\.[a-zA-Z0-9]+$/.test(uri);
const hasExtension = /\\.[a-zA-Z0-9]+$/.test(uri);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the fix to escape the backslash is correct, the regex /[a-zA-Z0-9]+/ for the file extension is quite restrictive. It would not match extensions containing characters like underscores or hyphens.

A more robust approach would be to use /\.[^/.]+$/. This regex will match a dot followed by one or more characters that are not a forward slash or a dot, at the end of the string. This makes it more future-proof for various asset types.

For example, it would correctly handle paths like:

  • /assets/image.svg
  • /assets/font-v2.woff2
  • /archive.tar.gz (matching .gz)

And correctly ignore paths like:

  • /users/123
  • /about/
  • /path/ending/with/a.

Note: After applying this change, you will need to update the test snapshots.

Suggested change
const hasExtension = /\\.[a-zA-Z0-9]+$/.test(uri);
const hasExtension = /\.[^/.]+$/.test(uri);

if (!hasExtension) {
request.uri = '/index.html';
}
return request;
}`;
}`;
}

function redirectFunctionCode(domainName: string): string {
Expand All @@ -257,5 +257,5 @@ function redirectFunctionCode(domainName: string): string {
value: 'https://${domainName}',
},
},
};`;
};`;
}
6 changes: 3 additions & 3 deletions test/static-website/__snapshots__/static-website.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading