Skip to content
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

Service Worker Not being minified #62

Closed
logotip4ik opened this issue Aug 29, 2023 · 5 comments
Closed

Service Worker Not being minified #62

logotip4ik opened this issue Aug 29, 2023 · 5 comments

Comments

@logotip4ik
Copy link

https://stackblitz.com/edit/nuxt-starter-l1gvkq?file=nuxt.config.ts

Reproduction

  1. yarn && yarn run build
  2. code .output/public/sw.js stackblitz wont show and update .output directory? You need manually open it after each build

Info

strategies: 'injectManifest' will inline workbox utils but then it won't minify the service worker itself. With generateSW strategy, vite will minify both sw.js and workbox-*.js files

@userquin
Copy link
Member

@logotip4ik I'll review latest changes in Nuxt, it is weird, mode should be production and injectManifest should minify the sw (the pwa plugin includes process.env.NODE_ENV="production" via Vite define option in the custom build): https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/modules.ts#L95

@userquin
Copy link
Member

anyway, your sw has errors: missing race and debug variables and error in L55

@logotip4ik
Copy link
Author

anyway, your sw has errors: missing race and debug variables and error in L55

I thought that they would be somehow inlined at the build time 😅. I took this example from vite-pwa website (https://vite-pwa-org.netlify.app/workbox/inject-manifest.html#network-first-strategy). Should i open pr to fix this ?

@userquin
Copy link
Member

@logotip4ik found the problem:

  • by default Vite will use esbuild to minify, since we're using a custom Vite build to build the service worker, Vite will use esbuild since pwa plugin will not provide/change the build.minify option
  • vite-plugin-pwa is using es as default Rollup format to build the service worker, but Vite will not remove empty spaces https://vitejs.dev/config/build-options.html#build-minify:

    Note the build.minify option does not minify whitespaces when using the 'es' format in lib mode, as it removes pure annotations and breaks tree-shaking.

You can change the format using iife, the service worker is minified but the code generated is wrong and will not work.

You can check it here in this SB fork : https://stackblitz.com/edit/nuxt-starter-al3pyt?file=nuxt.config.ts.
If you run yarn build the output will be on server/public folder, I've changed nitro output.dir and there is an issue on nitro about custom output (also tried with terser plugin with no luck).

@logotip4ik
Copy link
Author

Can't reproduce with @vite-pwa/nuxt@0.10.0, vite-plugin-pwa@0.20.1 and vite@5.4.1, now everything is minimized, so will close. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants