-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Develop and debug ServiceWorker #955
Comments
Hi, any updates on this? It's an important topic, not sure what the "official" guidelines are myself. |
It's disappointing the Preact service worker doc doesn't seem to cover the info needed to get a simple custom service worker up and running, which states:
However I get the following error in the browser's console after when running
src/sw.js: import { getFiles, setupPrecaching, setupRouting } from "preact-cli/sw/";
import { registerRoute } from "workbox-routing";
import { NetworkFirst } from "workbox-strategies";
registerRoute(
({ url }) => url.pathname.startsWith("/api/"),
new NetworkFirst()
);
setupRouting();
setupPrecaching(getFiles()); Is the InjectManifestPlugin not actually compiling the service worker? |
Hi @JCofman - Preact CLI doesn't actually compile SW during development (preact watch). It serves a debug sw.js that is mainly there to replace any stuck production Service Worker, but that's it. I think it would be interesting to have the option of enabling the SW generation for dev builds. The code that does the SW generation (below) will need to be modified to support being injected when Another option might be to have dev mode always use InjectManifestPlugin, even when generating it's basic no-op service worker. That would make it easy to replace the SW with a custom one in a way that works in both dev and prod builds. |
@developit thanks for the suggestions! I'm now using the script below in my The main issue I'm seeing now is that changing When the recompile happens the following is logged on the server:
I would like to find a way to toggle off the workbox logging when not needed as its very chatty. preact.config.jsimport Webpack from "webpack";
import { InjectManifest } from "workbox-webpack-plugin";
import { join } from "path";
import { existsSync } from "fs";
export default function (config, env, helpers) {
if (env.isWatch) {
let swPath;
if (env.sw) {
swPath = join(__dirname, "..", "..", "..", "sw", "sw.js");
const userSwPath = join(env.src, "sw.js");
if (existsSync(userSwPath)) {
swPath = userSwPath;
} else {
console.log(
`Could not find sw.js in ${env.src}. Using the default service worker.`
);
}
}
if (env.esm && env.sw) {
config.plugins.push(
new InjectManifest({
swSrc: swPath,
swDest: "sw-esm.js",
include: [
/^\/?index\.html$/,
/\.esm.js$/,
/\.css$/,
/\.(png|jpg|svg|gif|webp)$/,
],
webpackCompilationPlugins: [
new Webpack.DefinePlugin({
"process.env.ESM": true,
}),
],
})
);
}
if (env.sw) {
config.plugins.push(
new InjectManifest({
swSrc: swPath,
include: [
/index\.html$/,
/\.js$/,
/\.css$/,
/\.(png|jpg|svg|gif|webp)$/,
],
exclude: [/\.esm\.js$/],
})
);
}
}
} |
Hi 👋
I had some issues adding a cusotm serviceWorker. I think it would be helpfull to provide some more information on the docs https://preactjs.com/cli/service-worker/ site. I am willing to help :)
Do you want to request a feature or report a bug?
Both? :)
What is the current behavior?
I am not sure how I can properly debug / update and develop the
serviceWorker
during preact watch. As mentioned in the docs https://preactjs.com/cli/service-worker/ I can write a customsw.js
and add the base functionality from the default preact serviceWorker.When I try to do it like mentioned in the docs I
sw.js
into thesrc
folder and copy paste default code from the docs--sw
preact watch --sw
to include the service worker during development phase (it's currently not mentioned in the referenced docs but I did not find another way otherwise I would get the defaultsw-debug.js
output.preact watch --sw
visiting (0.0.0.0:8080) results in an errorIf the current behaviour is a bug, please provide the steps to reproduce.
mentioned above
What is the expected behaviour?
Add an explanation / guide about how to edit serviceWorker behaviour during
preact watch
so that you can customize the default serviceWorker behavior.If this is a feature request, what is motivation or use case for changing the behaviour?
Better PWA workflow
Please mention other relevant information.
Please paste the results of
preact info
here.The text was updated successfully, but these errors were encountered: