Skip to content

Commit

Permalink
fix preload
Browse files Browse the repository at this point in the history
  • Loading branch information
codehz committed Nov 26, 2024
1 parent c7bfb9a commit b04df36
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
9 changes: 7 additions & 2 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Glob, Transpiler, fileURLToPath, pathToFileURL } from "bun";
import { basename, join, relative } from "node:path";
import { basename, join, parse, relative } from "node:path";
import { hashremap } from "./hash";

function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
Expand Down Expand Up @@ -105,8 +106,12 @@ export async function build({
const path = relative(outdir, output.path);
if (output.kind === "entry-point" && output.hash) {
hashed[`/${path}`] = output.hash;
const imports = transpiler.scanImports(await output.text());
dependencies[`/${hashremap(path, output.hash)}`] = imports
.filter((x) => x.kind === "import-statement")
.map((x) => "/" + join(path, "..", x.path));
}
if (output.kind === "entry-point" || output.kind === "chunk") {
if (output.kind === "chunk") {
const imports = transpiler.scanImports(await output.text());
dependencies[`/${path}`] = imports
.filter((x) => x.kind === "import-statement")
Expand Down
Binary file modified bun.lockb
Binary file not shown.
8 changes: 7 additions & 1 deletion example/pages/users/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Clock } from "../../components/Clock";

export default function User(props: any) {
return (
<div>
user {JSON.stringify(props)}
<div onClick={() => history.back()}>back</div>
<Clock time={props.time} />
</div>
);
}

export function getServerSideProps(props: any) {
console.log("some secret");
return {
props,
props: {
...props,
time: new Date(),
},
};
}
13 changes: 7 additions & 6 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,17 @@ export class StaticRouters {
headers: { Location: result.redirect },
});
}
const hashedBootstrapModules = bootstrapModules?.map((name) => {
const hash = this.#hashed[name];
if (hash) return hashremap(name, hash);
return name;
});
const stream = await renderToReadableStream(
<Shell route={serverSide.pathname + search} {...staticProps} {...result}>
<MetaContext.Provider
value={{ hash: this.#hashed, dependencies: this.#dependencies }}
>
{bootstrapModules?.map((name, idx) => (
{hashedBootstrapModules?.map((name, idx) => (
<PreloadModule key={idx} module={name} />
))}
<PreloadModule
Expand All @@ -170,11 +175,7 @@ export class StaticRouters {
]
.filter(Boolean)
.join(";"),
bootstrapModules: bootstrapModules?.map((name) => {
const hash = this.#hashed[name];
if (hash) return hashremap(name, hash);
return name;
}),
bootstrapModules: hashedBootstrapModules,
onError,
}
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"license": "MIT",
"type": "module",
"devDependencies": {
"typescript": "^5.6.3",
"@types/bun": "^1.1.12",
"typescript": "^5.7.2",
"@types/bun": "^1.1.14",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"bun-plugin-dts": "^0.2.4"
Expand Down

0 comments on commit b04df36

Please sign in to comment.