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
65const 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 ) ;
0 commit comments