Unverified Commit 07c3464a authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Refactor router reducer (#38983)

parent c3fd9e4e
......@@ -130,7 +130,6 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
? `require('next/dist/client/components/hot-reloader.client.js').default`
: 'null'
}
export const hooksClientContext = require('next/dist/client/components/hooks-client-context.js')
export const __next_app_webpack_require__ = __webpack_require__
`
......
import { createContext } from 'react'
import { NextParsedUrlQuery } from '../../server/request-meta'
export const QueryContext = createContext<NextParsedUrlQuery>(null as any)
export const SearchParamsContext = createContext<NextParsedUrlQuery>(
null as any
)
export const PathnameContext = createContext<string>(null as any)
export const ParamsContext = createContext(null as any)
export const LayoutSegmentsContext = createContext(null as any)
if (process.env.NODE_ENV !== 'production') {
QueryContext.displayName = 'QueryContext'
SearchParamsContext.displayName = 'SearchParamsContext'
PathnameContext.displayName = 'PathnameContext'
ParamsContext.displayName = 'ParamsContext'
LayoutSegmentsContext.displayName = 'LayoutSegmentsContext'
......
......@@ -2,7 +2,7 @@
import { useContext } from 'react'
import {
QueryContext,
SearchParamsContext,
// ParamsContext,
PathnameContext,
// LayoutSegmentsContext,
......@@ -13,11 +13,11 @@ import {
} from '../../shared/lib/app-router-context'
export function useSearchParams() {
return useContext(QueryContext)
return useContext(SearchParamsContext)
}
export function useSearchParam(key: string): string | string[] {
const params = useContext(QueryContext)
const params = useContext(SearchParamsContext)
return params[key]
}
......
......@@ -6,7 +6,7 @@ import {
// @ts-expect-error TODO-APP: startTransition exists
startTransition,
} from 'react'
import { FullAppTreeContext } from '../../shared/lib/app-router-context'
import { GlobalLayoutRouterContext } from '../../shared/lib/app-router-context'
import {
register,
unregister,
......@@ -397,7 +397,7 @@ function processMessage(
}
export default function HotReload({ assetPrefix }: { assetPrefix: string }) {
const { tree } = useContext(FullAppTreeContext)
const { tree } = useContext(GlobalLayoutRouterContext)
const router = useRouter()
const webSocketRef = useRef<WebSocket>()
......
......@@ -8,7 +8,7 @@ import type {
} from '../../server/app-render'
import {
AppTreeContext,
FullAppTreeContext,
GlobalLayoutRouterContext,
} from '../../shared/lib/app-router-context'
import { fetchServerResponse } from './app-router.client'
import { matchSegment } from './match-segments'
......@@ -72,7 +72,7 @@ export function InnerLayoutRouter({
changeByServerResponse,
tree: fullTree,
focusRef,
} = useContext(FullAppTreeContext)
} = useContext(GlobalLayoutRouterContext)
const focusAndScrollRef = useRef<HTMLDivElement>(null)
useEffect(() => {
......
This diff is collapsed.
......@@ -830,29 +830,17 @@ export async function renderToHTML(
const AppRouter =
ComponentMod.AppRouter as typeof import('../client/components/app-router.client').default
const {
QueryContext,
PathnameContext,
// ParamsContext,
// LayoutSegmentsContext,
} = ComponentMod.hooksClientContext as typeof import('../client/components/hooks-client-context')
const WrappedComponentTreeWithRouter = () => {
return (
<QueryContext.Provider value={query}>
<PathnameContext.Provider value={pathname}>
{/* <ParamsContext.Provider value={pathParams}> */}
<AppRouter
hotReloader={HotReloader && <HotReloader assetPrefix="" />}
initialCanonicalUrl={initialCanonicalUrl}
initialTree={initialTree}
initialStylesheets={initialStylesheets}
>
<ComponentTree />
</AppRouter>
{/* </ParamsContext.Provider> */}
</PathnameContext.Provider>
</QueryContext.Provider>
<AppRouter
hotReloader={HotReloader && <HotReloader assetPrefix="" />}
initialCanonicalUrl={initialCanonicalUrl}
initialTree={initialTree}
initialStylesheets={initialStylesheets}
>
<ComponentTree />
</AppRouter>
)
}
......
......@@ -14,11 +14,33 @@ export type CacheNode = {
}
export type AppRouterInstance = {
/**
* Reload the current page. Fetches new data from the server.
*/
reload(): void
/**
* Hard navigate to the provided href. Fetches new data from the server.
* Pushes a new history entry.
*/
push(href: string): void
/**
* Soft navigate to the provided href. Does not fetch data from the server if it was already fetched.
* Pushes a new history entry.
*/
softPush(href: string): void
/**
* Hard navigate to the provided href. Does not fetch data from the server if it was already fetched.
* Replaces the current history entry.
*/
replace(href: string): void
/**
* Soft navigate to the provided href. Does not fetch data from the server if it was already fetched.
* Replaces the current history entry.
*/
softReplace(href: string): void
/**
* Soft prefetch the provided href. Does not fetch data from the server if it was already fetched.
*/
prefetch(href: string): Promise<void>
}
......@@ -31,7 +53,7 @@ export const AppTreeContext = React.createContext<{
url: string
stylesheets?: string[]
}>(null as any)
export const FullAppTreeContext = React.createContext<{
export const GlobalLayoutRouterContext = React.createContext<{
tree: FlightRouterState
changeByServerResponse: (
previousTree: FlightRouterState,
......@@ -43,5 +65,5 @@ export const FullAppTreeContext = React.createContext<{
if (process.env.NODE_ENV !== 'production') {
AppRouterContext.displayName = 'AppRouterContext'
AppTreeContext.displayName = 'AppTreeContext'
FullAppTreeContext.displayName = 'FullAppTreeContext'
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