Use modulepreload to speculatively prefetch #182
Closed
GrandSchtroumpf
started this conversation in
Proposals For Qwik
Replies: 2 comments
-
Beta Was this translation helpful? Give feedback.
0 replies
-
thank you @GrandSchtroumpf ! as work has already begun on this before this proposal, I'll create the proper RFC issue (#193) and close this proposal We're going to work on getting the buffering story right once and for all in the upcoming weeks, we would love to collaborate on this like the last time. Thanks again! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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)Beta Was this translation helpful? Give feedback.
All reactions