Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(remix-server-runtime): Performance improvements for large apps #4748

Merged
merged 6 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
- dhargitai
- dhmacs
- dima-takoy
- dmarkow
- DNLHC
- dogukanakkaya
- dokeet
Expand Down
42 changes: 33 additions & 9 deletions packages/remix-server-runtime/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,50 @@ export interface ServerRoute extends Route {
module: ServerRouteModule;
}

function groupRoutesByParentId(manifest: ServerRouteManifest) {
let routes: Record<string, Omit<ServerRoute, "children">[]> = {};

Object.values(manifest).forEach((route) => {
let parentId = route.parentId || "";
if (!routes[parentId]) {
routes[parentId] = [];
}
routes[parentId].push(route);
});

return routes;
}

export function createRoutes(
manifest: ServerRouteManifest,
parentId?: string
parentId?: string,
routesByParentId?: Record<string, Omit<ServerRoute, "children">[]>
): ServerRoute[] {
return Object.entries(manifest)
.filter(([, route]) => route.parentId === parentId)
.map(([id, route]) => ({
// Create a map of routes by parentId to use recursively instead of
// repeatedly filtering the manifest.
routesByParentId ||= groupRoutesByParentId(manifest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@machour - would the logical or assignment (|=) be transpiled correctly for older browsers in Remix's build process?

I recall recently that null coalescing operator was removed:
#4561

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, this code will only run on the server, so it shouldn't cause any problem with the browser

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ran into an issue on node with this on node 14 that should be fixed since we changed the target in our babelrc, but that might be worth confirming.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing the ||= in df6f364 and switched it to use a default param value


return (routesByParentId[parentId || ""] || [])
.map((route) => ({
...route,
children: createRoutes(manifest, id),
children: createRoutes(manifest, route.id, routesByParentId),
}));
}


// Convert the Remix ServerManifest into DataRouteObject's for use with
// createStaticHandler
export function createStaticHandlerDataRoutes(
manifest: ServerRouteManifest,
loadContext: AppLoadContext,
parentId?: string
parentId?: string,
routesByParentId?: Record<string, Omit<ServerRoute, "children">[]>
): AgnosticDataRouteObject[] {
return Object.values(manifest)
.filter((route) => route.parentId === parentId)
// Create a map of routes by parentId to use recursively instead of
// repeatedly filtering the manifest.
routesByParentId ||= groupRoutesByParentId(manifest);

return (routesByParentId[parentId || ""] || [])
.map((route) => {
let commonRoute = {
// Always include root due to default boundaries
Expand Down Expand Up @@ -103,7 +126,8 @@ export function createStaticHandlerDataRoutes(
children: createStaticHandlerDataRoutes(
manifest,
loadContext,
route.id
route.id,
routesByParentId
),
...commonRoute,
};
Expand Down