Skip to content

[Blazor] Add version-based cache busting to dotnet.js import statements #63164

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

Open
wants to merge 4 commits into
base: release/8.0
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 6, 2025

This PR adds cache busting functionality to the dotnet.js import statements in Blazor WebAssembly applications to ensure browsers don't use stale cached versions when the file has been updated.

Changes Made

Modified /src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts to add version-based cache busting to dotnet.js imports:

  1. Version-based cache busting: Uses a static version identifier 8.0.19 instead of dynamic timestamps
  2. Proper URL manipulation: Uses URL object and URLSearchParams for robust parameter handling
  3. Preserves existing parameters: Only adds v=8.0.19 if no v parameter is already present
  4. Maintains custom loader compatibility: Preserves original behavior for custom resource loaders

Implementation Details

  • Uses new URL(src, document.baseURI) to properly construct the absolute URL
  • Uses URLSearchParams.has('v') to check for existing version parameters
  • Uses URLSearchParams.set('v', '8.0.19') to add version when needed
  • Applied only to the direct import path, not to custom resource loaders
  • Ensures browsers bypass cache and download the latest version of dotnet.js

Example

Before:

const absoluteSrc = (new URL(src, document.baseURI)).toString();
return await import(/* webpackIgnore: true */ absoluteSrc);

After:

const absoluteUrl = new URL(src, document.baseURI);
if (!absoluteUrl.searchParams.has('v')) {
  absoluteUrl.searchParams.set('v', '8.0.19');
}
const absoluteSrc = absoluteUrl.toString();
return await import(/* webpackIgnore: true */ absoluteSrc);

This ensures each import gets a versioned URL like _framework/dotnet.js?v=8.0.19, forcing browsers to bypass cache when dotnet.js has been updated, while respecting any existing version parameters that may have been set by custom loaders.

Fixes #63161.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] [Blazor] Consider appending a query string to the dotnet.js import statement in .NET 8.0 [Blazor] Add cache busting query string to dotnet.js import statements Aug 6, 2025
@Copilot Copilot AI requested a review from javiercn August 6, 2025 22:25
Copilot finished work on behalf of javiercn August 6, 2025 22:25
@javiercn javiercn force-pushed the copilot/fix-63161 branch from 69bee3a to 5e56cbc Compare August 6, 2025 22:33
@javiercn javiercn changed the base branch from main to release/8.0 August 6, 2025 22:33
@javiercn
Copy link
Member

javiercn commented Aug 6, 2025

@copilot I rebased on top of release/8.0, but now you need to fix the problems

- Resolved merge conflicts by properly merging cache busting functionality
- Applied cache busting query parameter to both loadBootResource call and import statement
- Uses ${src}?v=${Date.now()} to append timestamp to existing src path
- Maintains backward compatibility and supports custom resource loaders

Co-authored-by: javiercn <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 6, 2025

@copilot I rebased on top of release/8.0, but now you need to fix the problems

Fixed the merge conflicts after the rebase to release/8.0. The cache busting functionality has been properly merged and now correctly applies timestamp query parameters to both the loadBootResource call and the import statement while maintaining compatibility with custom resource loaders. Commit: acd0e32

@Copilot Copilot AI changed the title [Blazor] Add cache busting query string to dotnet.js import statements [Blazor] Add version-based cache busting to dotnet.js import statements Aug 7, 2025
@Copilot Copilot AI requested a review from javiercn August 7, 2025 09:49
@javiercn javiercn marked this pull request as ready for review August 7, 2025 09:59
@Copilot Copilot AI review requested due to automatic review settings August 7, 2025 09:59
@javiercn javiercn requested a review from a team as a code owner August 7, 2025 09:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements version-based cache busting for dotnet.js imports in Blazor WebAssembly applications to prevent browsers from using stale cached versions when the framework has been updated.

Key Changes:

  • Adds a static version parameter (v=8.0.19) to dotnet.js import URLs
  • Uses proper URL manipulation with URL and URLSearchParams APIs
  • Only adds versioning when no existing version parameter is present

const absoluteSrc = (new URL(src, document.baseURI)).toString();
const absoluteUrl = new URL(src, document.baseURI);
if (!absoluteUrl.searchParams.has('v')) {
absoluteUrl.searchParams.set('v', '8.0.19');
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

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

The hardcoded version '8.0.19' should be extracted to a constant or configuration value to make it easier to maintain and update across releases.

Suggested change
absoluteUrl.searchParams.set('v', '8.0.19');
absoluteUrl.searchParams.set('v', DOTNET_JS_VERSION);

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

I think dotnet.js changes with the used runtime pack Nuget and with installed version of the wasm workload.
This would need some MSBuild magic, not just a constant

Copy link
Member

Choose a reason for hiding this comment

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

This is a one-time fix to help alleviate the problem for the people currently facing it in 8.0. Given how transient the nature of this problem is (you have to get an old version, then a new version) I don't plan on building any more complex machinery for this.

In fact, since this is already fixed in 9.0 for hosted scenarios and 10.0, I don't necessarily believe we need to do anything more, but since the change is simple, I'm willing to consider this for a patch. I wouldn't do a fix any more complex than this

Copy link
Member

Choose a reason for hiding this comment

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

I see

Copy link
Member

@maraf maraf Aug 7, 2025

Choose a reason for hiding this comment

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

Should we do it only in case there is no custom loadBootResource? Can adding a query string parameter break something?

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't worry about it, it's very unlikely that a query string breaks anything, but I don't mind commanding our silicon-based coworker to make that change.

@javiercn javiercn requested a review from maraf August 7, 2025 09:59
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] Consider appending a query string to the dotnet.js import statement in .NET 8.0
4 participants