-
-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use runtimeCaching
? Can't cache API resquests
#626
Comments
you need to add the api request to |
workbox needs to create the cache, will not work on first request (cannot create the cache while intercepting the request), try hard page refresh: you will need to switch to custom sw (injectManifest) and warm the cache manually on install event. |
Not sur to understand you 100% ... I've tried a lot of hard refreshs... Still no luck and the call to my api isn't event intercepted ! : Can I do so without some custom code ? With only with vite.config.js... I'm always doing vite build + vite preview to test that out ... Is it mandatory or should it work in developement ? ( with |
try adding |
I had this mode once but seems to not changed anything, But I didn't have the chrome log level "all" ! I found the error then with a log : For some reason workbox won't cache cross origin if the regex doesn't match the full url ... Didn't find it in the docs but in some SO comment I've changed the configuration to : VitePWA({
strategies: 'generateSW',
registerType: 'autoUpdate',
mode: 'development',
workbox: {
globPatterns: ['**/*.{js,jsx,css,html,png,svg,woff2,json}'],
runtimeCaching: [
{
urlPattern: new RegExp(`^${API_URL}\/.*`, 'i'),
handler: 'StaleWhileRevalidate',
method: 'GET',
options: {
cacheName: 'api-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365, // <== 365 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
}, The Thanks anyways @userquin your help was very appreciated ♥ |
you can also use a callback, for example, to check in same origin, check the docs: urlPattern: ({ url, sameOrigin }) => sameOrigin && url.pathname.match(/^\/api\//), |
hello can you resolved? Iam too same question. |
Here is my vite.config.js
I'm just trying to cache the call to my API (
/api/*
)But I've tried a lot of variations of this config (with or without
registerType: autoUpdate
and differents handlers for the runtimeCaching ('CacheFirst' | 'CacheOnly' | 'NetworkFirst' | 'NetworkOnly' | 'StaleWhileRevalidate'
) ... ) And none worksWhat am I doing wrong ? Do I need to run
vite build && vite preview
to see the working service worker ? Or 'just' with the developement version (vite
) it should also work ?Here is the sw.js generated
Linked SO question (with more screens)
The text was updated successfully, but these errors were encountered: