Closed stevenwoodson closed 7 months ago
Latest commit: 7118bf66bf77a117acfff6e961304af15ffe9ef8
The changes in this PR will be included in the next version bump.
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
Yes, you are exactly right!
I had considered de-duplicating against the normally-added scripts and styles, but not against the ones added by other dynamic imports.
Thanks for taking this on! I can write the tests and WeakMap
tracking later this week if you don't beat me to it.
That'd be fantastic, I'll admit I've not had cause to use WeakMap
before so I certainly defer to the experts on that part.
I'm glad the overall logic is sound, thanks for offering to take it that next step further! Please do @ me if there's any more I can do otherwise.
Seems my edit worked a bit too well, after some further testing I realized that the CSS/JS additions only happened once ever so the first page that used that component got the CSS and JS while the rest didn't.
I just committed a change that also keeps track of the current pathname and resets the processed array when a new path is encountered. I'm unsure if this is the ideal way to do this, but it is working in my additional tests.
Hey @lilnasy, Just pulled the latest edits and can confirm this is still working as expected.
Thanks for touching it up and adding tests, I agree this looks ready to merge! :shipit:
@stevenwoodson Awesome, incredible work!
Hi @lilnasy!
Background
I've been working through a proof of concept Astro implementation that pulls content from a CMS (in my case Sanity) and went down the rabbit hole of noticing that all components styling and JS dependencies get loaded on every page regardless of whether they're used or not.
I came across this roadmap discussion that mentioned your dynamic-import integration and was very excited to give it a try.
From my initial testing, it does indeed isolate dependencies on a per-page basis Though as I dug in a bit further with more complex pages, I noticed that if a component was used on the page more than once then the style and JavaScript dependencies are added to the page that same number of times. It doesn't affect the page rendering but does add a considerable amount to the pageload/
This Proof of Concept
Rather than just opening an issue, I wanted to dive in further and understand what's going on first, and then offer a suggestion. This is a bit of a crude example, and I'm sure it can be refined, but it gets the job done.
This addition keeps track of the components that have been processed in
processed
, if it was already then the styles, links, and scripts are suppressed so they don't get added again.In my tests, the page still renders fine and I don't see duplicate styles or JS in using the standard dev or build commands.