2022-03-07 18:09:55 +01:00
|
|
|
import { initialize, hydrate, version, router, emitter } from './'
|
2019-06-05 20:15:42 +02:00
|
|
|
import initOnDemandEntries from './dev/on-demand-entries-client'
|
|
|
|
import initWebpackHMR from './dev/webpack-hot-middleware-client'
|
|
|
|
import initializeBuildWatcher from './dev/dev-build-watcher'
|
2020-01-09 21:31:28 +01:00
|
|
|
import { displayContent } from './dev/fouc'
|
2021-10-15 09:09:54 +02:00
|
|
|
import { connectHMR, addMessageListener } from './dev/error-overlay/websocket'
|
2021-07-14 20:12:04 +02:00
|
|
|
import {
|
|
|
|
assign,
|
|
|
|
urlQueryToSearchParams,
|
|
|
|
} from '../shared/lib/router/utils/querystring'
|
2018-01-30 16:40:52 +01:00
|
|
|
|
2021-11-23 14:10:56 +01:00
|
|
|
if (!window._nextSetupHydrationWarning) {
|
|
|
|
const origConsoleError = window.console.error
|
|
|
|
window.console.error = (...args) => {
|
|
|
|
const isHydrateError = args.some(
|
|
|
|
(arg) =>
|
|
|
|
typeof arg === 'string' &&
|
2022-05-20 21:04:27 +02:00
|
|
|
arg.match(/(hydration|content does not match|did not match)/i)
|
2021-11-23 14:10:56 +01:00
|
|
|
)
|
|
|
|
if (isHydrateError) {
|
|
|
|
args = [
|
|
|
|
...args,
|
|
|
|
`\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
origConsoleError.apply(window.console, args)
|
|
|
|
}
|
|
|
|
window._nextSetupHydrationWarning = true
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:12:04 +02:00
|
|
|
window.next = {
|
|
|
|
version,
|
|
|
|
// router is initialized later so it has to be live-binded
|
|
|
|
get router() {
|
|
|
|
return router
|
|
|
|
},
|
|
|
|
emitter,
|
|
|
|
}
|
2020-08-13 14:39:36 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
const webpackHMR = initWebpackHMR()
|
|
|
|
initialize({ webpackHMR })
|
|
|
|
.then(({ assetPrefix }) => {
|
|
|
|
connectHMR({ assetPrefix, path: '/_next/webpack-hmr' })
|
|
|
|
|
|
|
|
return hydrate({ beforeRender: displayContent }).then(() => {
|
|
|
|
initOnDemandEntries()
|
2020-09-03 14:27:08 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
let buildIndicatorHandler = () => {}
|
2020-09-02 18:57:21 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
function devPagesManifestListener(event) {
|
|
|
|
if (event.data.indexOf('devPagesManifest') !== -1) {
|
|
|
|
fetch(
|
|
|
|
`${assetPrefix}/_next/static/development/_devPagesManifest.json`
|
|
|
|
)
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((manifest) => {
|
|
|
|
window.__DEV_PAGES_MANIFEST = manifest
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(`Failed to fetch devPagesManifest`, err)
|
|
|
|
})
|
|
|
|
} else if (event.data.indexOf('middlewareChanges') !== -1) {
|
2021-08-27 14:29:30 +02:00
|
|
|
return window.location.reload()
|
2022-03-07 18:09:55 +01:00
|
|
|
} else if (event.data.indexOf('serverOnlyChanges') !== -1) {
|
|
|
|
const { pages } = JSON.parse(event.data)
|
|
|
|
|
|
|
|
// Make sure to reload when the dev-overlay is showing for an
|
|
|
|
// API route
|
|
|
|
if (pages.includes(router.query.__NEXT_PAGE)) {
|
|
|
|
return window.location.reload()
|
|
|
|
}
|
2021-08-27 14:29:30 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
if (!router.clc && pages.includes(router.pathname)) {
|
|
|
|
console.log('Refreshing page data due to server-side change')
|
2020-09-02 18:57:21 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
buildIndicatorHandler('building')
|
2020-09-03 14:27:08 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
const clearIndicator = () => buildIndicatorHandler('built')
|
2020-09-03 14:27:08 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
router
|
|
|
|
.replace(
|
|
|
|
router.pathname +
|
|
|
|
'?' +
|
|
|
|
String(
|
|
|
|
assign(
|
|
|
|
urlQueryToSearchParams(router.query),
|
|
|
|
new URLSearchParams(location.search)
|
|
|
|
)
|
|
|
|
),
|
2022-03-30 17:45:34 +02:00
|
|
|
router.asPath,
|
|
|
|
{ scroll: false }
|
2022-03-07 18:09:55 +01:00
|
|
|
)
|
2022-06-20 13:31:19 +02:00
|
|
|
.catch(() => {
|
|
|
|
// trigger hard reload when failing to refresh data
|
|
|
|
// to show error overlay properly
|
|
|
|
location.reload()
|
|
|
|
})
|
2022-03-07 18:09:55 +01:00
|
|
|
.finally(clearIndicator)
|
|
|
|
}
|
2020-09-02 18:57:21 +02:00
|
|
|
}
|
2020-08-13 14:39:36 +02:00
|
|
|
}
|
2022-03-07 18:09:55 +01:00
|
|
|
addMessageListener(devPagesManifestListener)
|
2020-08-13 14:39:36 +02:00
|
|
|
|
2022-03-07 18:09:55 +01:00
|
|
|
if (process.env.__NEXT_BUILD_INDICATOR) {
|
|
|
|
initializeBuildWatcher((handler) => {
|
|
|
|
buildIndicatorHandler = handler
|
|
|
|
}, process.env.__NEXT_BUILD_INDICATOR_POSITION)
|
|
|
|
}
|
|
|
|
})
|
2019-05-29 13:57:26 +02:00
|
|
|
})
|
2020-05-18 21:24:37 +02:00
|
|
|
.catch((err) => {
|
2018-07-24 11:24:40 +02:00
|
|
|
console.error('Error was not caught', err)
|
2017-04-18 06:18:43 +02:00
|
|
|
})
|