Skip to content

Commit b04df36

Browse files
committed
fix preload
1 parent c7bfb9a commit b04df36

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

build.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Glob, Transpiler, fileURLToPath, pathToFileURL } from "bun";
2-
import { basename, join, relative } from "node:path";
2+
import { basename, join, parse, relative } from "node:path";
3+
import { hashremap } from "./hash";
34

45
function escapeRegExp(string: string) {
56
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
@@ -105,8 +106,12 @@ export async function build({
105106
const path = relative(outdir, output.path);
106107
if (output.kind === "entry-point" && output.hash) {
107108
hashed[`/${path}`] = output.hash;
109+
const imports = transpiler.scanImports(await output.text());
110+
dependencies[`/${hashremap(path, output.hash)}`] = imports
111+
.filter((x) => x.kind === "import-statement")
112+
.map((x) => "/" + join(path, "..", x.path));
108113
}
109-
if (output.kind === "entry-point" || output.kind === "chunk") {
114+
if (output.kind === "chunk") {
110115
const imports = transpiler.scanImports(await output.text());
111116
dependencies[`/${path}`] = imports
112117
.filter((x) => x.kind === "import-statement")

bun.lockb

396 Bytes
Binary file not shown.

example/pages/users/[id].tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
import { Clock } from "../../components/Clock";
2+
13
export default function User(props: any) {
24
return (
35
<div>
46
user {JSON.stringify(props)}
57
<div onClick={() => history.back()}>back</div>
8+
<Clock time={props.time} />
69
</div>
710
);
811
}
912

1013
export function getServerSideProps(props: any) {
1114
console.log("some secret");
1215
return {
13-
props,
16+
props: {
17+
...props,
18+
time: new Date(),
19+
},
1420
};
1521
}

index.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,17 @@ export class StaticRouters {
144144
headers: { Location: result.redirect },
145145
});
146146
}
147+
const hashedBootstrapModules = bootstrapModules?.map((name) => {
148+
const hash = this.#hashed[name];
149+
if (hash) return hashremap(name, hash);
150+
return name;
151+
});
147152
const stream = await renderToReadableStream(
148153
<Shell route={serverSide.pathname + search} {...staticProps} {...result}>
149154
<MetaContext.Provider
150155
value={{ hash: this.#hashed, dependencies: this.#dependencies }}
151156
>
152-
{bootstrapModules?.map((name, idx) => (
157+
{hashedBootstrapModules?.map((name, idx) => (
153158
<PreloadModule key={idx} module={name} />
154159
))}
155160
<PreloadModule
@@ -170,11 +175,7 @@ export class StaticRouters {
170175
]
171176
.filter(Boolean)
172177
.join(";"),
173-
bootstrapModules: bootstrapModules?.map((name) => {
174-
const hash = this.#hashed[name];
175-
if (hash) return hashremap(name, hash);
176-
return name;
177-
}),
178+
bootstrapModules: hashedBootstrapModules,
178179
onError,
179180
}
180181
);

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"license": "MIT",
1515
"type": "module",
1616
"devDependencies": {
17-
"typescript": "^5.6.3",
18-
"@types/bun": "^1.1.12",
17+
"typescript": "^5.7.2",
18+
"@types/bun": "^1.1.14",
1919
"@types/react": "^18.3.12",
2020
"@types/react-dom": "^18.3.1",
2121
"bun-plugin-dts": "^0.2.4"

0 commit comments

Comments
 (0)