Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B2BP-1011] Normalize URI in cloudfront-functions #531

Merged
merged 11 commits into from
Jan 13, 2025
18 changes: 10 additions & 8 deletions apps/cloudfront-functions/src/viewer-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ const handler = (
if (event.context.eventType === 'viewer-request') {
// do the rewrite
const { request } = event;
const uri = request.uri;
const uriEndsWithSlash = uri.endsWith('/');

if (uriEndsWithSlash) {
request.uri = uri.replace(/\/$/, '');
}
const uriEndsWithSlash = request.uri.endsWith('/');
const isHomepage = request.uri === '/';

// Add the .html extension if missing
if (!/\.[0-9a-zA-Z]+$/.test(uri)) {
request.uri += '.html';
if (!isHomepage) {
if (uriEndsWithSlash) {
request.uri = request.uri.replace(/\/$/, '');
}
// Always add .html if there's no file extension, including special cases
if (!/\.[0-9a-zA-Z]+$/.test(request.uri)) {
request.uri += '.html';
}
}

return request;
Expand Down
Loading