/
next-dev.js
107 lines (93 loc) · 3.31 KB
/
next-dev.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* globals __REPLACE_NOOP_IMPORT__ */
import { initNext, version, router, emitter, render, renderError } from './'
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'
import { displayContent } from './dev/fouc'
import { addMessageListener } from './dev/error-overlay/eventsource'
import {
assign,
urlQueryToSearchParams,
} from '../shared/lib/router/utils/querystring'
// Temporary workaround for the issue described here:
// https://github.com/vercel/next.js/issues/3775#issuecomment-407438123
// 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
// eslint-disable-next-line no-unused-expressions
__REPLACE_NOOP_IMPORT__
// Support EventSource on Internet Explorer 11
if (!window.EventSource) {
window.EventSource = EventSourcePolyfill
}
const {
__NEXT_DATA__: { assetPrefix },
} = window
const prefix = assetPrefix || ''
const webpackHMR = initWebpackHMR()
window.next = {
version,
// router is initialized later so it has to be live-binded
get router() {
return router
},
emitter,
render,
renderError,
}
initNext({ webpackHMR })
.then(({ renderCtx }) => {
initOnDemandEntries({ assetPrefix: prefix })
let buildIndicatorHandler = () => {}
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)
})
} 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()
}
if (!router.clc && pages.includes(router.pathname)) {
console.log('Refreshing page data due to server-side change')
buildIndicatorHandler('building')
const clearIndicator = () => buildIndicatorHandler('built')
router
.replace(
router.pathname +
'?' +
String(
assign(
urlQueryToSearchParams(router.query),
new URLSearchParams(location.search)
)
),
router.asPath
)
.finally(clearIndicator)
}
}
}
devPagesManifestListener.unfiltered = true
addMessageListener(devPagesManifestListener)
if (process.env.__NEXT_BUILD_INDICATOR) {
initializeBuildWatcher((handler) => {
buildIndicatorHandler = handler
})
}
// delay rendering until after styles have been applied in development
displayContent(() => {
render(renderCtx)
})
})
.catch((err) => {
console.error('Error was not caught', err)
})