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: JJ Kasper <jj@jjsweb.site>