-
Notifications
You must be signed in to change notification settings - Fork 26k
/
app-router-context.shared-runtime.ts
137 lines (127 loc) · 3.29 KB
/
app-router-context.shared-runtime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use client'
import type {
FocusAndScrollRef,
PrefetchKind,
ThenableRecord,
} from '../../client/components/router-reducer/router-reducer-types'
import type { FetchServerResponseResult } from '../../client/components/router-reducer/fetch-server-response'
import type {
FlightRouterState,
FlightData,
} from '../../server/app-render/types'
import React from 'react'
export type ChildSegmentMap = Map<string, CacheNode>
// eslint-disable-next-line no-shadow
export enum CacheStates {
LAZY_INITIALIZED = 'LAZYINITIALIZED',
DATA_FETCH = 'DATAFETCH',
READY = 'READY',
}
/**
* Cache node used in app-router / layout-router.
*/
export type CacheNode =
| {
status: CacheStates.DATA_FETCH
/**
* In-flight request for this node.
*/
data: ThenableRecord<FetchServerResponseResult> | null
head?: React.ReactNode
/**
* React Component for this node.
*/
subTreeData: null
/**
* Child parallel routes.
*/
parallelRoutes: Map<string, ChildSegmentMap>
}
| {
status: CacheStates.READY
/**
* In-flight request for this node.
*/
data: null
head?: React.ReactNode
/**
* React Component for this node.
*/
subTreeData: React.ReactNode
/**
* Child parallel routes.
*/
parallelRoutes: Map<string, ChildSegmentMap>
}
| {
status: CacheStates.LAZY_INITIALIZED
data: null
head?: React.ReactNode
subTreeData: null
/**
* Child parallel routes.
*/
parallelRoutes: Map<string, ChildSegmentMap>
}
export interface NavigateOptions {
/** @internal */
forceOptimisticNavigation?: boolean
scroll?: boolean
}
export interface PrefetchOptions {
kind: PrefetchKind
}
export interface AppRouterInstance {
/**
* Navigate to the previous history entry.
*/
back(): void
/**
* Navigate to the next history entry.
*/
forward(): void
/**
* Refresh the current page.
*/
refresh(): void
/**
* Navigate to the provided href.
* Pushes a new history entry.
*/
push(href: string, options?: NavigateOptions): void
/**
* Navigate to the provided href.
* Replaces the current history entry.
*/
replace(href: string, options?: NavigateOptions): void
/**
* Prefetch the provided href.
*/
prefetch(href: string, options?: PrefetchOptions): void
}
export const AppRouterContext = React.createContext<AppRouterInstance | null>(
null
)
export const LayoutRouterContext = React.createContext<{
childNodes: CacheNode['parallelRoutes']
tree: FlightRouterState
url: string
}>(null as any)
export const GlobalLayoutRouterContext = React.createContext<{
buildId: string
tree: FlightRouterState
changeByServerResponse: (
previousTree: FlightRouterState,
flightData: FlightData,
overrideCanonicalUrl: URL | undefined
) => void
focusAndScrollRef: FocusAndScrollRef
nextUrl: string | null
}>(null as any)
export const TemplateContext = React.createContext<React.ReactNode>(null as any)
if (process.env.NODE_ENV !== 'production') {
AppRouterContext.displayName = 'AppRouterContext'
LayoutRouterContext.displayName = 'LayoutRouterContext'
GlobalLayoutRouterContext.displayName = 'GlobalLayoutRouterContext'
TemplateContext.displayName = 'TemplateContext'
}