Unverified Commit 8699abf2 authored by Markus Gritsch's avatar Markus Gritsch Committed by GitHub

Safely retrieve router, improve page reload logic (#74209)

This PR improves the hot module reloading (HMR) logic in the dev client
by:

- Safe Router Retrieval: Uses optional chaining (window.next?.router) to
prevent errors if the router is uninitialized.
- Enhanced Error Page Handling: Ensures the client reloads when on /404
or /_error pages, addressing cases where page additions/removals can't
be reliably matched to the current path.
- Improved Reload Logic: Checks if the current page is added or removed
and triggers a reload accordingly to keep the client state up-to-date.
- Updated Comments: Clarifies the conditions under which reloads occur,
especially when on error pages.

These changes make the HMR process more robust and prevent potential
runtime issues.

Closes #71974
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 22970b82
import type {
NextRouter,
PrivateRouteInfo,
} from '../../shared/lib/router/router'
import connect from '../components/react-dev-overlay/pages/hot-reloader-client' import connect from '../components/react-dev-overlay/pages/hot-reloader-client'
import { sendMessage } from '../components/react-dev-overlay/pages/websocket' import { sendMessage } from '../components/react-dev-overlay/pages/websocket'
// Define a local type for the window.next object
interface NextWindow {
next?: {
router?: NextRouter & {
components: { [pathname: string]: PrivateRouteInfo }
}
}
__nextDevClientId?: string
location: Location
}
declare const window: NextWindow
let reloading = false let reloading = false
export default (mode: 'webpack' | 'turbopack') => { export default (mode: 'webpack' | 'turbopack') => {
...@@ -8,11 +25,13 @@ export default (mode: 'webpack' | 'turbopack') => { ...@@ -8,11 +25,13 @@ export default (mode: 'webpack' | 'turbopack') => {
devClient.subscribeToHmrEvent((obj: any) => { devClient.subscribeToHmrEvent((obj: any) => {
if (reloading) return if (reloading) return
// if we're on an error/404 page, we can't reliably tell if the newly added/removed page
// matches the current path. In that case, assume any added/removed entries should trigger a reload of the current page // Retrieve the router if it's available
const router = window.next?.router
// Determine if we're on an error page or the router is not initialized
const isOnErrorPage = const isOnErrorPage =
window.next.router.pathname === '/404' || !router || router.pathname === '/404' || router.pathname === '/_error'
window.next.router.pathname === '/_error'
switch (obj.action) { switch (obj.action) {
case 'reloadPage': { case 'reloadPage': {
...@@ -27,7 +46,13 @@ export default (mode: 'webpack' | 'turbopack') => { ...@@ -27,7 +46,13 @@ export default (mode: 'webpack' | 'turbopack') => {
} }
case 'removedPage': { case 'removedPage': {
const [page] = obj.data const [page] = obj.data
if (page === window.next.router.pathname || isOnErrorPage) {
// Check if the removed page is the current page
const isCurrentPage = page === router?.pathname
// We enter here if the removed page is currently being viewed
// or if we happen to be on an error page.
if (isCurrentPage || isOnErrorPage) {
sendMessage( sendMessage(
JSON.stringify({ JSON.stringify({
event: 'client-removed-page', event: 'client-removed-page',
...@@ -41,11 +66,17 @@ export default (mode: 'webpack' | 'turbopack') => { ...@@ -41,11 +66,17 @@ export default (mode: 'webpack' | 'turbopack') => {
} }
case 'addedPage': { case 'addedPage': {
const [page] = obj.data const [page] = obj.data
if (
(page === window.next.router.pathname && // Check if the added page is the current page
typeof window.next.router.components[page] === 'undefined') || const isCurrentPage = page === router?.pathname
isOnErrorPage
) { // Check if the page component is not yet loaded
const isPageNotLoaded =
typeof router?.components?.[page] === 'undefined'
// We enter this block if the newly added page is the one currently being viewed
// but hasn't been loaded yet, or if we're on an error page.
if ((isCurrentPage && isPageNotLoaded) || isOnErrorPage) {
sendMessage( sendMessage(
JSON.stringify({ JSON.stringify({
event: 'client-added-page', event: 'client-added-page',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment