Skip to content
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

Fix CSS Hot Reload on iOS device #27812

Merged
merged 2 commits into from
Mar 6, 2025
Merged

Conversation

spadapet
Copy link
Contributor

Description of Change

This fixes a bug with:

  • MAUI Blazor Hybrid apps
  • Open in Visual Studio 2022
  • Using Pair to Mac to debug on a Mac
  • Hot reload CSS on iOS Simulator

Hot reloading CSS didn't work because the web view on iOS didn't refresh the CSS since the href didn't change. The CSS link looks like:

  • <link rel="stylesheet" href="app.css" />

And when the CSS file changed, the href was modified by adding an empty string to it. The browser in iOS didn't detect any change in the href (since the change was a no-op) and didn't refresh the CSS. On Windows and Android the browser would still refresh CSS when the href had a no-op change.

My fix is to make a real change to the CSS link's href by appending a query string with a timestamp to it. Now all the browsers refresh the CSS since the timestamp is always unique.

Issues Fixed

Bug 1821555: [XVS][MAUI] CSS Hot reload on iOS Simulator not working

@spadapet spadapet self-assigned this Feb 14, 2025
@Copilot Copilot bot review requested due to automatic review settings February 14, 2025 20:25
@spadapet spadapet requested a review from a team as a code owner February 14, 2025 20:25
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.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

src/BlazorWebView/src/SharedSource/StaticContentHotReloadManager.cs:32

  • Ensure that there is a test case verifying that the CSS reloads correctly with the new timestamp query string.
allLinkElems.forEach(elem => elem.href = elem.href.split('?')[0] + '?reload_version=' + Date.now());

@spadapet spadapet requested a review from lewing February 14, 2025 22:29
@rmarinho rmarinho added the area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging label Feb 17, 2025
@spadapet spadapet enabled auto-merge February 20, 2025 19:46
@spadapet
Copy link
Contributor Author

Ping for code review: @dotnet/dotnet-maui-blazor-eng

@spadapet spadapet force-pushed the dev/peterspa/HotReloadCssMac branch 2 times, most recently from 16321e5 to 6ad7a2e Compare February 24, 2025 18:36
@spadapet
Copy link
Contributor Author

@Redth , @lewing , thanks for reviewing my other CSS hot reload PR (#27788).
Can you review this one too?

Appending an empty string is not enough to force CSS to be reloaded, the new URL needs to be unique, so a timestamp is appended. Thanks!

@spadapet spadapet requested a review from Redth February 24, 2025 18:39
Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

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

Is there ever a case where the css would have a query string already?

Also, this looks to be getting all css, what happens with CDNs and things like that? Will that refresh all remote ones too?

@spadapet
Copy link
Contributor Author

Is there ever a case where the css would have a query string already?

Also, this looks to be getting all css, what happens with CDNs and things like that? Will that refresh all remote ones too?

Good questions. For the CSS files that can be hot reloaded, those are static files and shouldn't have a query string. But your second comment about refreshing all CSS files (even remote ones) is right. That isn't a new problem introduced by this PR, but it was there and I'm touching that code, so I'll see if the refresh can somehow be scoped to what actually changed.

@spadapet
Copy link
Contributor Author

Ah, @mattleibow looks like refreshing every CSS file was done on purpose:

image

So I think I either:

  • Just do a filename match for each CSS file and deal with occasional false positives
  • Fix up the query string with a timestamp in a smarter way that preserves other parameters, in case all CSS files still need to be refreshed

@mattleibow mattleibow disabled auto-merge February 27, 2025 04:01
@mattleibow
Copy link
Member

mattleibow commented Feb 27, 2025

Doesn't the browser have a something to update query strings? Like, if the param exists, then update else add?

Edit

let url = new URL("https://example.com?foo=1&bar=2");
let params = new URLSearchParams(url.search);

// Add a third parameter.
params.set("baz", 3);
params.toString(); // "foo=1&bar=2&baz=3"

@spadapet
Copy link
Contributor Author

spadapet commented Feb 27, 2025

Doesn't the browser have a something to update query strings? Like, if the param exists, then update else add?

Yes, I'm about to push my changes that seem to solve the issues:

  • Add or update a timestamp parameter without affecting others
  • Only refresh CSS files that match the filename of the one that changed. This should be way more efficient than reloading all CSS files, with rare false positives.

@spadapet spadapet force-pushed the dev/peterspa/HotReloadCssMac branch from 6ad7a2e to 8f32093 Compare February 27, 2025 07:35
@spadapet spadapet requested a review from mattleibow February 27, 2025 07:36
@spadapet spadapet force-pushed the dev/peterspa/HotReloadCssMac branch 3 times, most recently from 3c40b3e to e34f0a3 Compare March 3, 2025 18:29
@spadapet spadapet force-pushed the dev/peterspa/HotReloadCssMac branch from e34f0a3 to c7f25a8 Compare March 4, 2025 18:53
@mattleibow mattleibow enabled auto-merge (squash) March 6, 2025 07:29
@mattleibow mattleibow merged commit 64e9c3d into main Mar 6, 2025
123 checks passed
@mattleibow mattleibow deleted the dev/peterspa/HotReloadCssMac branch March 6, 2025 10:56
bhavanesh2001 pushed a commit to bhavanesh2001/maui that referenced this pull request Mar 7, 2025
* Fix CSS file reloading on Mac

* Code review
rmarinho pushed a commit that referenced this pull request Mar 11, 2025
* Fix CSS file reloading on Mac

* Code review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants