Unverified Commit 0b0dbd79 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Attach active src route to next global (#90171)

## Summary
- expose the active app source page on `window.next.__n_src_page`
- derive the source page from the client router tree instead of sending
it via `InitialRSCPayload`
- ensure the value updates on client transitions (including dynamic
routes and route groups)
- extend app-dir e2e coverage for initial load and transition behavior

## Testing
- `pnpm test-dev test/e2e/app-dir/app/index.test.ts -t "__n_src_page"`
- `pnpm --filter=next types`
parent 633edeb5
...@@ -31,7 +31,10 @@ import { findHeadInCache } from './router-reducer/reducers/find-head-in-cache' ...@@ -31,7 +31,10 @@ import { findHeadInCache } from './router-reducer/reducers/find-head-in-cache'
import { unresolvedThenable } from './unresolved-thenable' import { unresolvedThenable } from './unresolved-thenable'
import { removeBasePath } from '../remove-base-path' import { removeBasePath } from '../remove-base-path'
import { hasBasePath } from '../has-base-path' import { hasBasePath } from '../has-base-path'
import { getSelectedParams } from './router-reducer/compute-changed-path' import {
extractSourcePageFromFlightRouterState,
getSelectedParams,
} from './router-reducer/compute-changed-path'
import { useNavFailureHandler } from './nav-failure-handler' import { useNavFailureHandler } from './nav-failure-handler'
import { import {
dispatchTraverseAction, dispatchTraverseAction,
...@@ -191,6 +194,16 @@ function Router({ ...@@ -191,6 +194,16 @@ function Router({
}, [cache, tree]) }, [cache, tree])
} }
useEffect(() => {
const sourcePage = extractSourcePageFromFlightRouterState(state.tree)
if (sourcePage !== undefined) {
window.next.__internal_src_page = sourcePage
} else {
delete window.next.__internal_src_page
}
}, [state.tree])
useEffect(() => { useEffect(() => {
// If the app is restored from bfcache, it's possible that // If the app is restored from bfcache, it's possible that
// pushRef.mpaNavigation is true, which would mean that any re-render of this component // pushRef.mpaNavigation is true, which would mean that any re-render of this component
......
...@@ -27,6 +27,44 @@ const segmentToPathname = (segment: Segment): string => { ...@@ -27,6 +27,44 @@ const segmentToPathname = (segment: Segment): string => {
return segment[1] return segment[1]
} }
const segmentToSourcePagePathname = (segment: Segment): string => {
if (typeof segment === 'string') {
if (segment === 'children') return ''
if (segment.startsWith(PAGE_SEGMENT_KEY)) return 'page'
return segment
}
const [paramName, , dynamicParamType] = segment
switch (dynamicParamType) {
case 'c':
return `[...${paramName}]`
case 'ci(..)(..)':
return `(..)(..)[...${paramName}]`
case 'ci(.)':
return `(.)[...${paramName}]`
case 'ci(..)':
return `(..)[...${paramName}]`
case 'ci(...)':
return `(...)[...${paramName}]`
case 'oc':
return `[[...${paramName}]]`
case 'd':
return `[${paramName}]`
case 'di(..)(..)':
return `(..)(..)[${paramName}]`
case 'di(.)':
return `(.)[${paramName}]`
case 'di(..)':
return `(..)[${paramName}]`
case 'di(...)':
return `(...)[${paramName}]`
default:
dynamicParamType satisfies never
return `[${paramName}]`
}
}
function normalizeSegments(segments: string[]): string { function normalizeSegments(segments: string[]): string {
return ( return (
segments.reduce((acc, segment) => { segments.reduce((acc, segment) => {
...@@ -79,6 +117,55 @@ export function extractPathFromFlightRouterState( ...@@ -79,6 +117,55 @@ export function extractPathFromFlightRouterState(
return normalizeSegments(segments) return normalizeSegments(segments)
} }
function extractSourcePageSegmentsFromFlightRouterState(
flightRouterState: FlightRouterState
): string[] | undefined {
const segment = segmentToSourcePagePathname(flightRouterState[0])
if (segment === DEFAULT_SEGMENT_KEY) {
return undefined
}
if (segment === 'page') {
return [segment]
}
const parallelRoutes = flightRouterState[1] ?? {}
const childrenPath = parallelRoutes.children
? extractSourcePageSegmentsFromFlightRouterState(parallelRoutes.children)
: undefined
if (childrenPath !== undefined) {
return segment === ''
? childrenPath
: [removeLeadingSlash(segment), ...childrenPath]
}
for (const [key, value] of Object.entries(parallelRoutes)) {
if (key === 'children') continue
const childPath = extractSourcePageSegmentsFromFlightRouterState(value)
if (childPath !== undefined) {
return segment === ''
? childPath
: [removeLeadingSlash(segment), ...childPath]
}
}
return undefined
}
export function extractSourcePageFromFlightRouterState(
flightRouterState: FlightRouterState
): string | undefined {
const sourcePageSegments =
extractSourcePageSegmentsFromFlightRouterState(flightRouterState)
return sourcePageSegments ? `/${sourcePageSegments.join('/')}` : undefined
}
function computeChangedPathImpl( function computeChangedPathImpl(
treeA: FlightRouterState, treeA: FlightRouterState,
treeB: FlightRouterState treeB: FlightRouterState
......
...@@ -145,6 +145,56 @@ describe('app dir - basic', () => { ...@@ -145,6 +145,56 @@ describe('app dir - basic', () => {
}) })
}) })
it.each([
{
path: '/dashboard',
srcPage: '/dashboard/page',
},
{
path: '/dynamic/category-1/id-2',
srcPage: '/dynamic/[category]/[id]/page',
},
{
path: '/dashboard/another',
srcPage: '/(newroot)/dashboard/another/page',
},
])(
'should expose app source page on window.next.__internal_src_page for $path',
async ({ path, srcPage }) => {
const browser = await next.browser(path)
await retry(async () => {
expect(await browser.eval('window.next.__internal_src_page')).toBe(
srcPage
)
})
}
)
it('should update window.next.__internal_src_page on app router transitions', async () => {
const browser = await next.browser('/dashboard')
await retry(async () => {
expect(await browser.eval('window.next.__internal_src_page')).toBe(
'/dashboard/page'
)
})
await browser.eval(`window.next.router.push('/dynamic/category-1/id-2')`)
await retry(async () => {
expect(await browser.eval('window.next.__internal_src_page')).toBe(
'/dynamic/[category]/[id]/page'
)
})
await browser.eval(`window.next.router.push('/dashboard/another')`)
await retry(async () => {
expect(await browser.eval('window.next.__internal_src_page')).toBe(
'/(newroot)/dashboard/another/page'
)
})
})
if (!isNextDev) { if (!isNextDev) {
it('should successfully detect app route during prefetch', async () => { it('should successfully detect app route during prefetch', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
......
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