Skip to content

Commit

Permalink
Fix FOUC on subsequent client-side navigations to route.lazy routes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Oct 5, 2023
1 parent 9623b4e commit 20baca1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
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

0 comments on commit 20baca1

Please sign in to comment.