rsnext/packages/next/shared/lib/app-router-context.ts
Tim Neutkens 419765affe
Add refreshing of Server Components (#38508)
* Add todo

* Reload page when server component changes

* Implement router.reload() that refreshes full tree
2022-07-11 14:02:46 +02:00

44 lines
1.3 KiB
TypeScript

import React from 'react'
import type { FlightRouterState, FlightData } from '../../server/app-render'
export type ChildSegmentMap = Map<string, CacheNode>
type ParallelRoutesCacheNodes = Map<string, ChildSegmentMap>
export type CacheNode = {
data: ReturnType<
typeof import('../../client/components/app-router.client').fetchServerResponse
> | null
subTreeData: null | React.ReactNode
parallelRoutes: ParallelRoutesCacheNodes
}
export type AppRouterInstance = {
reload(): void
push(href: string): void
softPush(href: string): void
replace(href: string): void
softReplace(href: string): void
prefetch(href: string): Promise<void>
}
export const AppRouterContext = React.createContext<AppRouterInstance>(
null as any
)
export const AppTreeContext = React.createContext<{
childNodes: CacheNode['parallelRoutes']
tree: FlightRouterState
url: string
}>(null as any)
export const FullAppTreeContext = React.createContext<{
tree: FlightRouterState
changeByServerResponse: (
previousTree: FlightRouterState,
flightData: FlightData
) => void
}>(null as any)
if (process.env.NODE_ENV !== 'production') {
AppRouterContext.displayName = 'AppRouterContext'
AppTreeContext.displayName = 'AppTreeContext'
FullAppTreeContext.displayName = 'FullAppTreeContext'
}