Skip to content

Commit 3a54290

Browse files
Fix static plugin trailing slash behavior for directories and index matching (#2949)
Co-authored-by: Ivan R. Judson <irjudson@gmail.com>
1 parent d7989b1 commit 3a54290

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

server/static.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export function handleApplication(scope: Scope) {
5252
staticFiles.set(entry.urlPath, entry.absolutePath);
5353
// If the file is an index.html, also store it in the index entries
5454
if (entry.urlPath.endsWith('index.html')) {
55-
indexEntries.set(dirname(entry.urlPath), entry.absolutePath);
55+
// Without trailing slash; null -> 301 redirect to trailing slash
56+
indexEntries.set(dirname(entry.urlPath), null);
57+
// With trailing slash; serves the index.html file
58+
indexEntries.set(join(dirname(entry.urlPath), '/'), entry.absolutePath);
5659
}
5760
break;
5861
case 'unlink':
@@ -90,7 +93,18 @@ export function handleApplication(scope: Scope) {
9093
}
9194

9295
if (index) {
96+
// Retrieve index entry
9397
staticFile = indexEntries.get(req.pathname);
98+
99+
// If `null`, redirect to trailing slash
100+
if (staticFile === null) {
101+
return {
102+
status: 301,
103+
headers: {
104+
Location: join(req.pathname, '/'),
105+
},
106+
}
107+
}
94108
}
95109
}
96110

0 commit comments

Comments
 (0)