|
|
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((obj: any) => { |
|
|
if (reloading) return |
|
|
|
|
|
|
|
|
const router = window.next?.router |
|
|
|
|
|
|
|
|
const isOnErrorPage = |
|
|
!router || router.pathname === '/404' || router.pathname === '/_error' |
|
|
|
|
|
switch (obj.action) { |
|
|
case 'reloadPage': { |
|
|
sendMessage( |
|
|
JSON.stringify({ |
|
|
event: 'client-reload-page', |
|
|
clientId: window.__nextDevClientId, |
|
|
}) |
|
|
) |
|
|
reloading = true |
|
|
return window.location.reload() |
|
|
} |
|
|
case 'removedPage': { |
|
|
const [page] = obj.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 'addedPage': { |
|
|
const [page] = obj.data |
|
|
|
|
|
|
|
|
const isCurrentPage = page === router?.pathname |
|
|
|
|
|
|
|
|
const isPageNotLoaded = |
|
|
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 'serverError': |
|
|
case 'devPagesManifestUpdate': |
|
|
case 'isrManifest': |
|
|
case 'building': |
|
|
case 'finishBuilding': { |
|
|
return |
|
|
} |
|
|
default: { |
|
|
throw new Error('Unexpected action ' + obj.action) |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
return devClient |
|
|
} |
|
|
|