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
Is there a way to use sw without using precache? #302
Comments
You only need the injection point, just assign it to a const variable, dont use it and you are done. About usage of workbox you shoukd ask on workbox repo, just check the links on the docs there are some examples on their docs website |
Hi @userquin, thanks, I'm confused about the "just assign it to a const variable and dont use it" that you said. In which file do I do that, and who is it? I assume I'll still use the same setup of the I tried to put |
// sw.ts => your service worker
const wbManifest = self.__WB_MANIFEST
// use another strategies without using wbManifest
// you don't need to use precacheAndRoute The The error you get comes from Maybe in a future we can just build the custom service worker without using |
Hi @userquin, thanks, not sure if I understood you correctly, but if I leave everything unchanged, and only make the one-line change you suggested, it won't compile: // sw.ts
import { precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
precacheAndRoute(self.__WB_MANIFEST); // does compile
registerRoute(myRoute); into ↓ // sw.ts
import { registerRoute } from 'workbox-routing';
const wbManifest = self.__WB_MANIFEST; // does not compile
registerRoute(myRoute); Searching "wbManifest" in this repo doesn't yield any match. Did you mean another spelling? |
@ZYinMD check this repo, it is building a custom sw for serving dynamic pages: the sw is here https://github.com/userquin/test-pwa-ts-4.5.5/blob/main/src/sw.ts You can check it deployed at netlify here: https://optimistic-cray-38ef22.netlify.app/ |
Hi @userquin, thanks, I thought we were talking about how to use sw without using precache? But the I also went to the production site, when I reload, I see both index.html and many other static asset files are served from precache. |
@ZYinMD my sw is using the EDIT: I mean, if you're not using |
@ZYinMD you should ask on workbox repo instead here |
Thanks, after some reading, I've come to the conclusion that both |
Stumbled upon very this issue, fixed it by adjusting |
have some solution? If I don't need the precacheAndRoute (because I have to give a page asap on the first load), but need the registerRoute, now I must add //sw.js
import { registerRoute } from 'workbox-routing'
import {
NetworkFirst,
StaleWhileRevalidate,
CacheFirst
} from 'workbox-strategies'
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
import { ExpirationPlugin } from 'workbox-expiration'
console.log(self.__WB_MANIFEST)
registerRoute(
({ request }) => request.mode === 'navigate',
new NetworkFirst({
cacheName: 'pages',
plugins: [
new CacheableResponsePlugin({
statuses: [200]
})
]
})
)
registerRoute(
({ request }) =>
request.destination === 'style' ||
request.destination === 'script' ||
request.destination === 'worker' ||
request.destination === 'font',
new StaleWhileRevalidate({
cacheName: 'assets',
plugins: [
new CacheableResponsePlugin({
statuses: [200]
})
]
})
)
registerRoute(
({ request }) => request.destination === 'image',
new CacheFirst({
cacheName: 'images',
plugins: [
new CacheableResponsePlugin({
statuses: [200]
}),
new ExpirationPlugin({
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 30
})
]
})
) //vite.config.js
VitePWA({
mode: process.env.NODE_MODE,
base: "/",
srcDir: "src",
filename: "sw.js",
includeAssets: ["/assets/favicon.png"],
strategies: "injectManifest",
manifest: {
name: "CRM",
short_name: "J-CRM",
theme_color: "#78aab3",
start_url: "/",
display: "standalone",
background_color: "#c0c0c0",
icons: [
{
src: "/assets/android-chrome-192x192.png",
sizes: "192x192",
type: "image/png",
},
{
src: "/assets/android-chrome-512x512.png",
sizes: "512x512",
type: "image/png",
purpose: "any maskable",
},
],
},
}) |
I solve my problem by using this configuration injectManifest: { On VitePWA plugin. https://vite-pwa-org.netlify.app/guide/inject-manifest.html#service-worker-code |
(I'm using injectManifest)
In my app, I need to use sw to intercept and modify traffic, but I don't need precaching (in fact, precaching leads to various troubles in my case).
So, I removed
precacheAndRoute(self.__WB_MANIFEST)
from my sw source code, then I got this error when building:Is there a way to use workbox routing strategies without using precache? Thanks!
The text was updated successfully, but these errors were encountered: