Update HMR logic. Refresh on server props update.
This commit is contained in:
+3
-3
@@ -5,7 +5,7 @@ import AppNames from "../../utils/grab-app-names";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
const { PAGES_DIR } = grabDirNames();
|
||||
export default function grabClientHydrationScript({ page_local_path }) {
|
||||
const { ClientRootElementIDName, ClientRootComponentWindowName } = grabConstants();
|
||||
const { ClientRootElementIDName, ClientRootComponentWindowName, ClientWindowPagePropsName, } = grabConstants();
|
||||
const root_component_path = path.join(PAGES_DIR, `${AppNames["RootPagesComponentName"]}.tsx`);
|
||||
const does_root_exist = existsSync(root_component_path);
|
||||
let txt = ``;
|
||||
@@ -14,7 +14,7 @@ export default function grabClientHydrationScript({ page_local_path }) {
|
||||
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`;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ export default function grabClientHydrationScript({ page_local_path }) {
|
||||
txt += ` } });\n\n`;
|
||||
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,4 +1,5 @@
|
||||
import _ from "lodash";
|
||||
import grabPageComponent from "./web-pages/grab-page-component";
|
||||
export default async function serverPostBuildFn({ artifacts }) {
|
||||
if (!global.IS_FIRST_BUNDLE_READY) {
|
||||
global.IS_FIRST_BUNDLE_READY = true;
|
||||
@@ -9,6 +10,10 @@ export default async function serverPostBuildFn({ artifacts }) {
|
||||
for (let i = 0; i < global.HMR_CONTROLLERS.length; i++) {
|
||||
const controller = global.HMR_CONTROLLERS[i];
|
||||
const target_artifact = artifacts.find((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(controller, ["controller"]),
|
||||
target_map: target_artifact,
|
||||
@@ -16,6 +21,9 @@ export default async function serverPostBuildFn({ artifacts }) {
|
||||
if (!target_artifact) {
|
||||
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,5 +1,7 @@
|
||||
import { AppData } from "../../../data/app-data";
|
||||
import grabConstants from "../../../utils/grab-constants";
|
||||
export default async function (params) {
|
||||
const { ClientWindowPagePropsName } = grabConstants();
|
||||
let script = "";
|
||||
script += `console.log(\`Development Environment\`);\n\n`;
|
||||
script += `const _ce = console.error.bind(console);\n`;
|
||||
@@ -28,6 +30,9 @@ export default async function (params) {
|
||||
script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`;
|
||||
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`;
|
||||
script += ` const newLink = document.createElement("link");\n`;
|
||||
|
||||
Reference in New Issue
Block a user