Unverified Commit 8fc8a6c8 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add comments in new router reducer (#39025)

Expand explanation of new router.



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 90ddd0dc
...@@ -2,7 +2,7 @@ import React, { useEffect } from 'react' ...@@ -2,7 +2,7 @@ import React, { useEffect } from 'react'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack' import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { import {
AppRouterContext, AppRouterContext,
AppTreeContext, LayoutRouterContext,
GlobalLayoutRouterContext, GlobalLayoutRouterContext,
} from '../../shared/lib/app-router-context' } from '../../shared/lib/app-router-context'
import type { import type {
...@@ -99,7 +99,7 @@ export default function AppRouter({ ...@@ -99,7 +99,7 @@ export default function AppRouter({
children: React.ReactNode children: React.ReactNode
hotReloader?: React.ReactNode hotReloader?: React.ReactNode
}) { }) {
const [{ tree, cache, pushRef, focusRef, canonicalUrl }, dispatch] = const [{ tree, cache, pushRef, focusAndScrollRef, canonicalUrl }, dispatch] =
React.useReducer(reducer, { React.useReducer(reducer, {
tree: initialTree, tree: initialTree,
cache: { cache: {
...@@ -109,7 +109,7 @@ export default function AppRouter({ ...@@ -109,7 +109,7 @@ export default function AppRouter({
typeof window === 'undefined' ? new Map() : initialParallelRoutes, typeof window === 'undefined' ? new Map() : initialParallelRoutes,
}, },
pushRef: { pendingPush: false, mpaNavigation: false }, pushRef: { pendingPush: false, mpaNavigation: false },
focusRef: { focus: false }, focusAndScrollRef: { apply: false },
canonicalUrl: canonicalUrl:
initialCanonicalUrl + initialCanonicalUrl +
// Hash is read as the initial value for canonicalUrl in the browser // Hash is read as the initial value for canonicalUrl in the browser
...@@ -300,11 +300,11 @@ export default function AppRouter({ ...@@ -300,11 +300,11 @@ export default function AppRouter({
value={{ value={{
changeByServerResponse, changeByServerResponse,
tree, tree,
focusRef, focusAndScrollRef,
}} }}
> >
<AppRouterContext.Provider value={appRouter}> <AppRouterContext.Provider value={appRouter}>
<AppTreeContext.Provider <LayoutRouterContext.Provider
value={{ value={{
childNodes: cache.parallelRoutes, childNodes: cache.parallelRoutes,
tree: tree, tree: tree,
...@@ -324,7 +324,7 @@ export default function AppRouter({ ...@@ -324,7 +324,7 @@ export default function AppRouter({
// HotReloader uses the router tree and router.reload() in order to apply Server Component changes. // HotReloader uses the router tree and router.reload() in order to apply Server Component changes.
hotReloader hotReloader
} }
</AppTreeContext.Provider> </LayoutRouterContext.Provider>
</AppRouterContext.Provider> </AppRouterContext.Provider>
</GlobalLayoutRouterContext.Provider> </GlobalLayoutRouterContext.Provider>
</SearchParamsContext.Provider> </SearchParamsContext.Provider>
......
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
} from './hooks-client-context' } from './hooks-client-context'
import { import {
AppRouterContext, AppRouterContext,
AppTreeContext, LayoutRouterContext,
} from '../../shared/lib/app-router-context' } from '../../shared/lib/app-router-context'
/** /**
...@@ -59,7 +59,7 @@ export function usePathname(): string { ...@@ -59,7 +59,7 @@ export function usePathname(): string {
export function useSelectedLayoutSegment( export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children' parallelRouteKey: string = 'children'
): string { ): string {
const { tree } = useContext(AppTreeContext) const { tree } = useContext(LayoutRouterContext)
const segment = tree[1][parallelRouteKey][0] const segment = tree[1][parallelRouteKey][0]
......
This diff is collapsed.
import React from 'react' import React from 'react'
import type { FocusRef } from '../../client/components/reducer' import type { FocusAndScrollRef } from '../../client/components/reducer'
import type { FlightRouterState, FlightData } from '../../server/app-render' import type { FlightRouterState, FlightData } from '../../server/app-render'
export type ChildSegmentMap = Map<string, CacheNode> export type ChildSegmentMap = Map<string, CacheNode>
...@@ -46,7 +46,7 @@ export type AppRouterInstance = { ...@@ -46,7 +46,7 @@ export type AppRouterInstance = {
export const AppRouterContext = React.createContext<AppRouterInstance>( export const AppRouterContext = React.createContext<AppRouterInstance>(
null as any null as any
) )
export const AppTreeContext = React.createContext<{ export const LayoutRouterContext = React.createContext<{
childNodes: CacheNode['parallelRoutes'] childNodes: CacheNode['parallelRoutes']
tree: FlightRouterState tree: FlightRouterState
url: string url: string
...@@ -58,11 +58,11 @@ export const GlobalLayoutRouterContext = React.createContext<{ ...@@ -58,11 +58,11 @@ export const GlobalLayoutRouterContext = React.createContext<{
previousTree: FlightRouterState, previousTree: FlightRouterState,
flightData: FlightData flightData: FlightData
) => void ) => void
focusRef: FocusRef focusAndScrollRef: FocusAndScrollRef
}>(null as any) }>(null as any)
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
AppRouterContext.displayName = 'AppRouterContext' AppRouterContext.displayName = 'AppRouterContext'
AppTreeContext.displayName = 'AppTreeContext' LayoutRouterContext.displayName = 'LayoutRouterContext'
GlobalLayoutRouterContext.displayName = 'GlobalLayoutRouterContext' GlobalLayoutRouterContext.displayName = 'GlobalLayoutRouterContext'
} }
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