| import { HMR_MESSAGE_SENT_TO_BROWSER } from '../../server/dev/hot-reloader-types' |
| import type { |
| NextRouter, |
| PrivateRouteInfo, |
| } from '../../shared/lib/router/router' |
| import connect from './hot-reloader/pages/hot-reloader-pages' |
| import { sendMessage } from './hot-reloader/pages/websocket' |
|
|
| |
| interface NextWindow { |
| next?: { |
| router?: NextRouter & { |
| components: { [pathname: string]: PrivateRouteInfo } |
| } |
| } |
| __nextDevClientId?: string |
| location: Location |
| } |
|
|
| declare const window: NextWindow |
|
|
| let reloading = false |
|
|
| export default () => { |
| const devClient = connect() |
|
|
| devClient.subscribeToHmrEvent((message) => { |
| if (reloading) return |
|
|
| |
| const router = window.next?.router |
|
|
| |
| const isOnErrorPage = |
| !router || router.pathname === '/404' || router.pathname === '/_error' |
|
|
| switch (message.type) { |
| case HMR_MESSAGE_SENT_TO_BROWSER.RELOAD_PAGE: { |
| sendMessage( |
| JSON.stringify({ |
| event: 'client-reload-page', |
| clientId: window.__nextDevClientId, |
| }) |
| ) |
| reloading = true |
| return window.location.reload() |
| } |
| case HMR_MESSAGE_SENT_TO_BROWSER.REMOVED_PAGE: { |
| const [page] = message.data |
|
|
| |
| const isCurrentPage = page === router?.pathname |
|
|
| |
| |
| if (isCurrentPage || isOnErrorPage) { |
| sendMessage( |
| JSON.stringify({ |
| event: 'client-removed-page', |
| clientId: window.__nextDevClientId, |
| page, |
| }) |
| ) |
| return window.location.reload() |
| } |
| return |
| } |
| case HMR_MESSAGE_SENT_TO_BROWSER.ADDED_PAGE: { |
| const [page] = message.data |
|
|
| |
| const isCurrentPage = page === router?.pathname |
|
|
| |
| const isPageNotLoaded = |
| page !== null && typeof router?.components?.[page] === 'undefined' |
|
|
| |
| |
| if ((isCurrentPage && isPageNotLoaded) || isOnErrorPage) { |
| sendMessage( |
| JSON.stringify({ |
| event: 'client-added-page', |
| clientId: window.__nextDevClientId, |
| page, |
| }) |
| ) |
| return window.location.reload() |
| } |
| return |
| } |
| case HMR_MESSAGE_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE: { |
| return |
| } |
| default: { |
| message satisfies never |
| } |
| } |
| }) |
|
|
| return devClient |
| } |
|
|