-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add dynamic imports in scripts tip #118
Conversation
Deploying astro-tips with
|
Latest commit: |
996362d
|
Status: | ✅ Deploy successful! |
Preview URL: | https://63c7f7c1.astro-tips-blf.pages.dev |
Branch Preview URL: | https://dynamic-imports.astro-tips-blf.pages.dev |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 7: ...this is great for a balance between performance and...
Line 19: ...allowing for manual optimization...
Line 64: ...Ensuring that modules somethings missing here
critical for snappy INP....
Line 75: ...only when it is clicked you that could...
|
||
Dynamic imports also allow you to control the sequence of network requests. Instead of fetching all modules in parallel, which is the default behavior when using multiple static imports or separate module scripts, dynamic imports enable you to ensure that one module is downloaded first before initiating the fetch for the next module. This can be particularly useful when you have modules that are critical for immediate user interaction and others that are less critical or if you are supporting users with very low bandwidth. | ||
|
||
## Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the examples below can be improved by 1:
- Using real modules and using them (ie. must be basic to avoid distracting, you can use EC line markers)
- Explain why using 1 example or another is better for those modules
Overall being more practical I guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any ideas for real world modules? I used confetti for the example repo but wasn't creative enough to come up with real real world examples lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean confetti is better than "..."!
Nice work @OliverSpeir! Some quick thoughts:
Hope that's a bit helpful, but honestly it's already looking great! |
@delucis and thank you for fact checking I lowkey forgot about the consequences of astro bringing everything into a single file |
Should probably work in here how this can affect total blocking time ( with the client visible or rare button click examples ) edit: actually I've come to conclusion it's better to just explain this very simply and let the users draw any conclusions |
Ok upon further inspection I think astro hoists all into on main script but it does in create separate .js files for all code that is imported in those script tags (it dedupes them obviously). So it is accurate to be able to granularly control network requests, because the main hoisted .js file will by default have static imports which are all fetched in parallel |
@florian-lefebvre I've changed the imports from "..." to the fake names, I went to add the confetti in there but I felt it detracted from the clarity of the tip because having it called criticalModule and lessCriticalModule I think makes it easier to understand Is this ok with you? I know it's still not ideal <script>
import criticalModule from 'criticalModule';
criticalModule();
const lessCriticalModule = await import('lessCriticalModule');
lessCriticalModule();
</script> |
NWTWWHB! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, we're almost there I think! Can you clean up the spacing a little bit? It's inconsistent across the tip
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Closes #93
Ready for review, this is a really information heavy tip so any writing style reviews would be helpful.
Basically... does this make sense? does it sufficiently explain things?