v7: can I conditionally render the route's component? #14005
Replies: 2 comments
-
I also tried using a middleware, but I'm left with the same problem. If I define a |
Beta Was this translation helpful? Give feedback.
-
I think the best approach is to use the entry.server.ts escape-hatch. I check the upstream. If the response isn't a 404, I return it. If it is, I pass through to the react-router application. // app/entry.server.tsx
export default async function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
routerContext: EntryContext,
_loadContext: AppLoadContext,
// If you have middleware enabled:
// loadContext: unstable_RouterContextProvider
) {
const upstreamResponse = await proxyRequest(request)
if (upstreamResponse && upstreamResponse.status !== 404) {
return upstreamResponse)
}
return new Promise((resolve, reject) => {
// default handleRequest logic
}
} That almost works. In dev mode, every proxied request results in the following log:
In production, it's similar:
The requests do go to my If I add a fallback route, the log lines go away: // app/routes.ts
route('*', 'routes/fallback.tsx')
// app/routes/fallback.tsx
function notFound() {
throw new Response('Not Found', {
status: 404,
statusText: 'Not Found',
});
}
export const action = notFound;
export const loader = notFound;
export default function Fallback() {
return <>Not Found</>;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a
root
route with the stockErrorBoundary
from the guides.I have a fallback route:
I want to use my fallback route to proxy an upstream HTTP server. That's relatively straightforward:
But this means that I'm displaying the upstream server's 404 page instead of my app's.
I tried throwing error responses:
But that made no difference.
If I add a default export component, then 404s render it in my app's layout.
But now every request that goes to my
FallbackRoute
renders that Not Found component.Is there any way for my loader to say this is a response you should return directly, but this is data to be passed to the component to be rendered in the app?
Beta Was this translation helpful? Give feedback.
All reactions