Skip to content

Conversation

@SkyZeroZx
Copy link
Contributor

feat(service-worker): Add type option to provideServiceWorker

Enables specifying the script type ('classic' or 'module') when registering Service Workers, improving compatibility with ES module features.

The change includes:

  • Added type to SwRegistrationOptions interface
  • Modified service worker registration to pass the type option
  • Added comprehensive unit tests to verify the new functionality
  • Updated existing tests to include the new option

Motivation/Use Cases

The type option is particularly useful for:

  • ES Module support: Enabling ES module features like import/export in service worker scripts
  • Modern JavaScript: Supporting modern JavaScript syntax and module system in service workers
  • Compliance with PWA best practices: Aligning with modern service worker registration standards

Proposed Solution

  • Add type property to the SwRegistrationOptions abstract class
  • Implement property handling in the service worker registration logic
  • Pass the option through to the native navigator.serviceWorker.register() API
  • Maintain full backward compatibility with existing configurations (defaults to 'classic')

Examples of New Usage

Basic configuration with provideServiceWorker

export const appConfig: ApplicationConfig = {
  providers: [
    provideServiceWorker('ngsw-worker.js', {
      enabled: !isDevMode(),
      type: 'module', // Enable ES module features
    }),
  ],
};

@pullapprove pullapprove bot requested a review from AndrewKushnir July 26, 2025 06:03
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: service-worker Issues related to the @angular/service-worker package labels Jul 26, 2025
@ngbot ngbot bot added this to the Backlog milestone Jul 26, 2025
Enables specifying the script type ('classic' or 'module') when registering Service Workers, improving compatibility with ES module features.
@SkyZeroZx SkyZeroZx force-pushed the add-service-worker-type branch from 2ec391b to 3b82c3e Compare July 26, 2025 06:05
Copy link
Contributor

@thePunderWoman thePunderWoman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

reviewed-for: fw-general, public-api

@pullapprove pullapprove bot requested review from JeanMeche and kirjs August 6, 2025 11:38
Copy link
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api

@thePunderWoman thePunderWoman added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels Aug 6, 2025
@crisbeto
Copy link
Member

crisbeto commented Aug 6, 2025

This PR was merged into the repository by commit 5220b51.

The changes were merged into the following branches: main

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: service-worker Issues related to the @angular/service-worker package detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants