|
|
'use client' |
|
|
|
|
|
import type { |
|
|
CacheNode, |
|
|
LazyCacheNode, |
|
|
LoadingModuleData, |
|
|
} from '../../shared/lib/app-router-context.shared-runtime' |
|
|
import type { |
|
|
FlightRouterState, |
|
|
FlightSegmentPath, |
|
|
} from '../../server/app-render/types' |
|
|
import type { ErrorComponent } from './error-boundary' |
|
|
import { |
|
|
ACTION_SERVER_PATCH, |
|
|
type FocusAndScrollRef, |
|
|
} from './router-reducer/router-reducer-types' |
|
|
|
|
|
import React, { |
|
|
useContext, |
|
|
use, |
|
|
startTransition, |
|
|
Suspense, |
|
|
useDeferredValue, |
|
|
type JSX, |
|
|
} from 'react' |
|
|
import ReactDOM from 'react-dom' |
|
|
import { |
|
|
LayoutRouterContext, |
|
|
GlobalLayoutRouterContext, |
|
|
TemplateContext, |
|
|
} from '../../shared/lib/app-router-context.shared-runtime' |
|
|
import { fetchServerResponse } from './router-reducer/fetch-server-response' |
|
|
import { unresolvedThenable } from './unresolved-thenable' |
|
|
import { ErrorBoundary } from './error-boundary' |
|
|
import { matchSegment } from './match-segments' |
|
|
import { disableSmoothScrollDuringRouteTransition } from '../../shared/lib/router/utils/disable-smooth-scroll' |
|
|
import { RedirectBoundary } from './redirect-boundary' |
|
|
import { HTTPAccessFallbackBoundary } from './http-access-fallback/error-boundary' |
|
|
import { createRouterCacheKey } from './router-reducer/create-router-cache-key' |
|
|
import { hasInterceptionRouteInCurrentTree } from './router-reducer/reducers/has-interception-route-in-current-tree' |
|
|
import { dispatchAppRouterAction } from './use-action-queue' |
|
|
import { useRouterBFCache, type RouterBFCacheEntry } from './bfcache' |
|
|
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' |
|
|
|
|
|
const Activity = process.env.__NEXT_ROUTER_BF_CACHE |
|
|
? (require('react') as typeof import('react')).unstable_Activity |
|
|
: null! |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function walkAddRefetch( |
|
|
segmentPathToWalk: FlightSegmentPath | undefined, |
|
|
treeToRecreate: FlightRouterState |
|
|
): FlightRouterState { |
|
|
if (segmentPathToWalk) { |
|
|
const [segment, parallelRouteKey] = segmentPathToWalk |
|
|
const isLast = segmentPathToWalk.length === 2 |
|
|
|
|
|
if (matchSegment(treeToRecreate[0], segment)) { |
|
|
if (treeToRecreate[1].hasOwnProperty(parallelRouteKey)) { |
|
|
if (isLast) { |
|
|
const subTree = walkAddRefetch( |
|
|
undefined, |
|
|
treeToRecreate[1][parallelRouteKey] |
|
|
) |
|
|
return [ |
|
|
treeToRecreate[0], |
|
|
{ |
|
|
...treeToRecreate[1], |
|
|
[parallelRouteKey]: [ |
|
|
subTree[0], |
|
|
subTree[1], |
|
|
subTree[2], |
|
|
'refetch', |
|
|
], |
|
|
}, |
|
|
] |
|
|
} |
|
|
|
|
|
return [ |
|
|
treeToRecreate[0], |
|
|
{ |
|
|
...treeToRecreate[1], |
|
|
[parallelRouteKey]: walkAddRefetch( |
|
|
segmentPathToWalk.slice(2), |
|
|
treeToRecreate[1][parallelRouteKey] |
|
|
), |
|
|
}, |
|
|
] |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return treeToRecreate |
|
|
} |
|
|
|
|
|
const __DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE = ( |
|
|
ReactDOM as any |
|
|
).__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findDOMNode( |
|
|
instance: React.ReactInstance | null | undefined |
|
|
): Element | Text | null { |
|
|
|
|
|
if (typeof window === 'undefined') return null |
|
|
|
|
|
|
|
|
|
|
|
const internal_reactDOMfindDOMNode = |
|
|
__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE.findDOMNode |
|
|
return internal_reactDOMfindDOMNode(instance) |
|
|
} |
|
|
|
|
|
const rectProperties = [ |
|
|
'bottom', |
|
|
'height', |
|
|
'left', |
|
|
'right', |
|
|
'top', |
|
|
'width', |
|
|
'x', |
|
|
'y', |
|
|
] as const |
|
|
|
|
|
|
|
|
|
|
|
function shouldSkipElement(element: HTMLElement) { |
|
|
|
|
|
|
|
|
|
|
|
if (['sticky', 'fixed'].includes(getComputedStyle(element).position)) { |
|
|
if (process.env.NODE_ENV === 'development') { |
|
|
console.warn( |
|
|
'Skipping auto-scroll behavior due to `position: sticky` or `position: fixed` on element:', |
|
|
element |
|
|
) |
|
|
} |
|
|
return true |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const rect = element.getBoundingClientRect() |
|
|
return rectProperties.every((item) => rect[item] === 0) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function topOfElementInViewport(element: HTMLElement, viewportHeight: number) { |
|
|
const rect = element.getBoundingClientRect() |
|
|
return rect.top >= 0 && rect.top <= viewportHeight |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getHashFragmentDomNode(hashFragment: string) { |
|
|
|
|
|
if (hashFragment === 'top') { |
|
|
return document.body |
|
|
} |
|
|
|
|
|
|
|
|
return ( |
|
|
document.getElementById(hashFragment) ?? |
|
|
|
|
|
document.getElementsByName(hashFragment)[0] |
|
|
) |
|
|
} |
|
|
interface ScrollAndFocusHandlerProps { |
|
|
focusAndScrollRef: FocusAndScrollRef |
|
|
children: React.ReactNode |
|
|
segmentPath: FlightSegmentPath |
|
|
} |
|
|
class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerProps> { |
|
|
handlePotentialScroll = () => { |
|
|
|
|
|
const { focusAndScrollRef, segmentPath } = this.props |
|
|
|
|
|
if (focusAndScrollRef.apply) { |
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
focusAndScrollRef.segmentPaths.length !== 0 && |
|
|
!focusAndScrollRef.segmentPaths.some((scrollRefSegmentPath) => |
|
|
segmentPath.every((segment, index) => |
|
|
matchSegment(segment, scrollRefSegmentPath[index]) |
|
|
) |
|
|
) |
|
|
) { |
|
|
return |
|
|
} |
|
|
|
|
|
let domNode: |
|
|
| ReturnType<typeof getHashFragmentDomNode> |
|
|
| ReturnType<typeof findDOMNode> = null |
|
|
const hashFragment = focusAndScrollRef.hashFragment |
|
|
|
|
|
if (hashFragment) { |
|
|
domNode = getHashFragmentDomNode(hashFragment) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!domNode) { |
|
|
domNode = findDOMNode(this) |
|
|
} |
|
|
|
|
|
|
|
|
if (!(domNode instanceof Element)) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (!(domNode instanceof HTMLElement) || shouldSkipElement(domNode)) { |
|
|
if (process.env.NODE_ENV !== 'production') { |
|
|
if (domNode.parentElement?.localName === 'head') { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (domNode.nextElementSibling === null) { |
|
|
return |
|
|
} |
|
|
domNode = domNode.nextElementSibling |
|
|
} |
|
|
|
|
|
|
|
|
focusAndScrollRef.apply = false |
|
|
focusAndScrollRef.hashFragment = null |
|
|
focusAndScrollRef.segmentPaths = [] |
|
|
|
|
|
disableSmoothScrollDuringRouteTransition( |
|
|
() => { |
|
|
|
|
|
if (hashFragment) { |
|
|
;(domNode as HTMLElement).scrollIntoView() |
|
|
|
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
const htmlElement = document.documentElement |
|
|
const viewportHeight = htmlElement.clientHeight |
|
|
|
|
|
|
|
|
if (topOfElementInViewport(domNode as HTMLElement, viewportHeight)) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
htmlElement.scrollTop = 0 |
|
|
|
|
|
|
|
|
if (!topOfElementInViewport(domNode as HTMLElement, viewportHeight)) { |
|
|
|
|
|
;(domNode as HTMLElement).scrollIntoView() |
|
|
} |
|
|
}, |
|
|
{ |
|
|
|
|
|
dontForceLayout: true, |
|
|
onlyHashChange: focusAndScrollRef.onlyHashChange, |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
focusAndScrollRef.onlyHashChange = false |
|
|
|
|
|
|
|
|
domNode.focus() |
|
|
} |
|
|
} |
|
|
|
|
|
componentDidMount() { |
|
|
this.handlePotentialScroll() |
|
|
} |
|
|
|
|
|
componentDidUpdate() { |
|
|
|
|
|
if (this.props.focusAndScrollRef.apply) { |
|
|
this.handlePotentialScroll() |
|
|
} |
|
|
} |
|
|
|
|
|
render() { |
|
|
return this.props.children |
|
|
} |
|
|
} |
|
|
|
|
|
function ScrollAndFocusHandler({ |
|
|
segmentPath, |
|
|
children, |
|
|
}: { |
|
|
segmentPath: FlightSegmentPath |
|
|
children: React.ReactNode |
|
|
}) { |
|
|
const context = useContext(GlobalLayoutRouterContext) |
|
|
if (!context) { |
|
|
throw new Error('invariant global layout router not mounted') |
|
|
} |
|
|
|
|
|
return ( |
|
|
<InnerScrollAndFocusHandler |
|
|
segmentPath={segmentPath} |
|
|
focusAndScrollRef={context.focusAndScrollRef} |
|
|
> |
|
|
{children} |
|
|
</InnerScrollAndFocusHandler> |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function InnerLayoutRouter({ |
|
|
tree, |
|
|
segmentPath, |
|
|
cacheNode, |
|
|
url, |
|
|
}: { |
|
|
tree: FlightRouterState |
|
|
segmentPath: FlightSegmentPath |
|
|
cacheNode: CacheNode |
|
|
url: string |
|
|
}) { |
|
|
const context = useContext(GlobalLayoutRouterContext) |
|
|
if (!context) { |
|
|
throw new Error('invariant global layout router not mounted') |
|
|
} |
|
|
|
|
|
const { tree: fullTree } = context |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resolvedPrefetchRsc = |
|
|
cacheNode.prefetchRsc !== null ? cacheNode.prefetchRsc : cacheNode.rsc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const rsc: any = useDeferredValue(cacheNode.rsc, resolvedPrefetchRsc) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resolvedRsc: React.ReactNode = |
|
|
typeof rsc === 'object' && rsc !== null && typeof rsc.then === 'function' |
|
|
? use(rsc) |
|
|
: rsc |
|
|
|
|
|
if (!resolvedRsc) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let lazyData = cacheNode.lazyData |
|
|
if (lazyData === null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const refetchTree = walkAddRefetch(['', ...segmentPath], fullTree) |
|
|
const includeNextUrl = hasInterceptionRouteInCurrentTree(fullTree) |
|
|
const navigatedAt = Date.now() |
|
|
cacheNode.lazyData = lazyData = fetchServerResponse( |
|
|
new URL(url, location.origin), |
|
|
{ |
|
|
flightRouterState: refetchTree, |
|
|
nextUrl: includeNextUrl ? context.nextUrl : null, |
|
|
} |
|
|
).then((serverResponse) => { |
|
|
startTransition(() => { |
|
|
dispatchAppRouterAction({ |
|
|
type: ACTION_SERVER_PATCH, |
|
|
previousTree: fullTree, |
|
|
serverResponse, |
|
|
navigatedAt, |
|
|
}) |
|
|
}) |
|
|
|
|
|
return serverResponse |
|
|
}) |
|
|
|
|
|
|
|
|
use(lazyData) |
|
|
} |
|
|
|
|
|
|
|
|
use(unresolvedThenable) as never |
|
|
} |
|
|
|
|
|
|
|
|
const subtree = ( |
|
|
|
|
|
<LayoutRouterContext.Provider |
|
|
value={{ |
|
|
parentTree: tree, |
|
|
parentCacheNode: cacheNode, |
|
|
parentSegmentPath: segmentPath, |
|
|
|
|
|
// TODO-APP: overriding of url for parallel routes |
|
|
url: url, |
|
|
}} |
|
|
> |
|
|
{resolvedRsc} |
|
|
</LayoutRouterContext.Provider> |
|
|
) |
|
|
|
|
|
return subtree |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function LoadingBoundary({ |
|
|
loading, |
|
|
children, |
|
|
}: { |
|
|
loading: LoadingModuleData | Promise<LoadingModuleData> |
|
|
children: React.ReactNode |
|
|
}): JSX.Element { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let loadingModuleData |
|
|
if ( |
|
|
typeof loading === 'object' && |
|
|
loading !== null && |
|
|
typeof (loading as any).then === 'function' |
|
|
) { |
|
|
const promiseForLoading = loading as Promise<LoadingModuleData> |
|
|
loadingModuleData = use(promiseForLoading) |
|
|
} else { |
|
|
loadingModuleData = loading as LoadingModuleData |
|
|
} |
|
|
|
|
|
if (loadingModuleData) { |
|
|
const loadingRsc = loadingModuleData[0] |
|
|
const loadingStyles = loadingModuleData[1] |
|
|
const loadingScripts = loadingModuleData[2] |
|
|
return ( |
|
|
<Suspense |
|
|
fallback={ |
|
|
<> |
|
|
{loadingStyles} |
|
|
{loadingScripts} |
|
|
{loadingRsc} |
|
|
</> |
|
|
} |
|
|
> |
|
|
{children} |
|
|
</Suspense> |
|
|
) |
|
|
} |
|
|
|
|
|
return <>{children}</> |
|
|
} |
|
|
|
|
|
function RenderChildren({ children }: { children: React.ReactNode }) { |
|
|
return <>{children}</> |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function OuterLayoutRouter({ |
|
|
parallelRouterKey, |
|
|
error, |
|
|
errorStyles, |
|
|
errorScripts, |
|
|
templateStyles, |
|
|
templateScripts, |
|
|
template, |
|
|
notFound, |
|
|
forbidden, |
|
|
unauthorized, |
|
|
gracefullyDegrade, |
|
|
segmentViewBoundaries, |
|
|
}: { |
|
|
parallelRouterKey: string |
|
|
error: ErrorComponent | undefined |
|
|
errorStyles: React.ReactNode | undefined |
|
|
errorScripts: React.ReactNode | undefined |
|
|
templateStyles: React.ReactNode | undefined |
|
|
templateScripts: React.ReactNode | undefined |
|
|
template: React.ReactNode |
|
|
notFound: React.ReactNode | undefined |
|
|
forbidden: React.ReactNode | undefined |
|
|
unauthorized: React.ReactNode | undefined |
|
|
gracefullyDegrade?: boolean |
|
|
segmentViewBoundaries?: React.ReactNode |
|
|
}) { |
|
|
const context = useContext(LayoutRouterContext) |
|
|
if (!context) { |
|
|
throw new Error('invariant expected layout router to be mounted') |
|
|
} |
|
|
|
|
|
const { parentTree, parentCacheNode, parentSegmentPath, url } = context |
|
|
|
|
|
|
|
|
|
|
|
const parentParallelRoutes = parentCacheNode.parallelRoutes |
|
|
let segmentMap = parentParallelRoutes.get(parallelRouterKey) |
|
|
|
|
|
|
|
|
if (!segmentMap) { |
|
|
segmentMap = new Map() |
|
|
parentParallelRoutes.set(parallelRouterKey, segmentMap) |
|
|
} |
|
|
const parentTreeSegment = parentTree[0] |
|
|
const segmentPath = |
|
|
parentSegmentPath === null |
|
|
? |
|
|
|
|
|
|
|
|
[parallelRouterKey] |
|
|
: parentSegmentPath.concat([parentTreeSegment, parallelRouterKey]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const activeTree = parentTree[1][parallelRouterKey] |
|
|
const activeSegment = activeTree[0] |
|
|
const activeStateKey = createRouterCacheKey(activeSegment, true) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let bfcacheEntry: RouterBFCacheEntry | null = useRouterBFCache( |
|
|
activeTree, |
|
|
activeStateKey |
|
|
) |
|
|
let children: Array<React.ReactNode> = [] |
|
|
do { |
|
|
const tree = bfcacheEntry.tree |
|
|
const stateKey = bfcacheEntry.stateKey |
|
|
const segment = tree[0] |
|
|
const cacheKey = createRouterCacheKey(segment) |
|
|
|
|
|
|
|
|
let cacheNode = segmentMap.get(cacheKey) |
|
|
if (cacheNode === undefined) { |
|
|
|
|
|
|
|
|
const newLazyCacheNode: LazyCacheNode = { |
|
|
lazyData: null, |
|
|
rsc: null, |
|
|
prefetchRsc: null, |
|
|
head: null, |
|
|
prefetchHead: null, |
|
|
parallelRoutes: new Map(), |
|
|
loading: null, |
|
|
navigatedAt: -1, |
|
|
} |
|
|
|
|
|
|
|
|
cacheNode = newLazyCacheNode |
|
|
segmentMap.set(cacheKey, newLazyCacheNode) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ErrorBoundaryComponent = gracefullyDegrade |
|
|
? RenderChildren |
|
|
: ErrorBoundary |
|
|
|
|
|
let segmentBoundaryTriggerNode: React.ReactNode = null |
|
|
let segmentViewStateNode: React.ReactNode = null |
|
|
if ( |
|
|
process.env.NODE_ENV !== 'production' && |
|
|
process.env.__NEXT_DEVTOOL_SEGMENT_EXPLORER |
|
|
) { |
|
|
const { SegmentBoundaryTriggerNode, SegmentViewStateNode } = |
|
|
require('../../next-devtools/userspace/app/segment-explorer-node') as typeof import('../../next-devtools/userspace/app/segment-explorer-node') |
|
|
|
|
|
const pagePrefix = normalizeAppPath(url) |
|
|
segmentViewStateNode = ( |
|
|
<SegmentViewStateNode key={pagePrefix} page={pagePrefix} /> |
|
|
) |
|
|
|
|
|
segmentBoundaryTriggerNode = ( |
|
|
<> |
|
|
<SegmentBoundaryTriggerNode /> |
|
|
</> |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const loadingModuleData = parentCacheNode.loading |
|
|
let child = ( |
|
|
<TemplateContext.Provider |
|
|
key={stateKey} |
|
|
value={ |
|
|
<ScrollAndFocusHandler segmentPath={segmentPath}> |
|
|
<ErrorBoundaryComponent |
|
|
errorComponent={error} |
|
|
errorStyles={errorStyles} |
|
|
errorScripts={errorScripts} |
|
|
> |
|
|
<LoadingBoundary loading={loadingModuleData}> |
|
|
<HTTPAccessFallbackBoundary |
|
|
notFound={notFound} |
|
|
forbidden={forbidden} |
|
|
unauthorized={unauthorized} |
|
|
> |
|
|
<RedirectBoundary> |
|
|
<InnerLayoutRouter |
|
|
url={url} |
|
|
tree={tree} |
|
|
cacheNode={cacheNode} |
|
|
segmentPath={segmentPath} |
|
|
/> |
|
|
{segmentBoundaryTriggerNode} |
|
|
</RedirectBoundary> |
|
|
</HTTPAccessFallbackBoundary> |
|
|
</LoadingBoundary> |
|
|
</ErrorBoundaryComponent> |
|
|
{segmentViewStateNode} |
|
|
</ScrollAndFocusHandler> |
|
|
} |
|
|
> |
|
|
{templateStyles} |
|
|
{templateScripts} |
|
|
{template} |
|
|
</TemplateContext.Provider> |
|
|
) |
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') { |
|
|
const { SegmentStateProvider } = |
|
|
require('../../next-devtools/userspace/app/segment-explorer-node') as typeof import('../../next-devtools/userspace/app/segment-explorer-node') |
|
|
|
|
|
child = ( |
|
|
<SegmentStateProvider key={stateKey}> |
|
|
{child} |
|
|
{segmentViewBoundaries} |
|
|
</SegmentStateProvider> |
|
|
) |
|
|
} |
|
|
|
|
|
if (process.env.__NEXT_ROUTER_BF_CACHE) { |
|
|
child = ( |
|
|
<Activity |
|
|
key={stateKey} |
|
|
mode={stateKey === activeStateKey ? 'visible' : 'hidden'} |
|
|
> |
|
|
{child} |
|
|
</Activity> |
|
|
) |
|
|
} |
|
|
|
|
|
children.push(child) |
|
|
|
|
|
bfcacheEntry = bfcacheEntry.next |
|
|
} while (bfcacheEntry !== null) |
|
|
|
|
|
return children |
|
|
} |
|
|
|