Skip to content

Commit c70dd25

Browse files
committed
Add fallback for /path to /path/index.html
1 parent 57aa158 commit c70dd25

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

lib/fastify-static.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
11
import { FastifyInstance } from "fastify"
22
import fastifyStatic from "fastify-static"
3+
import globby from "globby"
34
import path from "path"
45

5-
import { Config } from "./types"
6+
import { Config, StaticFallbacks } from "./types"
7+
8+
//
9+
// Unfortunately I couldn't make fastify-static add a fallback to /path/index.html when /path is requested.
10+
// Therefore this collects those fallbacks once on startup.
11+
//
12+
function collectFallbacks(staticDir: string) {
13+
const files = globby.sync("", { cwd: staticDir, onlyFiles: true })
14+
15+
const fallbacks = files.reduce((result, filename) => {
16+
if (!filename.includes("/")) {
17+
return result
18+
}
19+
if (!filename.endsWith(".html") && !filename.endsWith(".htm")) {
20+
return result
21+
}
22+
23+
const directory = path.dirname(filename)
24+
const url = "/" + directory
25+
26+
if (result[url]) {
27+
return result
28+
}
29+
30+
if (!result[directory]) {
31+
const fallbackDir = path.resolve(staticDir, directory)
32+
result["/" + directory] = fallbackDir
33+
}
34+
35+
return result
36+
}, {} as StaticFallbacks)
37+
38+
return fallbacks
39+
}
640

741
//
842
// https://github.com/fastify/fastify-static#fastify-static
943
//
1044
export function registerServeStatic(server: FastifyInstance, config: Config) {
45+
const fallbacks = collectFallbacks(config.staticDir)
46+
1147
server.setNotFoundHandler(async (req, res) => {
12-
res.sendFile("index.html", config.staticDir)
48+
const fallbackDir = fallbacks[req.url]
49+
if (fallbackDir) {
50+
res.sendFile("index.html", fallbackDir)
51+
} else {
52+
res.sendFile("index.html", config.staticDir)
53+
}
1354
})
55+
1456
server.register(fastifyStatic, {
1557
root: path.resolve(config.staticDir),
1658
extensions: ["html", "htm"],

lib/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ export interface GitHubUser {
9494
two_factor_authentication: boolean
9595
plan: GitHubPlan
9696
}
97+
98+
export interface StaticFallbacks {
99+
[url: string]: string
100+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"fastify": "^3.9.2",
3030
"fastify-cookie": "^5.0.2",
3131
"fastify-static": "^3.3.0",
32+
"globby": "^11.0.1",
3233
"nanoid": "^3.1.20",
3334
"ow": "^0.21.0"
3435
},

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,18 @@ [email protected]:
10381038
merge2 "^1.3.0"
10391039
slash "^3.0.0"
10401040

1041+
globby@^11.0.1:
1042+
version "11.0.1"
1043+
resolved "https://registry.npmjs.org/globby/-/globby-11.0.1.tgz#9a2bf107a068f3ffeabc49ad702c79ede8cfd357"
1044+
integrity sha512-iH9RmgwCmUJHi2z5o2l3eTtGBtXek1OYlHrbcxOYugyHLmAsZrPj43OtHThd62Buh/Vv6VyCBD2bdyWcGNQqoQ==
1045+
dependencies:
1046+
array-union "^2.1.0"
1047+
dir-glob "^3.0.1"
1048+
fast-glob "^3.1.1"
1049+
ignore "^5.1.4"
1050+
merge2 "^1.3.0"
1051+
slash "^3.0.0"
1052+
10411053
10421054
version "11.8.0"
10431055
resolved "https://registry.yarnpkg.com/got/-/got-11.8.0.tgz#be0920c3586b07fd94add3b5b27cb28f49e6545f"

0 commit comments

Comments
 (0)