Skip to content

Commit

Permalink
[DEV-1988] - Added exceptions list to map edge cases where extensions…
Browse files Browse the repository at this point in the history
… false positives may cause 404 (#1211)

* fix: added exceptions list to map edge cases where extensions false positives may cause 404

* chore: changeset added
  • Loading branch information
christian-calabrese authored Oct 29, 2024
1 parent 2aef954 commit bcba266
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-mails-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cloudfront-functions": patch
---

Fixed .bollo ending urls causing 404
14 changes: 10 additions & 4 deletions apps/cloudfront-functions/src/viewer-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ const handler = (
const { request } = event;
const uri = request.uri;

// Check if the uri refers to a gitbook assets
// Check if the uri refers to a gitbook asset
const isGitbookAssets = uri.startsWith('/gitbook/docs');
const isWoff2 = uri.endsWith('.woff2'); // woff2 is a font format
const uriEndsWithSlash = uri.endsWith('/');
const isHomepage = uri === '/';

// List of special cases (add more as needed)
const specialCases: string[] = ['.bollo'];

Check failure on line 23 in apps/cloudfront-functions/src/viewer-request-handler.ts

View workflow job for this annotation

GitHub Actions / Update CHANGELOG

Only readonly arrays allowed

// Function to check if URI ends with any of the special cases
const isSpecialCase = specialCases.some(caseExt => uri.endsWith(caseExt));

Check failure on line 26 in apps/cloudfront-functions/src/viewer-request-handler.ts

View workflow job for this annotation

GitHub Actions / Update CHANGELOG

Replace `caseExt` with `(caseExt)`

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

Check failure on line 33 in apps/cloudfront-functions/src/viewer-request-handler.ts

View workflow job for this annotation

GitHub Actions / Update CHANGELOG

Replace `!isGitbookAssets·&&·!isWoff2·&&·(!/\.[a-zA-Z]+$/.test(uri)·||·isSpecialCase)` with `⏎········!isGitbookAssets·&&⏎········!isWoff2·&&⏎········(!/\.[a-zA-Z]+$/.test(uri)·||·isSpecialCase)⏎······`
request.uri += '.html';
}
}
Expand All @@ -34,4 +40,4 @@ const handler = (
// do nothing
return event.request;
}
};
};

Check failure on line 43 in apps/cloudfront-functions/src/viewer-request-handler.ts

View workflow job for this annotation

GitHub Actions / Update CHANGELOG

Insert `⏎`

0 comments on commit bcba266

Please sign in to comment.