-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,82 @@ | ||
import { loadableReady } from '@loadable/component' | ||
import ReactDOM from 'react-dom' | ||
import React from 'react' | ||
import { ApplicationRoot } from './components/ApplicationRoot.js' | ||
import application from '@main' | ||
import { EV_AFTER_HYDRATION, EV_BEFORE_HYDRATION } from '../server/events.js' | ||
import { NextStaticData } from '../types/entrypoint.js' | ||
|
||
async function init() { | ||
if (typeof window.__NEXT_STATIC_DATA__ === 'undefined') { | ||
throw new Error( | ||
'[next-static]: Client side rendering expected `__NEXT_STATIC_DATA__` to be defined.' | ||
) | ||
declare global { | ||
interface Window { | ||
/* prod */ | ||
__NEXT_STATIC_CONTEXT_EXTEND__: Pick<NextStaticData, 'context'> | ||
} | ||
const { | ||
locale, | ||
locales, | ||
basePath, | ||
domains, | ||
defaultLocale, | ||
linkPrefix, | ||
context, | ||
query, | ||
} = window.__NEXT_STATIC_DATA__ | ||
window.dispatchEvent(new CustomEvent(EV_BEFORE_HYDRATION)) | ||
|
||
await loadableReady() | ||
const { components, props } = await application(context) | ||
|
||
for (const [index, Component] of components.entries()) { | ||
const selector = `[data-next-static-index="${index}"]` | ||
const root = document.querySelector(selector) | ||
if (!root) { | ||
throw new Error( | ||
`[next-static] Unable to rehydrate static root. Cannot find selector ${selector}.` | ||
) | ||
} | ||
ReactDOM.hydrate( | ||
<ApplicationRoot | ||
locale={locale} | ||
domains={domains} | ||
defaultLocale={defaultLocale} | ||
locales={locales} | ||
basePath={basePath} | ||
linkPrefix={linkPrefix} | ||
query={query} | ||
> | ||
<Component {...props} /> | ||
</ApplicationRoot>, | ||
root | ||
) | ||
} | ||
window.dispatchEvent(new CustomEvent(EV_AFTER_HYDRATION)) | ||
} | ||
|
||
;(async () => { | ||
async function init() { | ||
try { | ||
await init() | ||
const initialData = JSON.parse( | ||
document.getElementById('__NEXT_STATIC_DATA__')!.textContent! | ||
) as NextStaticData | ||
|
||
const thisInitialData = (initialData || {}) as NextStaticData | ||
|
||
const config = { | ||
...thisInitialData, | ||
context: { | ||
...thisInitialData.context, | ||
...(window.__NEXT_STATIC_CONTEXT_EXTEND__ || {}), | ||
}, | ||
} | ||
|
||
const { | ||
locale, | ||
locales, | ||
basePath, | ||
domains, | ||
defaultLocale, | ||
linkPrefix, | ||
context, | ||
query, | ||
} = config | ||
|
||
window.dispatchEvent(new CustomEvent(EV_BEFORE_HYDRATION)) | ||
|
||
const { ApplicationRoot } = await import('./components/ApplicationRoot.js') | ||
const application = await import('@main') | ||
await loadableReady() | ||
const { components, props } = await application.default(context) | ||
|
||
for (const [index, Component] of components.entries()) { | ||
const selector = `[data-next-static-index="${index}"]` | ||
const root = document.querySelector(selector) | ||
if (!root) { | ||
throw new Error( | ||
`[next-static] Unable to rehydrate static root. Cannot find selector ${selector}.` | ||
) | ||
} | ||
ReactDOM.hydrate( | ||
<ApplicationRoot | ||
locale={locale} | ||
domains={domains} | ||
defaultLocale={defaultLocale} | ||
locales={locales} | ||
basePath={basePath} | ||
linkPrefix={linkPrefix} | ||
query={query} | ||
> | ||
<Component {...props} /> | ||
</ApplicationRoot>, | ||
root | ||
) | ||
} | ||
window.dispatchEvent(new CustomEvent(EV_AFTER_HYDRATION)) | ||
} catch (e) { | ||
console.error(`[next-static] Error during application init.`, e) | ||
} | ||
} | ||
|
||
;(() => { | ||
if (document.readyState === 'complete') { | ||
return init() | ||
} | ||
document.addEventListener('DOMContentLoaded', init) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters