Skip to content

feat: serviceworker #7163

@mathe42

Description

@mathe42

Clear and concise description of the problem

With vite 2.8 we got better worker support with a custom build pipeline.

I propose the same for serviceworker so the following should be transformed:

navigator.serviceWorker.register(new URL('./sw.js', import.meta.url), {/**/})

Suggested solution

This could be a copy of the current worker implementation with the difference that service worker can't be modules so it allways has to get compiled to an iife. This is also true while in development.

So options should be something like:

export declare interface UserConfig {
   //...
    /**
     * ServiceWorker bundle options
     */
     serviceWorker?: {
        /**
         * Vite plugins that apply to serviceWorker bundle
         */
        plugins?: (PluginOption | PluginOption[])[];
        /**
         * Rollup options to build worker bundle
         */
        rollupOptions?: Omit<RollupOptions, 'plugins' | 'input' | 'onwarn' | 'preserveEntrySignatures'>;
        /**
        * Filename of ServiceWorker in bundle as this should NEVER change
        * @default sw.js
        */
        filename?: string
    };
}

This has some open questions:

  1. What happens when using multiple serviceWorker (with different scope) [how determan filename]

Alternative

No response

Additional context

The same might be usable for Worklets. (Audio, Paint, Animation, Layout)

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions