forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.tsx
248 lines (235 loc) · 6.59 KB
/
server.tsx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import * as React from "react";
import type {
RevalidationState,
Router as RemixRouter,
StaticHandlerContext,
} from "@remix-run/router";
import {
IDLE_FETCHER,
IDLE_NAVIGATION,
Action,
invariant,
UNSAFE_convertRoutesToDataRoutes as convertRoutesToDataRoutes,
} from "@remix-run/router";
import type { Location, RouteObject, To } from "react-router-dom";
import { Routes } from "react-router-dom";
import {
createPath,
parsePath,
Router,
UNSAFE_DataRouterContext as DataRouterContext,
UNSAFE_DataRouterStateContext as DataRouterStateContext,
UNSAFE_DataStaticRouterContext as DataStaticRouterContext,
} from "react-router-dom";
export interface StaticRouterProps {
basename?: string;
children?: React.ReactNode;
location: Partial<Location> | string;
}
/**
* A <Router> that may not navigate to any other location. This is useful
* on the server where there is no stateful UI.
*/
export function StaticRouter({
basename,
children,
location: locationProp = "/",
}: StaticRouterProps) {
if (typeof locationProp === "string") {
locationProp = parsePath(locationProp);
}
let action = Action.Pop;
let location: Location = {
pathname: locationProp.pathname || "/",
search: locationProp.search || "",
hash: locationProp.hash || "",
state: locationProp.state || null,
key: locationProp.key || "default",
};
let staticNavigator = getStatelessNavigator();
return (
<Router
basename={basename}
children={children}
location={location}
navigationType={action}
navigator={staticNavigator}
static={true}
/>
);
}
export interface StaticRouterProviderProps {
basename?: string;
context: StaticHandlerContext;
router: RemixRouter;
hydrate?: boolean;
nonce?: string;
}
/**
* A Data Router that may not navigate to any other location. This is useful
* on the server where there is no stateful UI.
*/
export function unstable_StaticRouterProvider({
basename,
context,
router,
hydrate = true,
nonce,
}: StaticRouterProviderProps) {
invariant(
router && context,
"You must provide `router` and `context` to <StaticRouterProvider>"
);
let dataRouterContext = {
router,
navigator: getStatelessNavigator(),
static: true,
basename: basename || "/",
};
let hydrateScript = "";
if (hydrate !== false) {
let data = {
loaderData: context.loaderData,
actionData: context.actionData,
errors: context.errors,
};
// Use JSON.parse here instead of embedding a raw JS object here to speed
// up parsing on the client. Dual-stringify is needed to ensure all quotes
// are properly escaped in the resulting string. See:
// https://v8.dev/blog/cost-of-javascript-2019#json
let json = JSON.stringify(JSON.stringify(data));
hydrateScript = `window.__staticRouterHydrationData = JSON.parse(${json});`;
}
return (
<>
<DataStaticRouterContext.Provider value={context}>
<DataRouterContext.Provider value={dataRouterContext}>
<DataRouterStateContext.Provider
value={dataRouterContext.router.state}
>
<Router
basename={dataRouterContext.basename}
location={dataRouterContext.router.state.location}
navigationType={dataRouterContext.router.state.historyAction}
navigator={dataRouterContext.navigator}
>
<Routes />
</Router>
</DataRouterStateContext.Provider>
</DataRouterContext.Provider>
</DataStaticRouterContext.Provider>
{hydrateScript ? (
<script
suppressHydrationWarning
nonce={nonce}
dangerouslySetInnerHTML={{ __html: hydrateScript }}
/>
) : null}
</>
);
}
function getStatelessNavigator() {
return {
createHref(to: To) {
return typeof to === "string" ? to : createPath(to);
},
push(to: To) {
throw new Error(
`You cannot use navigator.push() on the server because it is a stateless ` +
`environment. This error was probably triggered when you did a ` +
`\`navigate(${JSON.stringify(to)})\` somewhere in your app.`
);
},
replace(to: To) {
throw new Error(
`You cannot use navigator.replace() on the server because it is a stateless ` +
`environment. This error was probably triggered when you did a ` +
`\`navigate(${JSON.stringify(to)}, { replace: true })\` somewhere ` +
`in your app.`
);
},
go(delta: number) {
throw new Error(
`You cannot use navigator.go() on the server because it is a stateless ` +
`environment. This error was probably triggered when you did a ` +
`\`navigate(${delta})\` somewhere in your app.`
);
},
back() {
throw new Error(
`You cannot use navigator.back() on the server because it is a stateless ` +
`environment.`
);
},
forward() {
throw new Error(
`You cannot use navigator.forward() on the server because it is a stateless ` +
`environment.`
);
},
};
}
export function unstable_createStaticRouter(
routes: RouteObject[],
context: StaticHandlerContext
): RemixRouter {
let dataRoutes = convertRoutesToDataRoutes(routes);
let msg = (method: string) =>
`You cannot use router.${method}() on the server because it is a stateless environment`;
return {
get basename() {
return "/";
},
get state() {
return {
historyAction: Action.Pop,
location: context.location,
matches: context.matches,
loaderData: context.loaderData,
actionData: context.actionData,
errors: context.errors,
initialized: true,
navigation: IDLE_NAVIGATION,
restoreScrollPosition: null,
preventScrollReset: false,
revalidation: "idle" as RevalidationState,
fetchers: new Map(),
};
},
get routes() {
return dataRoutes;
},
initialize() {
throw msg("initialize");
},
subscribe() {
throw msg("subscribe");
},
enableScrollRestoration() {
throw msg("enableScrollRestoration");
},
navigate() {
throw msg("navigate");
},
fetch() {
throw msg("fetch");
},
revalidate() {
throw msg("revalidate");
},
createHref() {
throw msg("createHref");
},
getFetcher() {
return IDLE_FETCHER;
},
deleteFetcher() {
throw msg("deleteFetcher");
},
dispose() {
throw msg("dispose");
},
_internalFetchControllers: new Map(),
_internalActiveDeferreds: new Map(),
};
}