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

Fix resource routes being loaded through route.lazy #7498

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/route-lazy-resource-route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Fix resource routes being loaded through `route.lazy`
2 changes: 2 additions & 0 deletions packages/remix-dev/compiler/js/plugins/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { Context } from "../../context";

type Route = RemixConfig["routes"][string];

// If you change this, make sure you update loadRouteModuleWithBlockingLinks in
// remix-react/routes.ts
const browserSafeRouteExports: { [name: string]: boolean } = {
ErrorBoundary: true,
default: true,
Expand Down
19 changes: 16 additions & 3 deletions packages/remix-react/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,23 @@ async function loadRouteModuleWithBlockingLinks(
) {
let routeModule = await loadRouteModule(route, routeModules);
await prefetchStyleLinks(routeModule);

// Resource routes are built with an empty object as the default export -
// ignore those when setting the Component
let defaultExportIsEmptyObject =
typeof routeModule.default === "object" &&
Object.keys(routeModule.default || {}).length === 0;

// Include all `browserSafeRouteExports` fields
return {
...routeModule,
default: undefined,
Component: routeModule.default,
...(routeModule.default != null && !defaultExportIsEmptyObject
? { Component: routeModule.default }
: {}),
ErrorBoundary: routeModule.ErrorBoundary,
handle: routeModule.handle,
links: routeModule.links,
meta: routeModule.meta,
shouldRevalidate: routeModule.shouldRevalidate,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Manually re-exporting the fields here makes TS happier since it doesn't have to worry about the IndexRoute/NonIndexRoute stuff

};
}

Expand Down