Skip to content

Polyfills are not added to the service worker in production #13937

Open
@slavafomin

Description

@slavafomin

Describe the bug

Hello!

I'm using latest Svelte Kit 5 with a service-worker (i.e. a src/service-worker.ts file). I'm also using node polyfills:

// vite.config.ts

export default defineConfig({
  plugins: [
    nodePolyfills({
      include: ['buffer'],
      globals: {
        Buffer: true,
      },
    }),

However, it works fine for my main application, but when it comes to service worker it can't find a Buffer object in runtime.

Also, the service worker works fine during development, but fails in production.

Reproduction

  • add vite-plugin-node-polyfills plugin with Buffer polyfill
  • try to use Buffer inside the service worker in production (not dev-server)

System Info

System:
    OS: Linux 6.8 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
  Binaries:
    Node: 22.16.0 - ~/.nvm/versions/node/v22.16.0/bin/node
    npm: 11.4.2 - ~/.nvm/versions/node/v22.16.0/bin/npm
    pnpm: 10.10.0 - ~/.nvm/versions/node/v22.16.0/bin/pnpm
  Browsers:
    Chromium: 137.0.7151.103
  npmPackages:
    @sveltejs/adapter-static: ^3.0.8 => 3.0.8 
    @sveltejs/kit: ^2.21.5 => 2.21.5 
    @sveltejs/vite-plugin-svelte: ^5.1.0 => 5.1.0 
    svelte: ^5.34.4 => 5.34.4 
    vite: ^6.3.5 => 6.3.5
    vite-plugin-node-polyfills: ^0.23.0 => 0.23.0

Severity

Blocking app's release

Metadata

Metadata

Assignees

No one assigned

    Labels

    service workerStuff related to service workers

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions