File size: 2,285 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import { useContext } from 'react'
import { PathnameContext } from '../../shared/lib/hooks-client-context.shared-runtime'
/**
* This checks to see if the current render has any unknown route parameters.
* It's used to trigger a different render path in the error boundary.
*
* @returns true if there are any unknown route parameters, false otherwise
*/
function hasFallbackRouteParams(): boolean {
if (typeof window === 'undefined') {
// AsyncLocalStorage should not be included in the client bundle.
const { workUnitAsyncStorage } =
require('../../server/app-render/work-unit-async-storage.external') as typeof import('../../server/app-render/work-unit-async-storage.external')
const workUnitStore = workUnitAsyncStorage.getStore()
if (!workUnitStore) return false
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
case 'prerender-ppr':
const fallbackParams = workUnitStore.fallbackRouteParams
return fallbackParams ? fallbackParams.size > 0 : false
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
return false
}
return false
}
/**
* This returns a `null` value if there are any unknown route parameters, and
* otherwise returns the pathname from the context. This is an alternative to
* `usePathname` that is used in the error boundary to avoid rendering the
* error boundary when there are unknown route parameters. This doesn't throw
* when accessed with unknown route parameters.
*
* @returns
*
* @internal
*/
export function useUntrackedPathname(): string | null {
// If there are any unknown route parameters we would typically throw
// an error, but this internal method allows us to return a null value instead
// for components that do not propagate the pathname to the static shell (like
// the error boundary).
if (hasFallbackRouteParams()) {
return null
}
// This shouldn't cause any issues related to conditional rendering because
// the environment will be consistent for the render.
// eslint-disable-next-line react-hooks/rules-of-hooks
return useContext(PathnameContext)
}
|