2020-01-09 21:31:28 +01:00
|
|
|
/* globals __REPLACE_NOOP_IMPORT__ */
|
2017-10-14 19:20:49 +02:00
|
|
|
import initNext, * as next from './'
|
2019-06-05 20:15:42 +02:00
|
|
|
import EventSourcePolyfill from './dev/event-source-polyfill'
|
|
|
|
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-04-13 18:32:36 +02:00
|
|
|
import { addMessageListener } from './dev/error-overlay/eventsource'
|
2020-09-02 18:57:21 +02:00
|
|
|
import * as querystring from '../next-server/lib/router/utils/querystring'
|
2018-01-30 16:40:52 +01:00
|
|
|
|
2018-07-26 12:38:45 +02:00
|
|
|
// Temporary workaround for the issue described here:
|
2020-05-27 23:51:11 +02:00
|
|
|
// https://github.com/vercel/next.js/issues/3775#issuecomment-407438123
|
2018-07-26 12:38:45 +02:00
|
|
|
// The runtimeChunk doesn't have dynamic import handling code when there hasn't been a dynamic import
|
|
|
|
// The runtimeChunk can't hot reload itself currently to correct it when adding pages using on-demand-entries
|
2020-01-09 21:31:28 +01:00
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
__REPLACE_NOOP_IMPORT__
|
2018-07-26 12:38:45 +02:00
|
|
|
|
2019-02-22 16:23:58 +01:00
|
|
|
// Support EventSource on Internet Explorer 11
|
|
|
|
if (!window.EventSource) {
|
|
|
|
window.EventSource = EventSourcePolyfill
|
|
|
|
}
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
const {
|
2019-11-11 04:24:53 +01:00
|
|
|
__NEXT_DATA__: { assetPrefix },
|
2018-07-24 11:24:40 +02:00
|
|
|
} = window
|
|
|
|
|
|
|
|
const prefix = assetPrefix || ''
|
2021-04-13 18:32:36 +02:00
|
|
|
const webpackHMR = initWebpackHMR()
|
2017-01-12 02:58:20 +01:00
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
window.next = next
|
|
|
|
initNext({ webpackHMR })
|
2020-06-03 17:58:58 +02:00
|
|
|
.then(({ renderCtx, render }) => {
|
2019-02-19 22:45:07 +01:00
|
|
|
initOnDemandEntries({ assetPrefix: prefix })
|
2020-08-13 14:39:36 +02:00
|
|
|
|
2020-09-03 14:27:08 +02:00
|
|
|
let buildIndicatorHandler = () => {}
|
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
function devPagesManifestListener(event) {
|
|
|
|
if (event.data.indexOf('devPagesManifest') !== -1) {
|
|
|
|
fetch(`${prefix}/_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)
|
|
|
|
})
|
2020-09-02 18:57:21 +02:00
|
|
|
} else if (event.data.indexOf('serverOnlyChanges') !== -1) {
|
|
|
|
const { pages } = JSON.parse(event.data)
|
|
|
|
const router = window.next.router
|
|
|
|
|
2021-05-17 14:04:06 +02:00
|
|
|
if (!router.clc && pages.includes(router.pathname)) {
|
2020-09-02 18:57:21 +02:00
|
|
|
console.log('Refreshing page data due to server-side change')
|
|
|
|
|
2020-09-03 14:27:08 +02:00
|
|
|
buildIndicatorHandler('building')
|
|
|
|
|
|
|
|
const clearIndicator = () => buildIndicatorHandler('built')
|
|
|
|
|
|
|
|
router
|
|
|
|
.replace(
|
|
|
|
router.pathname +
|
|
|
|
'?' +
|
|
|
|
String(
|
|
|
|
querystring.assign(
|
|
|
|
querystring.urlQueryToSearchParams(router.query),
|
|
|
|
new URLSearchParams(location.search)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
router.asPath
|
|
|
|
)
|
|
|
|
.finally(clearIndicator)
|
2020-09-02 18:57:21 +02:00
|
|
|
}
|
2020-08-13 14:39:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
devPagesManifestListener.unfiltered = true
|
2021-04-13 18:32:36 +02:00
|
|
|
addMessageListener(devPagesManifestListener)
|
2020-08-13 14:39:36 +02:00
|
|
|
|
2020-09-03 14:27:08 +02:00
|
|
|
if (process.env.__NEXT_BUILD_INDICATOR) {
|
|
|
|
initializeBuildWatcher((handler) => {
|
|
|
|
buildIndicatorHandler = handler
|
|
|
|
})
|
|
|
|
}
|
2017-04-18 06:18:43 +02:00
|
|
|
|
2020-01-20 21:41:41 +01:00
|
|
|
// delay rendering until after styles have been applied in development
|
|
|
|
displayContent(() => {
|
|
|
|
render(renderCtx)
|
|
|
|
})
|
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
|
|
|
})
|