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 FOUC on subsequent client-side navigations to route.lazy routes #7576

Merged
merged 2 commits into from
Oct 5, 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-fouc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Fix FOUC on subsequent client-side navigations to route.lazy routes
46 changes: 32 additions & 14 deletions packages/remix-react/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,40 @@ export function createClientRoutes(
id: route.id,
index: route.index,
path: route.path,
loader({ request }) {
if (!route.hasLoader) return null;
return fetchServerHandler(request, route);
},
action({ request }) {
if (!route.hasAction) {
let msg =
`Route "${route.id}" does not have an action, but you are trying ` +
`to submit to it. To fix this, please add an \`action\` function to the route`;
console.error(msg);
return Promise.reject(
new ErrorResponse(405, "Method Not Allowed", new Error(msg), true)
);
async loader({ request }) {
// Only prefetch links if we've been loaded into the cache, route.lazy
// will handle initial loads
let routeModulePromise = routeModulesCache[route.id]
? prefetchStyleLinks(routeModulesCache[route.id])
: Promise.resolve();
try {
if (!route.hasLoader) return null;
return fetchServerHandler(request, route);
} finally {
await routeModulePromise;
}
},
async action({ request }) {
// Only prefetch links if we've been loaded into the cache, route.lazy
// will handle initial loads
let routeModulePromise = routeModulesCache[route.id]
? prefetchStyleLinks(routeModulesCache[route.id])
: Promise.resolve();
try {
if (!route.hasAction) {
let msg =
`Route "${route.id}" does not have an action, but you are trying ` +
`to submit to it. To fix this, please add an \`action\` function to the route`;
console.error(msg);
return Promise.reject(
new ErrorResponse(405, "Method Not Allowed", new Error(msg), true)
);
}

return fetchServerHandler(request, route);
return fetchServerHandler(request, route);
} finally {
await routeModulePromise;
}
},
...(routeModule
? // Use critical path modules directly
Expand Down