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