Skip to content

Commit 33adcf6

Browse files
committed
feat: handle clean urls e.g. /foo.html -> /foo/
1 parent 419b3c0 commit 33adcf6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/server.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ export function createApp(siteId: string) {
359359
blobKey = manifest.paths[`${urlPath}/index.html`];
360360
}
361361

362+
// If a blob wasn't found, and if the URL ends with ".html", and if a
363+
// corresponding blob exists at a clean URL, redirect automatically.
364+
// For example, this will redirect `/foo/bar.html` to `/foo/bar/` if
365+
// `/foo/bar/index.html` exists in the manifest.
366+
const cleanPath = urlPath.replace(/\.html$/, '');
367+
if (
368+
!blobKey &&
369+
urlPath.endsWith('.html') &&
370+
manifestPaths[`${cleanPath}/index.html`]
371+
) {
372+
res.redirect(301, preserveQueryString(req, `${cleanPath}/`));
373+
return;
374+
}
375+
362376
if (!blobKey) {
363377
// Trailing slash redirect.
364378
if (

0 commit comments

Comments
 (0)