RFC: Use modulepreload to speculatively prefetch #193
Labels
[STAGE-2] incomplete implementation
Remove this label when implementation is complete
[STAGE-2] not fully covered by tests yet
Remove this label when tests are verified to cover the implementation
[STAGE-2] unresolved discussions left
Remove this label when all critical discussions are resolved on the issue
[STAGE-3] docs changes not added yet
Remove this label when the necessary documentation for the feature / change is added
[STAGE-3] missing 2 reviews for RFC PRs
Remove this label when at least 2 core team members reviewed and approved the RFC implementation
Discussed in #182
Originally posted by GrandSchtroumpf October 14, 2024
What is it about?
Use to prefetch modules
What's the motivation for this proposal?
Problems you are trying to solve:
We cannot use the Cache API to prefetch module in devmode which leads to poor performance
Goals you are trying to achieve:
Separate the speculative prefetch process from the caching process.
Any other context or information you want to share:
modulepreload
has been discussed and discarded in the early day of qwik because Firefox didn't support it. This is not the case anymore.prefetchStrategy
->linkRel: 'modulepreload'
, but it only prefetch initial modules, those known at build time.modulepreload
could increase FCP/LCP. It was decided to change Qwik doc prefetch strategy to accumulate real user data to see if this is the case.If
modulepreload
affect negatively FCP/LCP it's still possible to useprefetchStrategy
->linkInsert: 'js-append'
instead of'html-append'
to delay to initial prefetch.Proposed Solution / Feature
What do you propose?
Separate Speculative Prefetch from Caching
Speculative Prefetch (should work in dev mode) :
Use
<link rel="modulepreload" />
to prefetch the modules:modulepreload
in the head (similar tolinkInsert: 'html-append'
prefetchStrategy)DOMContentLoaded
to start listening onqprefetch
eventqprefetch
event, append new<link rel="modulepreload" />
tags with the bundle href. If modulepreload is not support just do a regularfetch
for the Service Worker to hook it.Caching (should work only in prod mode) :
DOMContentLoaded
register service worker, send all existingmodulepreload
hrefs to the service worker to cache.fetch
cache the response if it's a qwik moduleCode examples
This is a working code snippet. To use it
entry.ssr
setprefetchStrategy
:root.tsx
remove<PrefetchServiceWorker />
&<PrefetchGraph />
and adduseModulePrelaod()
(see code snippet)sw.js
in thepublic
folder (see code snippet)useModulePreload.tsx
sw.js
Links / References
MDN: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/modulepreload
Experiment: https://github.com/thejackshelton/sw-playground
app using
useModulePreload
: https://qwik-playground.vercel.app/ (github: https://github.com/GrandSchtroumpf/qwik-hueeye)The text was updated successfully, but these errors were encountered: