2019-02-25 23:00:14 +01:00
|
|
|
/* globals __webpack_hash__ */
|
2020-01-09 21:31:28 +01:00
|
|
|
import { displayContent } from './fouc'
|
2021-10-15 09:09:54 +02:00
|
|
|
import initOnDemandEntries from './on-demand-entries-client'
|
|
|
|
import { addMessageListener, connectHMR } from './error-overlay/websocket'
|
2019-02-25 23:00:14 +01:00
|
|
|
|
|
|
|
const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent)
|
2021-11-21 15:42:19 +01:00
|
|
|
window.__NEXT_DATA__ = data
|
|
|
|
|
2019-02-28 18:52:13 +01:00
|
|
|
let { assetPrefix, page } = data
|
|
|
|
assetPrefix = assetPrefix || ''
|
2019-04-13 03:54:27 +02:00
|
|
|
let mostRecentHash = null
|
|
|
|
/* eslint-disable-next-line */
|
|
|
|
let curHash = __webpack_hash__
|
2019-05-29 13:57:26 +02:00
|
|
|
const hotUpdatePath =
|
|
|
|
assetPrefix + (assetPrefix.endsWith('/') ? '' : '/') + '_next/static/webpack/'
|
2019-04-13 03:54:27 +02:00
|
|
|
|
|
|
|
// Is there a newer version of this code available?
|
2019-11-11 04:24:53 +01:00
|
|
|
function isUpdateAvailable() {
|
2019-04-13 03:54:27 +02:00
|
|
|
// __webpack_hash__ is the hash of the current compilation.
|
|
|
|
// It's a global variable injected by Webpack.
|
|
|
|
/* eslint-disable-next-line */
|
|
|
|
return mostRecentHash !== __webpack_hash__
|
|
|
|
}
|
|
|
|
|
|
|
|
// Webpack disallows updates in other states.
|
2019-11-11 04:24:53 +01:00
|
|
|
function canApplyUpdates() {
|
2019-04-13 03:54:27 +02:00
|
|
|
return module.hot.status() === 'idle'
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function reads code updates on the fly and hard
|
|
|
|
// reloads the page when it has changed.
|
2019-11-11 04:24:53 +01:00
|
|
|
async function tryApplyUpdates() {
|
2019-04-13 03:54:27 +02:00
|
|
|
if (!isUpdateAvailable() || !canApplyUpdates()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
2021-06-18 19:12:20 +02:00
|
|
|
const res = await fetch(
|
|
|
|
typeof __webpack_runtime_id__ !== 'undefined'
|
|
|
|
? // eslint-disable-next-line no-undef
|
|
|
|
`${hotUpdatePath}${curHash}.${__webpack_runtime_id__}.hot-update.json`
|
|
|
|
: `${hotUpdatePath}${curHash}.hot-update.json`
|
|
|
|
)
|
2020-06-01 23:00:22 +02:00
|
|
|
const jsonData = await res.json()
|
2019-04-13 03:54:27 +02:00
|
|
|
const curPage = page === '/' ? 'index' : page
|
2020-07-20 17:14:02 +02:00
|
|
|
// webpack 5 uses an array instead
|
2021-08-17 09:18:08 +02:00
|
|
|
const pageUpdated = (
|
|
|
|
Array.isArray(jsonData.c) ? jsonData.c : Object.keys(jsonData.c)
|
2020-07-20 17:14:02 +02:00
|
|
|
).some((mod) => {
|
2019-05-29 13:57:26 +02:00
|
|
|
return (
|
|
|
|
mod.indexOf(
|
2022-03-24 22:49:38 +01:00
|
|
|
`pages${curPage.startsWith('/') ? curPage : `/${curPage}`}`
|
2019-05-29 13:57:26 +02:00
|
|
|
) !== -1 ||
|
|
|
|
mod.indexOf(
|
2022-03-24 22:49:38 +01:00
|
|
|
`pages${curPage.startsWith('/') ? curPage : `/${curPage}`}`.replace(
|
|
|
|
/\//g,
|
|
|
|
'\\'
|
|
|
|
)
|
2019-05-29 13:57:26 +02:00
|
|
|
) !== -1
|
|
|
|
)
|
|
|
|
})
|
2019-04-13 03:54:27 +02:00
|
|
|
|
|
|
|
if (pageUpdated) {
|
|
|
|
document.location.reload(true)
|
|
|
|
} else {
|
|
|
|
curHash = mostRecentHash
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error occurred checking for update', err)
|
|
|
|
document.location.reload(true)
|
|
|
|
}
|
|
|
|
}
|
2019-02-25 23:00:14 +01:00
|
|
|
|
2021-04-13 18:32:36 +02:00
|
|
|
addMessageListener((event) => {
|
2019-02-25 23:00:14 +01:00
|
|
|
if (event.data === '\uD83D\uDC93') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const message = JSON.parse(event.data)
|
|
|
|
|
|
|
|
if (message.action === 'sync' || message.action === 'built') {
|
|
|
|
if (!message.hash) {
|
|
|
|
return
|
|
|
|
}
|
2019-04-13 03:54:27 +02:00
|
|
|
mostRecentHash = message.hash
|
|
|
|
tryApplyUpdates()
|
2019-02-25 23:00:14 +01:00
|
|
|
} else if (message.action === 'reloadPage') {
|
|
|
|
document.location.reload(true)
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
console.warn('Invalid HMR message: ' + event.data + '\n' + ex)
|
|
|
|
}
|
|
|
|
})
|
2019-02-28 18:52:13 +01:00
|
|
|
|
2021-11-04 10:34:37 +01:00
|
|
|
connectHMR({
|
|
|
|
assetPrefix,
|
|
|
|
path: '/_next/webpack-hmr',
|
|
|
|
})
|
2020-01-09 21:31:28 +01:00
|
|
|
displayContent()
|
2021-11-18 23:23:21 +01:00
|
|
|
|
|
|
|
initOnDemandEntries(data.page)
|