Update HMR logic. Refresh on server props update.

This commit is contained in:
2026-03-21 18:03:36 +01:00
parent a504046a36
commit 32056b5cb6
9 changed files with 58 additions and 8 deletions
@@ -11,8 +11,11 @@ type Params = {
};
export default function grabClientHydrationScript({ page_local_path }: Params) {
const { ClientRootElementIDName, ClientRootComponentWindowName } =
grabConstants();
const {
ClientRootElementIDName,
ClientRootComponentWindowName,
ClientWindowPagePropsName,
} = grabConstants();
const root_component_path = path.join(
PAGES_DIR,
@@ -28,7 +31,7 @@ export default function grabClientHydrationScript({ page_local_path }: Params) {
txt += `import Root from "${root_component_path}";\n`;
}
txt += `import Page from "${page_local_path}";\n\n`;
txt += `const pageProps = window.__PAGE_PROPS__ || {};\n`;
txt += `const pageProps = window.${ClientWindowPagePropsName} || {};\n`;
if (does_root_exist) {
txt += `const component = <Root suppressHydrationWarning={true} {...pageProps}><Page {...pageProps} /></Root>\n`;
@@ -45,7 +48,7 @@ export default function grabClientHydrationScript({ page_local_path }: Params) {
txt += ` window.${ClientRootComponentWindowName} = root;\n`;
txt += ` window.__BUNEXT_RERENDER__ = (NewPage) => {\n`;
txt += ` const props = window.__PAGE_PROPS__ || {};\n`;
txt += ` const props = window.${ClientWindowPagePropsName} || {};\n`;
txt += ` root.render(<NewPage {...props} />);\n`;
txt += ` };\n`;
txt += `}\n`;
@@ -1,5 +1,6 @@
import _ from "lodash";
import type { BundlerCTXMap, GlobalHMRControllerObject } from "../../types";
import grabPageComponent from "./web-pages/grab-page-component";
type Params = {
artifacts: BundlerCTXMap[];
@@ -21,6 +22,12 @@ export default async function serverPostBuildFn({ artifacts }: Params) {
(a) => controller.target_map?.local_path == a.local_path,
);
const mock_req = new Request(controller.page_url);
const { serverRes } = await grabPageComponent({
req: mock_req,
});
const final_artifact: Omit<GlobalHMRControllerObject, "controller"> = {
..._.omit(controller, ["controller"]),
target_map: target_artifact,
@@ -30,6 +37,10 @@ export default async function serverPostBuildFn({ artifacts }: Params) {
delete final_artifact.target_map;
}
if (serverRes) {
final_artifact.page_props = serverRes;
}
try {
controller.controller.enqueue(
`event: update\ndata: ${JSON.stringify(final_artifact)}\n\n`,
@@ -1,11 +1,14 @@
import type { BundlerCTXMap } from "../../../types";
import { AppData } from "../../../data/app-data";
import grabConstants from "../../../utils/grab-constants";
type Params = {
bundledMap?: BundlerCTXMap;
};
export default async function (params?: Params) {
const { ClientWindowPagePropsName } = grabConstants();
let script = "";
script += `console.log(\`Development Environment\`);\n\n`;
@@ -38,6 +41,10 @@ export default async function (params?: Params) {
script += ` const data = JSON.parse(event.data);\n`;
// script += ` console.log("data", data);\n`;
script += ` if (data.page_props) {\n`;
script += ` window.${ClientWindowPagePropsName} = data.page_props\n`;
script += ` }\n`;
script += ` const oldCSSLink = document.querySelector('link[rel="stylesheet"]');\n`;
script += ` if (data.target_map.css_path) {\n`;
+6
View File
@@ -229,6 +229,11 @@ export type BunextPageModuleServerReturn<
url?: BunextPageModuleServerReturnURLObject;
};
export type BunextPageProps<
T extends { [k: string]: any } = { [k: string]: any },
Q extends { [k: string]: any } = { [k: string]: any },
> = BunextPageModuleServerReturn<T, Q>;
export type BunextPageModuleServerReturnURLObject = URL & {};
export type BunextPageModuleServerRedirect = {
@@ -280,6 +285,7 @@ export type GlobalHMRControllerObject = {
controller: ReadableStreamDefaultController<string>;
page_url: string;
target_map?: BundlerCTXMap;
page_props?: any;
};
export type BunextCacheFileMeta = {