MCPcopy
hub / github.com/vercel/next.js / onPopState

Function onPopState

packages/next/src/client/components/app-router.tsx:379–399  ·  view source on GitHub ↗
(event: PopStateEvent)

Source from the content-addressed store, hash-verified

377 * That case can happen when the old router injected the history entry.
378 */
379 const onPopState = (event: PopStateEvent) => {
380 if (!event.state) {
381 // TODO-APP: this case only happens when pushState/replaceState was called outside of Next.js. It should probably reload the page in this case.
382 return
383 }
384
385 // This case happens when the history entry was pushed by the `pages` router.
386 if (!event.state.__NA) {
387 window.location.reload()
388 return
389 }
390
391 // TODO-APP: Ideally the back button should not use startTransition as it should apply the updates synchronously
392 // Without startTransition works if the cache is there for this path
393 startTransition(() => {
394 dispatchTraverseAction(
395 window.location.href,
396 event.state.__PRIVATE_NEXTJS_INTERNALS_TREE
397 )
398 })
399 }
400
401 // Register popstate event to call onPopstate.
402 window.addEventListener('popstate', onPopState)

Callers

nothing calls this directly

Calls 3

startTransitionFunction · 0.90
dispatchTraverseActionFunction · 0.90
reloadMethod · 0.65

Tested by

no test coverage detected