Unverified Commit 2404b90e authored by Joseph's avatar Joseph Committed by GitHub

fix: try/catch access to localStorage within __NEXT_APP_ISR_INDICATOR useEffect (#72362)

### What?

Under certain circumstances, the Storage APIs, such as local storage,
are not available.

For example, the Brave browser allows users to block `all cookies`,
which forbids access to local storage.

-
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage#securityerror

### Why?

When access to local storage fails, the development mode page crashes,
leading to the `Application Error` view.

### How?

Wrap on a try/catch the local storage access done within a use-effect,
under `__NEXT_APP_ISR_INDICATOR`.

There is another local storage access point, but that one is done as
part of `processMessage`, which is called within a `try/catch` block
already.


Fixes #72046
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 0bc4086d
...@@ -618,17 +618,32 @@ export default function HotReload({ ...@@ -618,17 +618,32 @@ export default function HotReload({
if (appIsrManifest) { if (appIsrManifest) {
if (pathname && pathname in appIsrManifest) { if (pathname && pathname in appIsrManifest) {
const indicatorHiddenAt = Number( try {
localStorage?.getItem('__NEXT_DISMISS_PRERENDER_INDICATOR') const indicatorHiddenAt = Number(
) localStorage?.getItem('__NEXT_DISMISS_PRERENDER_INDICATOR')
)
const isHidden = const isHidden =
indicatorHiddenAt && indicatorHiddenAt &&
!isNaN(indicatorHiddenAt) && !isNaN(indicatorHiddenAt) &&
Date.now() < indicatorHiddenAt Date.now() < indicatorHiddenAt
if (!isHidden) {
dispatcher.onStaticIndicator(true)
}
} catch (reason) {
let message = ''
if (reason instanceof DOMException) {
// Most likely a SecurityError, because of an unavailable localStorage
message = reason.stack ?? reason.message
} else if (reason instanceof Error) {
message = 'Error: ' + reason.message + '\n' + (reason.stack ?? '')
} else {
message = 'Unexpected Exception: ' + reason
}
if (!isHidden) { console.warn('[HMR] ' + message)
dispatcher.onStaticIndicator(true)
} }
} else { } else {
dispatcher.onStaticIndicator(false) dispatcher.onStaticIndicator(false)
......
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