Skip to content

Commit

Permalink
add hash to entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
codehz committed Jul 5, 2024
1 parent 80e03b3 commit dda2918
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
16 changes: 14 additions & 2 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Glob, fileURLToPath, pathToFileURL } from "bun";
import { unlink } from "node:fs/promises";
import { basename, join } from "node:path";
import { basename, join, relative } from "node:path";

function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
Expand Down Expand Up @@ -38,11 +38,12 @@ export async function build({
for await (const path of glob(absPageDir)) {
entrypoints.push(path);
}
const outdir = join(baseDir, buildDir);
const result = await Bun.build({
entrypoints,
sourcemap,
target: "browser",
outdir: join(baseDir, buildDir),
outdir,
splitting: true,
minify,
define: {
Expand Down Expand Up @@ -99,6 +100,17 @@ export async function build({
await unlink(path);
}
}
const hashed: Record<string, string> = {};
for (const output of result.outputs) {
if (output.kind === "entry-point" && output.hash) {
const path = relative(outdir, output.path);
hashed[`/${path}`] = output.hash;
}
}
Bun.write(
join(outdir, ".meta.json"),
JSON.stringify({ version: 1, hashed })
);
}
return result;
}
Binary file modified bun.lockb
Binary file not shown.
6 changes: 5 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ Bun.serve({
const response = await router.serve(request, {
Shell: ExampleShell,
bootstrapModules: ["/hydrate.js"],
noStreaming: true
noStreaming: true,
staticHeaders: {
"x-powered-by": "bun",
"cache-control": "max-age=14400, immutable",
},
});
if (response) return response;
return new Response("Not found", {
Expand Down
22 changes: 16 additions & 6 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class StaticRouters {
readonly server: FileSystemRouter;
readonly client: FileSystemRouter;
readonly #routes_dump: string;
readonly #hashed: Record<string, string>;

constructor(
public baseDir: string,
Expand All @@ -23,12 +24,14 @@ export class StaticRouters {
dir: join(baseDir, buildDir, pageDir),
style: "nextjs",
});
this.#hashed = require(join(baseDir, buildDir, ".meta.json")).hashed;
this.#routes_dump = NJSON.stringify(
Object.fromEntries(
Object.entries(this.client.routes).map(([path, filePath]) => [
path,
"/" + relative(join(baseDir, buildDir), filePath),
])
Object.entries(this.client.routes).map(([path, filePath]) => {
let target = "/" + relative(join(baseDir, buildDir), filePath);
if (this.#hashed[target]) target += `?${this.#hashed[target]}`;
return [path, target];
})
),
{ omitStack: true }
);
Expand All @@ -46,21 +49,24 @@ export class StaticRouters {
console.error(error, errorInfo);
},
noStreaming,
staticHeaders,
}: {
Shell: React.ComponentType<{ children: React.ReactElement }>;
preloadScript?: string;
bootstrapModules?: string[];
context?: T;
onError?(error: unknown, errorInfo: React.ErrorInfo): string | void;
noStreaming?: boolean;
staticHeaders?: HeadersInit;
}
): Promise<Response | null> {
const { pathname, search } = new URL(request.url);
const staticResponse = await serveFromDir({
directory: this.buildDir,
path: pathname,
});
if (staticResponse) return new Response(staticResponse);
if (staticResponse)
return new Response(staticResponse, { headers: staticHeaders });
const serverSide = this.server.match(request);
if (!serverSide) return null;
const clientSide = this.client.match(request);
Expand Down Expand Up @@ -106,7 +112,11 @@ export class StaticRouters {
]
.filter(Boolean)
.join(";"),
bootstrapModules,
bootstrapModules: bootstrapModules?.map((name) => {
const hash = this.#hashed[name];
if (hash) return `${name}?${hash}`;
return name;
}),
onError,
}
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"peerDependencies": {
"typescript": "^5.0.0",
"react": "*",
"react-dom": "*"
"react": "^19.0.0-rc-cc1ec60d0d-20240607",
"react-dom": "^19.0.0-rc-cc1ec60d0d-20240607"
},
"dependencies": {
"next-json": "^0.2.3"
Expand Down

0 comments on commit dda2918

Please sign in to comment.