Unverified Commit 799f784c authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Skip writeToBfCache without window (#90771)

## What?

Skip bfcache handling server-side.
parent 75988e43
...@@ -25,6 +25,9 @@ const bfcacheMap: CacheMap<BFCacheEntry> = createCacheMap() ...@@ -25,6 +25,9 @@ const bfcacheMap: CacheMap<BFCacheEntry> = createCacheMap()
let currentBfCacheVersion = 0 let currentBfCacheVersion = 0
export function invalidateBfCache(): void { export function invalidateBfCache(): void {
if (typeof window === 'undefined') {
return
}
currentBfCacheVersion++ currentBfCacheVersion++
} }
...@@ -36,6 +39,10 @@ export function writeToBFCache( ...@@ -36,6 +39,10 @@ export function writeToBFCache(
head: React.ReactNode, head: React.ReactNode,
prefetchHead: React.ReactNode prefetchHead: React.ReactNode
): void { ): void {
if (typeof window === 'undefined') {
return
}
const entry: BFCacheEntry = { const entry: BFCacheEntry = {
rsc, rsc,
prefetchRsc, prefetchRsc,
...@@ -75,6 +82,9 @@ export function writeHeadToBFCache( ...@@ -75,6 +82,9 @@ export function writeHeadToBFCache(
export function readFromBFCache( export function readFromBFCache(
varyPath: SegmentVaryPath varyPath: SegmentVaryPath
): BFCacheEntry | null { ): BFCacheEntry | null {
if (typeof window === 'undefined') {
return null
}
const isRevalidation = false const isRevalidation = false
return getFromCacheMap( return getFromCacheMap(
// During a back/forward navigation, it doesn't matter how stale the data // During a back/forward navigation, it doesn't matter how stale the data
...@@ -92,6 +102,9 @@ export function readFromBFCacheDuringRegularNavigation( ...@@ -92,6 +102,9 @@ export function readFromBFCacheDuringRegularNavigation(
now: number, now: number,
varyPath: SegmentVaryPath varyPath: SegmentVaryPath
): BFCacheEntry | null { ): BFCacheEntry | null {
if (typeof window === 'undefined') {
return null
}
const isRevalidation = false const isRevalidation = false
return getFromCacheMap( return getFromCacheMap(
now, now,
......
...@@ -53,7 +53,9 @@ export function useActionQueue( ...@@ -53,7 +53,9 @@ export function useActionQueue(
// of the router state that represents the eventual target if/when the gesture // of the router state that represents the eventual target if/when the gesture
// completes. Otherwise it returns the canonical state. // completes. Otherwise it returns the canonical state.
const [state, setGesture] = useOptimistic(canonicalState) const [state, setGesture] = useOptimistic(canonicalState)
setGestureRouterState = setGesture if (typeof window !== 'undefined') {
setGestureRouterState = setGesture
}
// Because of a known issue that requires to decode Flight streams inside the // Because of a known issue that requires to decode Flight streams inside the
// render phase, we have to be a bit clever and assign the dispatch method to // render phase, we have to be a bit clever and assign the dispatch method to
...@@ -62,22 +64,28 @@ export function useActionQueue( ...@@ -62,22 +64,28 @@ export function useActionQueue(
// Ideally, what we'd do instead is pass the state as a prop to root.render; // Ideally, what we'd do instead is pass the state as a prop to root.render;
// this is conceptually how we're modeling the app router state, despite the // this is conceptually how we're modeling the app router state, despite the
// weird implementation details. // weird implementation details.
let nextDispatch: Dispatch<ReducerActions>
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const { useAppDevRenderingIndicator } = const { useAppDevRenderingIndicator } =
require('../../next-devtools/userspace/use-app-dev-rendering-indicator') as typeof import('../../next-devtools/userspace/use-app-dev-rendering-indicator') require('../../next-devtools/userspace/use-app-dev-rendering-indicator') as typeof import('../../next-devtools/userspace/use-app-dev-rendering-indicator')
// eslint-disable-next-line react-hooks/rules-of-hooks // eslint-disable-next-line react-hooks/rules-of-hooks
const appDevRenderingIndicator = useAppDevRenderingIndicator() const appDevRenderingIndicator = useAppDevRenderingIndicator()
dispatch = (action: ReducerActions) => { nextDispatch = (action: ReducerActions) => {
appDevRenderingIndicator(() => { appDevRenderingIndicator(() => {
actionQueue.dispatch(action, setState) actionQueue.dispatch(action, setState)
}) })
} }
} else { } else {
dispatch = (action: ReducerActions) => nextDispatch = (action: ReducerActions) =>
actionQueue.dispatch(action, setState) actionQueue.dispatch(action, setState)
} }
if (typeof window !== 'undefined') {
dispatch = nextDispatch
}
// When navigating to a non-prefetched route, then App Router state will be // When navigating to a non-prefetched route, then App Router state will be
// blocked until the server responds. We need to transfer the `_debugInfo` // blocked until the server responds. We need to transfer the `_debugInfo`
// from the underlying Flight response onto the top-level promise that is // from the underlying Flight response onto the top-level promise that is
......
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