Skip to content

Commit 5a363e5

Browse files
authored
Clean up types within consolidated package (#12177)
1 parent 31a9ad8 commit 5a363e5

File tree

22 files changed

+175
-365
lines changed

22 files changed

+175
-365
lines changed

.changeset/sour-cycles-lie.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@react-router/dev": major
3+
"react-router": major
4+
---
5+
6+
- Consolidate types previously duplicated across `@remix-run/router`, `@remix-run/server-runtime`, and `@remix-run/react` now that they all live in `react-router`
7+
- Examples: `LoaderFunction`, `LoaderFunctionArgs`, `ActionFunction`, `ActionFunctionArgs`, `DataFunctionArgs`, `RouteManifest`, `LinksFunction`, `Route`, `EntryRoute`
8+
- The `RouteManifest` type used by the "remix" code is now slightly stricter because it is using the former `@remix-run/router` `RouteManifest`
9+
- `Record<string, Route> -> Record<string, Route | undefined>`
10+
- Removed `AppData` type in favor of inlining `unknown` in the few locations it was used
11+
- Removed `ServerRuntimeMeta*` types in favor of the `Meta*` types they were duplicated from

packages/react-router-dev/vite/plugin.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,11 +2023,13 @@ function groupRoutesByParentId(manifest: ServerBuild["routes"]) {
20232023
let routes: Record<string, Omit<ServerRoute, "children">[]> = {};
20242024

20252025
Object.values(manifest).forEach((route) => {
2026-
let parentId = route.parentId || "";
2027-
if (!routes[parentId]) {
2028-
routes[parentId] = [];
2026+
if (route) {
2027+
let parentId = route.parentId || "";
2028+
if (!routes[parentId]) {
2029+
routes[parentId] = [];
2030+
}
2031+
routes[parentId].push(route);
20292032
}
2030-
routes[parentId].push(route);
20312033
});
20322034

20332035
return routes;

packages/react-router-dev/vite/styles.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,16 @@ const findDeps = async (
171171
};
172172

173173
const groupRoutesByParentId = (manifest: ServerRouteManifest) => {
174-
let routes: Record<string, Omit<ServerRoute, "children">[]> = {};
174+
let routes: Record<string, NonNullable<ServerRoute>[]> = {};
175175

176176
Object.values(manifest).forEach((route) => {
177-
let parentId = route.parentId || "";
178-
if (!routes[parentId]) {
179-
routes[parentId] = [];
177+
if (route) {
178+
let parentId = route.parentId || "";
179+
if (!routes[parentId]) {
180+
routes[parentId] = [];
181+
}
182+
routes[parentId].push(route);
180183
}
181-
routes[parentId].push(route);
182184
});
183185

184186
return routes;
@@ -189,11 +191,8 @@ const groupRoutesByParentId = (manifest: ServerRouteManifest) => {
189191
const createRoutes = (
190192
manifest: ServerRouteManifest,
191193
parentId: string = "",
192-
routesByParentId: Record<
193-
string,
194-
Omit<ServerRoute, "children">[]
195-
> = groupRoutesByParentId(manifest)
196-
): ServerRoute[] => {
194+
routesByParentId = groupRoutesByParentId(manifest)
195+
): NonNullable<ServerRoute>[] => {
197196
return (routesByParentId[parentId] || []).map((route) => ({
198197
...route,
199198
children: createRoutes(manifest, route.id, routesByParentId),

packages/react-router/index.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,6 @@ export { createRoutesStub } from "./lib/dom/ssr/routes-test-stub";
213213
// Expose old @remix-run/server-runtime API, minus duplicate APIs
214214
export { createCookie, isCookie } from "./lib/server-runtime/cookies";
215215

216-
// TODO: (v7) Clean up code paths for these exports
217-
// export {
218-
// json,
219-
// redirect,
220-
// redirectDocument,
221-
// } from "./lib/server-runtime/responses";
222216
export { createRequestHandler } from "./lib/server-runtime/server";
223217
export {
224218
createSession,
@@ -258,16 +252,6 @@ export type {
258252
} from "./lib/router/links";
259253

260254
export type {
261-
// TODO: (v7) Clean up code paths for these exports
262-
// ActionFunction,
263-
// ActionFunctionArgs,
264-
// LinksFunction,
265-
// LoaderFunction,
266-
// LoaderFunctionArgs,
267-
// ServerRuntimeMetaArgs,
268-
// ServerRuntimeMetaDescriptor,
269-
// ServerRuntimeMetaFunction,
270-
DataFunctionArgs,
271255
HeadersArgs,
272256
HeadersFunction,
273257
} from "./lib/server-runtime/routeModules";

packages/react-router/lib/dom-export/hydrated-router.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function createHydratedRouter(): DataRouter {
133133
// * or doesn't have a server loader and we have no data to render
134134
if (
135135
route &&
136+
manifestRoute &&
136137
shouldHydrateRouteLoader(
137138
manifestRoute,
138139
route,

packages/react-router/lib/dom/ssr/components.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ function PrefetchPageLinksImpl({
366366
let routesParams = new Set<string>();
367367
let foundOptOutRoute = false;
368368
nextMatches.forEach((m) => {
369-
if (!manifest.routes[m.route.id].hasLoader) {
369+
let manifestRoute = manifest.routes[m.route.id];
370+
if (!manifestRoute || !manifestRoute.hasLoader) {
370371
return;
371372
}
372373

@@ -376,7 +377,7 @@ function PrefetchPageLinksImpl({
376377
routeModules[m.route.id]?.shouldRevalidate
377378
) {
378379
foundOptOutRoute = true;
379-
} else if (manifest.routes[m.route.id].hasClientLoader) {
380+
} else if (manifestRoute.hasClientLoader) {
380381
foundOptOutRoute = true;
381382
} else {
382383
routesParams.add(m.route.id);
@@ -682,7 +683,7 @@ ${matches
682683
.map(
683684
(match, index) =>
684685
`import * as route${index} from ${JSON.stringify(
685-
manifest.routes[match.route.id].module
686+
manifest.routes[match.route.id]!.module
686687
)};`
687688
)
688689
.join("\n")}
@@ -728,7 +729,7 @@ import(${JSON.stringify(manifest.entry.module)});`;
728729
let routePreloads = matches
729730
.map((match) => {
730731
let route = manifest.routes[match.route.id];
731-
return (route.imports || []).concat([route.module]);
732+
return route ? (route.imports || []).concat([route.module]) : [];
732733
})
733734
.flat(1);
734735

packages/react-router/lib/dom/ssr/data.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import "../global";
22

3-
/**
4-
* Data for a route that was returned from a `loader()`.
5-
*/
6-
export type AppData = unknown;
7-
83
export async function createRequestInit(
94
request: Request
105
): Promise<RequestInit> {

packages/react-router/lib/dom/ssr/entry.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import type { StaticHandlerContext } from "../../router/router";
22

3-
import type { RouteManifest, EntryRoute } from "./routes";
3+
import type { EntryRoute } from "./routes";
44
import type { RouteModules } from "./routeModules";
5-
6-
// Object passed to RemixContext.Provider
5+
import type { RouteManifest } from "../../router/utils";
76

87
type SerializedError = {
98
message: string;
109
stack?: string;
1110
};
11+
12+
// Object passed to RemixContext.Provider
1213
export interface FrameworkContextObject {
1314
manifest: AssetsManifest;
1415
routeModules: RouteModules;

packages/react-router/lib/dom/ssr/fog-of-war.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from "react";
22
import type { PatchRoutesOnNavigationFunction } from "../../context";
33
import type { Router as DataRouter } from "../../router/router";
4+
import type { RouteManifest } from "../../router/utils";
45
import { matchRoutes } from "../../router/utils";
56
import type { AssetsManifest } from "./entry";
67
import type { RouteModules } from "./routeModules";
8+
import type { EntryRoute } from "./routes";
79
import { createClientRoutes } from "./routes";
810

911
declare global {
@@ -233,13 +235,12 @@ export async function fetchAndApplyManifestPatches(
233235

234236
// Patch routes we don't know about yet into the manifest
235237
let knownRoutes = new Set(Object.keys(manifest.routes));
236-
let patches: AssetsManifest["routes"] = Object.values(serverPatches).reduce(
237-
(acc, route) =>
238-
!knownRoutes.has(route.id)
239-
? Object.assign(acc, { [route.id]: route })
240-
: acc,
241-
{}
242-
);
238+
let patches = Object.values(serverPatches).reduce((acc, route) => {
239+
if (route && !knownRoutes.has(route.id)) {
240+
acc[route.id] = route;
241+
}
242+
return acc;
243+
}, {} as RouteManifest<EntryRoute>);
243244
Object.assign(manifest.routes, patches);
244245

245246
// Track discovered paths so we don't have to fetch them again
@@ -249,7 +250,7 @@ export async function fetchAndApplyManifestPatches(
249250
// in their new children
250251
let parentIds = new Set<string | undefined>();
251252
Object.values(patches).forEach((patch) => {
252-
if (!patch.parentId || !patches[patch.parentId]) {
253+
if (patch && (!patch.parentId || !patches[patch.parentId])) {
253254
parentIds.add(patch.parentId);
254255
}
255256
});

packages/react-router/lib/dom/ssr/links.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function getKeyedLinksForMatches(
2626
let module = routeModules[match.route.id];
2727
let route = manifest.routes[match.route.id];
2828
return [
29-
route.css ? route.css.map((href) => ({ rel: "stylesheet", href })) : [],
29+
route && route.css
30+
? route.css.map((href) => ({ rel: "stylesheet", href }))
31+
: [],
3032
module?.links?.() || [],
3133
];
3234
})
@@ -138,11 +140,12 @@ export async function getKeyedPrefetchLinks(
138140
): Promise<KeyedHtmlLinkDescriptor[]> {
139141
let links = await Promise.all(
140142
matches.map(async (match) => {
141-
let mod = await loadRouteModule(
142-
manifest.routes[match.route.id],
143-
routeModules
144-
);
145-
return mod.links ? mod.links() : [];
143+
let route = manifest.routes[match.route.id];
144+
if (route) {
145+
let mod = await loadRouteModule(route, routeModules);
146+
return mod.links ? mod.links() : [];
147+
}
148+
return [];
146149
})
147150
);
148151

@@ -197,7 +200,7 @@ export function getNewMatchesForLinks(
197200
if (mode === "data") {
198201
return nextMatches.filter((match, index) => {
199202
let manifestRoute = manifest.routes[match.route.id];
200-
if (!manifestRoute.hasLoader) {
203+
if (!manifestRoute || !manifestRoute.hasLoader) {
201204
return false;
202205
}
203206

@@ -235,6 +238,7 @@ export function getModuleLinkHrefs(
235238
matches
236239
.map((match) => {
237240
let route = manifestPatch.routes[match.route.id];
241+
if (!route) return [];
238242
let hrefs = [route.module];
239243
if (route.imports) {
240244
hrefs = hrefs.concat(route.imports);
@@ -256,12 +260,11 @@ function getCurrentPageModulePreloadHrefs(
256260
matches
257261
.map((match) => {
258262
let route = manifest.routes[match.route.id];
263+
if (!route) return [];
259264
let hrefs = [route.module];
260-
261265
if (route.imports) {
262266
hrefs = hrefs.concat(route.imports);
263267
}
264-
265268
return hrefs;
266269
})
267270
.flat(1)

0 commit comments

Comments
 (0)