Skip to content

Commit 6212b5d

Browse files
committed
add hash remap
1 parent ac4df53 commit 6212b5d

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

bun.lockb

-425 Bytes
Binary file not shown.

index.tsx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FileSystemRouter, type BunFile } from "bun";
22
import { NJSON } from "next-json";
33
import { readFileSync, statSync } from "node:fs";
4-
import { join, relative, normalize } from "node:path";
4+
import { join, parse, relative } from "node:path";
55
import { renderToReadableStream } from "react-dom/server";
66
import { ClientOnlyError } from "./client";
77
import { MetaContext, PreloadModule } from "./preload";
@@ -62,17 +62,24 @@ export class StaticRouters {
6262
const parsed = require(metafile);
6363
this.#hashed = parsed.hashed;
6464
this.#dependencies = parsed.dependencies;
65-
this.#routes = new Map(
66-
Object.entries(this.client.routes).map(([path, filePath]) => {
67-
let target = "/" + relative(join(baseDir, buildDir), filePath);
68-
if (this.#hashed[target]) target += `?${this.#hashed[target]}`;
69-
return [path, target];
70-
})
71-
);
65+
this.#routes = new Map<string, string>();
66+
this.#static_cache.reset();
67+
for (const [path, filePath] of Object.entries(this.client.routes)) {
68+
const target = "/" + relative(join(baseDir, buildDir), filePath);
69+
const converted = this.#hashed[target]
70+
? hashremap(target, this.#hashed[target])
71+
: target;
72+
this.#routes.set(path, converted);
73+
}
74+
for (const [file, hash] of Object.entries(this.#hashed)) {
75+
this.#static_cache.remap(
76+
hashremap(file, hash),
77+
join(baseDir, buildDir, file)
78+
);
79+
}
7280
this.#routes_dump = NJSON.stringify(Object.fromEntries(this.#routes), {
7381
omitStack: true,
7482
});
75-
this.#static_cache.reset();
7683
}
7784

7885
async serve<T = void>(
@@ -164,7 +171,7 @@ export class StaticRouters {
164171
.join(";"),
165172
bootstrapModules: bootstrapModules?.map((name) => {
166173
const hash = this.#hashed[name];
167-
if (hash) return `${name}?${hash}`;
174+
if (hash) return hashremap(name, hash);
168175
return name;
169176
}),
170177
onError,
@@ -198,7 +205,7 @@ function* scanCacheDependencies(
198205
: target.endsWith(".ts")
199206
? "ts"
200207
: "jsx",
201-
}).scanImports(readFileSync(target));
208+
}).scanImports(readFileSync(target, "utf-8"));
202209
for (const imp of imports) {
203210
if (imp.kind === "import-statement") {
204211
const path = require.resolve(
@@ -218,12 +225,20 @@ function* scanCacheDependencies(
218225
} catch {}
219226
}
220227

228+
function hashremap(input: string, hash: string) {
229+
const parsed = parse(input);
230+
return `${join(parsed.dir, parsed.name)}-${hash}${parsed.ext}`;
231+
}
232+
221233
export class StaticFileCache {
222234
#cache = new Map<string, BunFile>();
223235
constructor(public base: string) {}
224236
reset() {
225237
this.#cache.clear();
226238
}
239+
remap(pathname: string, real: string) {
240+
this.#cache.set(pathname, Bun.file(real));
241+
}
227242
match(pathname: string): BunFile | undefined {
228243
if (this.#cache.has(pathname)) {
229244
return this.#cache.get(pathname)!;

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
"license": "MIT",
1515
"type": "module",
1616
"devDependencies": {
17-
"@types/bun": "^1.1.6",
18-
"@types/react": "^18.3.3",
19-
"@types/react-dom": "^18.3.0",
20-
"bun-plugin-dts": "^0.2.3",
21-
"bun-types": "latest"
17+
"@types/bun": "^1.1.12",
18+
"@types/react": "^18.3.12",
19+
"@types/react-dom": "^18.3.1",
20+
"bun-plugin-dts": "^0.2.4"
2221
},
2322
"peerDependencies": {
2423
"typescript": "^5.2.2",
2524
"react": "19.0.0-rc-1eaccd82-20240816",
2625
"react-dom": "19.0.0-rc-1eaccd82-20240816"
2726
},
2827
"dependencies": {
29-
"next-json": "^0.2.3"
28+
"next-json": "^0.3.2"
3029
},
3130
"scripts": {
3231
"dist": "bun run scripts/dist.ts"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
// Enable latest features
4-
"lib": ["ESNext", "DOM"],
4+
"lib": ["ESNext", "DOM", "DOM.AsyncIterable"],
55
"target": "ESNext",
66
"module": "ESNext",
77
"moduleDetection": "force",

0 commit comments

Comments
 (0)