Code split sub-routes together
Wondering if it's possible to control how lazy file-based routes are split out instead of having each route be in its own chunk. Potential desired behavior would be that everything below a certain path (e.g. /users would include /users, /users/1, /users/new, etc) would all be included in the same chunk so that once you "entered" that section of the site once you'd already have the rest of the code loaded.
12 Replies
fascinating-indigo•17mo ago
this is currently not possible I think.
@Tanner Linsley can we make this configurable with automatic code splitting?
sensitive-blue•17mo ago
Anything is possible with enough effort
This would mean some pretty complex additions to the vite plugin, so I'm weary to promise anything yet.
I would be happy to see a PR or issue that explores the implications of this further, but not at the cost of focusing on other things for core maintainers.
fascinating-indigo•17mo ago
I was thinking of something like this:
https://v3.vitejs.dev/guide/build.html#chunking-strategy
https://rollupjs.org/configuration-options/#output-manualchunks
sensitive-blue•17mo ago
Yep, 100%
I would really love it if we could defer to vite for control on this
Technically, we're not creating virtual modules, we're just importing the same file again with a search parameter that allows us to strip it down
So I wonder if it would be possible for users to write their own manual chunking settings based on that
?tsr-split
paramfascinating-indigo•17mo ago
I did a quick manual test in the
basic-file-based-codesplitting
example:
baseline:
with the following vite config:
so you can already group routes into chunksdeep-jadeOP•17mo ago
Interesting, I'll give that a try with my setup.
fascinating-indigo•17mo ago
probably there is some configuration mistake still since
posts.grouped.lazy.js-o6hp8VLJ.js
is way bigger than it should be
this looks a bit better:
deep-jadeOP•17mo ago
I'm getting similar results on my end; however, when I load the actual site the split files are being loaded immediately. I'll continue messing with the configuration to see what needs to be included/excluded from the chunk to make it work.
I'm not super familiar with how the chunking is handled, but it's looking like for some reason some common code that my components (the ones referenced by the lazy routes) are referencing is getting included in their chunk which is causing it to be loaded with the initial set of files. If I separately chunk the lazy routes on their own, that piece works fine (although that chunk is super tiny and isn't really where I'm looking to get the benefit - the components that are referenced is obviously the meatier part of the file size).
Okay, I think I was able to get it going. I had to force my other code into a specific chunk instead of returning nothing. A bit later I'll make sure that it works with experimental chunking and I should be good to go. Thanks for the help!
fascinating-indigo•17mo ago
would be cool if you could post your final configuration for future reference
deep-jadeOP•17mo ago
Yeah, absolutely.
Experimental code splitting works as well, just needed to change my matcher. Here's what I ended up with (I'm also doing some splitting of larger vendor libraries). One remaining improvement on my end is to configure this at the top of the file instead of manually building if-checks.
deep-jade•15mo ago
I just looked into this and I don't see
id
containing any ?tsr-split
, should I look for it including .lazy.
instead?
I do see it being used when using experimental.enableCodeSplitting: true
But that's giving me different compile errors (TSR v1.35.6
):
Edit: It seems it doesn't like me using a default import, but changing it to import { default as X }
worked?
What's a good forum to report the vite router split bugs? I couldn't find any documentation on the experimental code splitting either 😊 @Tanner Linsley
@somnolent2703 Did you end up using experimental code splitting? I read in another comment that when doing so one shouldn't use .lazy.
in the file names. Did you confirm your chunks load correctly as needed?fascinating-indigo•15mo ago
please report any router bugs as a GitHub issue
yeah , either use lazy OR the automatic splitting