Specify the `component` dynamically, based on route context
hii
i have a use case in which i need to decide what component to render after fetching the data for the route.
how do you think it should be implemented?
currently, i'm thinking on using a code-based splitting approach. something like
but using it doesn't look right for the file-based routes project 🤔
17 Replies
optimistic-gold•2mo ago
If its based on the loaderData, then you can rely on Suspense to fetch the component.
The only issue I see if that, your component would be getting loaded-in only have the loader has completed, so there is an inherent waterfall.
If the component are SUPER different from one another, I'd reckon it may even serve to have a different page altogether.
Using the loaderData to "build" the page is also not a bad idea.
conscious-sapphire•2mo ago
If you want to make preloading possible, you can probably call the imports in the loader
correct-apricotOP•2mo ago
thanks for your response!
i think i might need to provide a bit more context
my goal is to have different types of content on the same URL pattern.
for example,
Content1.tsx
or Content2.tsx
could display /posts/$postId
, depending on the backend response in the loader
but here it gets a bit more difficult: i would like the page to properly SSR* without streaming, which is where React.lazy
would not work (because renderToString
doesn't support Suspense).
my intuition suggests that i might need to have separate routes for Content1.tsx
and Content2.tsx
, and somehow silently redirect (without changing the URL) to them from the data fetching route while passing the state to the context.
do you think that's possible?
*for SSR i'm considering migrating to TanStack Startwise-white•2mo ago
why split the route component then at all?
correct-apricotOP•2mo ago
because Content1.tsx and Content2.tsx have dependencies that are not related. splitting them allows for a smaller bundle size for that route
wise-white•2mo ago
but that's only relevant on the client then?
just trying to understand the big picture here
correct-apricotOP•2mo ago
tbh i'm not sure sure what you're referring to... the server needs to know which chunks to include for that page in the <head> of the SSR response, i guess
wise-white•2mo ago
that's only for preloading
in any case, if you do manual splitting, server wont know about that and thus cannot inject the modulepreloads appropriately
correct-apricotOP•2mo ago
what about stylesheets?
wise-white•2mo ago
wont work with manual splitting either
with automatic splitting, yes
how could the server possibly know which things you render before you render them
correct-apricotOP•2mo ago
need to have separate routes and somehow silently redirect (without changing the URL) to themi guess if this was possible it could work
wise-white•2mo ago
thats not possible
redirecting without url change
you could manually link the styles in the head function based on loader data
but this requires using importing the styles using ?url
correct-apricotOP•2mo ago
that's unfortunate :(
i'm currently using Vike, and there's a method
render(url)
, which allows you to render another route, while preserving the current URL:
https://vike.dev/render
however, it's still not perfect since there's no way to pass data to the next route :D
anyway, thanks for the help!wise-white•2mo ago
where does this requirement come from btw?
to not bind layout to the URL
correct-apricotOP•2mo ago
i'm building something like a file page on Github.
there are multiple ways of displaying a file: highlighted code, 3D model, CSV table, diagrams, etc.
i imagine all of that as different layouts, with their own dependencies
GitHub Docs
Working with non-code files - GitHub Docs
GitHub supports rendering and diffing in a number of non-code file formats.
wise-white•2mo ago
and does GitHub deliver different stylesheets and js bundles depending on the file type?
correct-apricotOP•2mo ago
idk
i haven't really checked because it's not like Github is known for having a snappy frontend experience lol
i only know that they load a 3D model viewer as an iframe