Skip to content

Commit 61d142d

Browse files
committed
Implemented handling for broken links, added path resolution to all paths, not only index.html
1 parent ef7de96 commit 61d142d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/serve-static.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,24 @@ export const serveStatic = <E extends Env = any>(
9292
root,
9393
!optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename
9494
)
95+
96+
try {
97+
path = realpathSync(path)
98+
} catch {
99+
await options.onNotFound?.(path, c)
100+
return next()
101+
}
102+
95103
let stats = getStats(path)
96104

97105
if (stats && stats.isDirectory()) {
98106
const indexFile = options.index ?? 'index.html'
99-
path = realpathSync(join(path, indexFile))
107+
try {
108+
path = realpathSync(join(path, indexFile))
109+
} catch {
110+
await options.onNotFound?.(path, c)
111+
return next()
112+
}
100113
stats = getStats(path)
101114
}
102115

0 commit comments

Comments
 (0)