File size: 2,137 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/**
* Before starting the Next.js runtime and requiring any module, we need to make
* sure the following scripts are executed in the correct order:
* - Polyfills
* - next/script with `beforeInteractive` strategy
*/
import { setAttributesFromProps } from './set-attributes-from-props'
const version = process.env.__NEXT_VERSION
window.next = {
version,
appDir: true,
}
function loadScriptsInSequence(
scripts: [src: string, props: { [prop: string]: any }][],
hydrate: () => void
) {
if (!scripts || !scripts.length) {
return hydrate()
}
return scripts
.reduce((promise, [src, props]) => {
return promise.then(() => {
return new Promise<void>((resolve, reject) => {
const el = document.createElement('script')
if (props) {
setAttributesFromProps(el, props)
}
if (src) {
el.src = src
el.onload = () => resolve()
el.onerror = reject
} else if (props) {
el.innerHTML = props.children
setTimeout(resolve)
}
document.head.appendChild(el)
})
})
}, Promise.resolve())
.catch((err: Error) => {
console.error(err)
// Still try to hydrate even if there's an error.
})
.then(() => {
hydrate()
})
}
export function appBootstrap(hydrate: () => void) {
loadScriptsInSequence((self as any).__next_s, () => {
// If the static shell is being debugged, skip hydration if the
// `__nextppronly` query is present. This is only enabled when the
// environment variable `__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING` is
// set to `1`. Otherwise the following is optimized out.
if (process.env.__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING === '1') {
const search = new URLSearchParams(window.location.search)
if (
search.get('__nextppronly') === 'fallback' ||
search.get('__nextppronly') === '1'
) {
console.warn(
`Skipping hydration due to __nextppronly=${search.get('__nextppronly')}`
)
return
}
}
hydrate()
})
}
|