Skip to content

Commit dda2918

Browse files
committed
add hash to entrypoint
1 parent 80e03b3 commit dda2918

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

build.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Glob, fileURLToPath, pathToFileURL } from "bun";
22
import { unlink } from "node:fs/promises";
3-
import { basename, join } from "node:path";
3+
import { basename, join, relative } from "node:path";
44

55
function escapeRegExp(string: string) {
66
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
@@ -38,11 +38,12 @@ export async function build({
3838
for await (const path of glob(absPageDir)) {
3939
entrypoints.push(path);
4040
}
41+
const outdir = join(baseDir, buildDir);
4142
const result = await Bun.build({
4243
entrypoints,
4344
sourcemap,
4445
target: "browser",
45-
outdir: join(baseDir, buildDir),
46+
outdir,
4647
splitting: true,
4748
minify,
4849
define: {
@@ -99,6 +100,17 @@ export async function build({
99100
await unlink(path);
100101
}
101102
}
103+
const hashed: Record<string, string> = {};
104+
for (const output of result.outputs) {
105+
if (output.kind === "entry-point" && output.hash) {
106+
const path = relative(outdir, output.path);
107+
hashed[`/${path}`] = output.hash;
108+
}
109+
}
110+
Bun.write(
111+
join(outdir, ".meta.json"),
112+
JSON.stringify({ version: 1, hashed })
113+
);
102114
}
103115
return result;
104116
}

bun.lockb

-642 Bytes
Binary file not shown.

example/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ Bun.serve({
88
const response = await router.serve(request, {
99
Shell: ExampleShell,
1010
bootstrapModules: ["/hydrate.js"],
11-
noStreaming: true
11+
noStreaming: true,
12+
staticHeaders: {
13+
"x-powered-by": "bun",
14+
"cache-control": "max-age=14400, immutable",
15+
},
1216
});
1317
if (response) return response;
1418
return new Response("Not found", {

index.tsx

+16-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class StaticRouters {
99
readonly server: FileSystemRouter;
1010
readonly client: FileSystemRouter;
1111
readonly #routes_dump: string;
12+
readonly #hashed: Record<string, string>;
1213

1314
constructor(
1415
public baseDir: string,
@@ -23,12 +24,14 @@ export class StaticRouters {
2324
dir: join(baseDir, buildDir, pageDir),
2425
style: "nextjs",
2526
});
27+
this.#hashed = require(join(baseDir, buildDir, ".meta.json")).hashed;
2628
this.#routes_dump = NJSON.stringify(
2729
Object.fromEntries(
28-
Object.entries(this.client.routes).map(([path, filePath]) => [
29-
path,
30-
"/" + relative(join(baseDir, buildDir), filePath),
31-
])
30+
Object.entries(this.client.routes).map(([path, filePath]) => {
31+
let target = "/" + relative(join(baseDir, buildDir), filePath);
32+
if (this.#hashed[target]) target += `?${this.#hashed[target]}`;
33+
return [path, target];
34+
})
3235
),
3336
{ omitStack: true }
3437
);
@@ -46,21 +49,24 @@ export class StaticRouters {
4649
console.error(error, errorInfo);
4750
},
4851
noStreaming,
52+
staticHeaders,
4953
}: {
5054
Shell: React.ComponentType<{ children: React.ReactElement }>;
5155
preloadScript?: string;
5256
bootstrapModules?: string[];
5357
context?: T;
5458
onError?(error: unknown, errorInfo: React.ErrorInfo): string | void;
5559
noStreaming?: boolean;
60+
staticHeaders?: HeadersInit;
5661
}
5762
): Promise<Response | null> {
5863
const { pathname, search } = new URL(request.url);
5964
const staticResponse = await serveFromDir({
6065
directory: this.buildDir,
6166
path: pathname,
6267
});
63-
if (staticResponse) return new Response(staticResponse);
68+
if (staticResponse)
69+
return new Response(staticResponse, { headers: staticHeaders });
6470
const serverSide = this.server.match(request);
6571
if (!serverSide) return null;
6672
const clientSide = this.client.match(request);
@@ -106,7 +112,11 @@ export class StaticRouters {
106112
]
107113
.filter(Boolean)
108114
.join(";"),
109-
bootstrapModules,
115+
bootstrapModules: bootstrapModules?.map((name) => {
116+
const hash = this.#hashed[name];
117+
if (hash) return `${name}?${hash}`;
118+
return name;
119+
}),
110120
onError,
111121
}
112122
);

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
},
2323
"peerDependencies": {
2424
"typescript": "^5.0.0",
25-
"react": "*",
26-
"react-dom": "*"
25+
"react": "^19.0.0-rc-cc1ec60d0d-20240607",
26+
"react-dom": "^19.0.0-rc-cc1ec60d0d-20240607"
2727
},
2828
"dependencies": {
2929
"next-json": "^0.2.3"

0 commit comments

Comments
 (0)