-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathbrowser.tsx
214 lines (197 loc) · 6.7 KB
/
browser.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
import type { HydrationState, Router } from "@remix-run/router";
import type { ReactElement } from "react";
import * as React from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { RemixContext } from "./components";
import type { EntryContext, FutureConfig } from "./entry";
import {
RemixErrorBoundary,
RemixRootDefaultErrorBoundary,
} from "./errorBoundaries";
import { deserializeErrors } from "./errors";
import type { RouteModules } from "./routeModules";
import {
createClientRoutes,
createClientRoutesWithHMRRevalidationOptOut,
} from "./routes";
/* eslint-disable prefer-let/prefer-let */
declare global {
var __remixContext: {
state: HydrationState;
future: FutureConfig;
// The number of active deferred keys rendered on the server
a?: number;
dev?: {
port?: number;
hmrRuntime?: string;
};
};
var __remixRouteModules: RouteModules;
var __remixManifest: EntryContext["manifest"];
var __remixRevalidation: number | undefined;
var $RefreshRuntime$: {
performReactRefresh: () => void;
};
}
/* eslint-enable prefer-let/prefer-let */
export interface RemixBrowserProps {}
declare global {
interface ImportMeta {
hot: any;
}
}
let router: Router;
let hmrAbortController: AbortController | undefined;
if (import.meta && import.meta.hot) {
import.meta.hot.accept(
"remix:manifest",
async ({
assetsManifest,
needsRevalidation,
}: {
assetsManifest: EntryContext["manifest"];
needsRevalidation: Set<string>;
}) => {
let routeIds = [
...new Set(
router.state.matches
.map((m) => m.route.id)
.concat(Object.keys(window.__remixRouteModules))
),
];
if (hmrAbortController) {
hmrAbortController.abort();
}
hmrAbortController = new AbortController();
let signal = hmrAbortController.signal;
// Load new route modules that we've seen.
let newRouteModules = Object.assign(
{},
window.__remixRouteModules,
Object.fromEntries(
(
await Promise.all(
routeIds.map(async (id) => {
if (!assetsManifest.routes[id]) {
return null;
}
let imported = await import(
assetsManifest.routes[id].module +
`?t=${assetsManifest.hmr?.timestamp}`
);
return [
id,
{
...imported,
// react-refresh takes care of updating these in-place,
// if we don't preserve existing values we'll loose state.
default: imported.default
? window.__remixRouteModules[id]?.default ??
imported.default
: imported.default,
CatchBoundary: imported.CatchBoundary
? window.__remixRouteModules[id]?.CatchBoundary ??
imported.CatchBoundary
: imported.CatchBoundary,
ErrorBoundary: imported.ErrorBoundary
? window.__remixRouteModules[id]?.ErrorBoundary ??
imported.ErrorBoundary
: imported.ErrorBoundary,
},
];
})
)
).filter(Boolean) as [string, RouteModules[string]][]
)
);
Object.assign(window.__remixRouteModules, newRouteModules);
// Create new routes
let routes = createClientRoutesWithHMRRevalidationOptOut(
needsRevalidation,
assetsManifest.routes,
window.__remixRouteModules,
window.__remixContext.future
);
// This is temporary API and will be more granular before release
router._internalSetRoutes(routes);
// Wait for router to be idle before updating the manifest and route modules
// and triggering a react-refresh
let unsub = router.subscribe((state) => {
if (state.revalidation === "idle") {
unsub();
// Abort if a new update comes in while we're waiting for the
// router to be idle.
if (signal.aborted) return;
// Ensure RouterProvider setState has flushed before re-rendering
setTimeout(() => {
Object.assign(window.__remixManifest, assetsManifest);
window.$RefreshRuntime$.performReactRefresh();
}, 1);
}
});
window.__remixRevalidation = (window.__remixRevalidation || 0) + 1;
router.revalidate();
}
);
}
/**
* The entry point for a Remix app when it is rendered in the browser (in
* `app/entry.client.js`). This component is used by React to hydrate the HTML
* that was received from the server.
*/
export function RemixBrowser(_props: RemixBrowserProps): ReactElement {
if (!router) {
let routes = createClientRoutes(
window.__remixManifest.routes,
window.__remixRouteModules,
window.__remixContext.future
);
let hydrationData = window.__remixContext.state;
if (hydrationData && hydrationData.errors) {
hydrationData = {
...hydrationData,
errors: deserializeErrors(hydrationData.errors),
};
}
router = createBrowserRouter(routes, {
hydrationData,
future: {
// Pass through the Remix future flag to avoid a v1 breaking change in
// useNavigation() - users can control the casing via the flag in v1.
// useFetcher still always uppercases in the back-compat layer in v1.
// In v2 we can just always pass true here and remove the back-compat
// layer
v7_normalizeFormMethod:
window.__remixContext.future.v2_normalizeFormMethod,
},
});
}
let [location, setLocation] = React.useState(router.state.location);
React.useLayoutEffect(() => {
return router.subscribe((newState) => {
if (newState.location !== location) {
setLocation(newState.location);
}
});
}, [location]);
// We need to include a wrapper RemixErrorBoundary here in case the root error
// boundary also throws and we need to bubble up outside of the router entirely.
// Then we need a stateful location here so the user can back-button navigate
// out of there
return (
<RemixContext.Provider
value={{
manifest: window.__remixManifest,
routeModules: window.__remixRouteModules,
future: window.__remixContext.future,
}}
>
<RemixErrorBoundary
location={location}
component={RemixRootDefaultErrorBoundary}
>
<RouterProvider router={router} fallbackElement={null} />
</RemixErrorBoundary>
</RemixContext.Provider>
);
}