Skip to content

Commit efc06ce

Browse files
committed
refactor: Minor adjustments to entry point / client script
1 parent 26cddc5 commit efc06ce

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

packages/cli/global.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ declare module 'shelljs' {
1616
export = shell;
1717
}
1818

19+
declare module 'preact-cli-entrypoint' {
20+
const app: any;
21+
export default app;
22+
}
23+
1924
declare module '*.module.css' {
2025
const classes: { [key: string]: string };
2126
export default classes;

packages/cli/src/lib/entry.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* global __webpack_public_path__ */
22

3-
import * as Preact from 'preact';
4-
const { h, render, hydrate } = Preact;
3+
import { h, render, hydrate } from 'preact';
54

65
const interopDefault = m => (m && m.default ? m.default : m);
76

@@ -27,36 +26,40 @@ if (process.env.NODE_ENV === 'development') {
2726
);
2827
}
2928

30-
let app = interopDefault(require('preact-cli-entrypoint'));
29+
const App = interopDefault(require('preact-cli-entrypoint'));
3130

32-
if (typeof app === 'function') {
33-
let root =
34-
document.getElementById('preact_root') || document.body.firstElementChild;
35-
36-
let init = () => {
37-
let app = interopDefault(require('preact-cli-entrypoint'));
38-
let preRenderData = {};
39-
const inlineDataElement = document.querySelector(
40-
'[type="__PREACT_CLI_DATA__"]'
31+
if (typeof App === 'function') {
32+
const init = () => {
33+
const prerenderDataElement = document.querySelector(
34+
'script[type="__PREACT_CLI_DATA__"]'
4135
);
42-
if (inlineDataElement) {
43-
preRenderData =
44-
JSON.parse(decodeURI(inlineDataElement.innerHTML)).preRenderData ||
45-
preRenderData;
36+
let prerenderData = {};
37+
38+
const root =
39+
(prerenderDataElement && prerenderDataElement.parentElement) ||
40+
document.body;
41+
42+
if (prerenderDataElement) {
43+
prerenderData =
44+
JSON.parse(decodeURI(prerenderDataElement.innerHTML)).prerenderData ||
45+
prerenderData;
4646
}
47+
4748
/* An object named CLI_DATA is passed as a prop,
4849
* this keeps us future proof if in case we decide,
4950
* to send other data like at some point in time.
5051
*/
51-
const CLI_DATA = { preRenderData };
52-
const currentURL = preRenderData.url ? normalizeURL(preRenderData.url) : '';
53-
const canHydrate =
54-
process.env.PRERENDER &&
55-
process.env.NODE_ENV === 'production' &&
56-
hydrate &&
57-
currentURL === normalizeURL(location.pathname);
58-
const doRender = canHydrate ? hydrate : render;
59-
doRender(h(app, { CLI_DATA }), document.body, root);
52+
const CLI_DATA = { prerenderData };
53+
const currentURL = prerenderData.url ? normalizeURL(prerenderData.url) : '';
54+
55+
if (
56+
prerenderDataElement &&
57+
currentURL === normalizeURL(location.pathname)
58+
) {
59+
hydrate(h(App, { CLI_DATA }), root);
60+
} else {
61+
render(h(App, { CLI_DATA }), root);
62+
}
6063
};
6164

6265
if (module.hot) module.hot.accept('preact-cli-entrypoint', init);

packages/cli/src/lib/webpack/render-html-plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ module.exports = async function renderHTMLPlugin(config, env) {
140140
config
141141
),
142142
env,
143-
preRenderData: values,
144-
CLI_DATA: { preRenderData: { url, ...routeData } },
143+
prerenderData: values,
144+
CLI_DATA: { prerenderData: { url, ...routeData } },
145145
ssr: config.prerender ? await prerender(config, values) : '',
146146
entrypoints,
147147
},

0 commit comments

Comments
 (0)