Skip to content

[Blazor] Update service-worker registration to prevent caching issues #62382

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

Merged
merged 2 commits into from
Jun 18, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 17, 2025

This PR updates the service worker registration in the Blazor WebAssembly PWA template to include the updateViaCache: 'none' option, which prevents caching issues during service worker updates.

Changes Made

Updated the service worker registration in src/ProjectTemplates/Web.ProjectTemplates/content/ComponentsWebAssembly-CSharp/wwwroot/index.html:

Before:

navigator.serviceWorker.register('service-worker.js');

After:

navigator.serviceWorker.register('service-worker.js', { updateViaCache: 'none' });

Why This Change is Needed

The updateViaCache: 'none' option ensures that:

  • The browser doesn't use cached versions of the service worker script itself
  • Service worker updates are applied reliably without being blocked by HTTP caching
  • PWA applications can update their service workers more predictably

This addresses caching issues that can prevent service worker updates from being applied correctly, which is particularly important for PWAs that rely on service workers for offline functionality.

Verification

  • ✅ PWA templates (dotnet new blazorwasm --pwa) now include the updated registration
  • ✅ Non-PWA templates remain unaffected
  • ✅ Templates build and function correctly
  • ✅ Only 1 line changed - minimal, surgical modification

Fixes #44635.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] [Blazor] Update service-worker registration to prevent caching issues [Blazor] Update service-worker registration to prevent caching issues Jun 17, 2025
@Copilot Copilot AI requested a review from javiercn June 17, 2025 21:42
Copilot finished work on behalf of javiercn June 17, 2025 21:42
@javiercn javiercn marked this pull request as ready for review June 18, 2025 09:27
@javiercn javiercn requested a review from a team as a code owner June 18, 2025 09:27
Copy link
Member

@ilonatommy ilonatommy left a comment

Choose a reason for hiding this comment

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

I don't see a problem with disabling cache but I would wait for @maraf to share his opinion.

@javiercn
Copy link
Member

I don't see a problem with disabling cache but I would wait for @maraf to share his opinion.

For context, browsers don't ever cache the service-worker file, but will cache scripts that are loaded by it, which is what caused problems in the past for our service worker (since we generate an assets.js file).

@javiercn
Copy link
Member

@copilot rebase the PR

Copy link
Contributor Author

Copilot AI commented Jun 18, 2025

@copilot rebase the PR

Rebase completed successfully. The PR is now up to date with main (commit 5d7c8bb).

Copilot finished work on behalf of javiercn June 18, 2025 10:33
@javiercn javiercn force-pushed the copilot/fix-44635 branch from 5d7c8bb to 4c6fecf Compare June 18, 2025 10:35
@javiercn javiercn merged commit 47f3830 into main Jun 18, 2025
27 checks passed
@javiercn javiercn deleted the copilot/fix-44635 branch June 18, 2025 14:12
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview7 milestone Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Blazor] Update service-worker registration to prevent caching issues
4 participants