-
Notifications
You must be signed in to change notification settings - Fork 380
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
Add try catch in FluentDesignTheme #2204
Add try catch in FluentDesignTheme #2204
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good addition, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain good performance, it's never a good idea to add a Try/Catch.
But in this case, it's probably negligible.
On the other hand, I don't see how the LocalStorage could contain a value not supported by the library itself. Unless the dev or the user manually modifies this value... and in that case, too bad for him :-)
@dvoituron this can happen when you migrate from another library to FluentUI. In My case I've migrated from a Bootstrap Libary to FluentUI. This library also uses localStorage to store theme details in key Every old user visiting the site will get an exception now until they clear their localStorage. |
Yep. It's not a FluentUI Blazor issue 😀 You could add a script to clean your user environments. |
@dvoituron yes. Right now I'm doing this as a workaround: public class ClearSiteDataMiddleware
{
private readonly RequestDelegate _next;
public ClearSiteDataMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
// Add the Clear-Site-Data header to the response
context.Response.OnStarting(() =>
{
context.Response.Headers.Append("Clear-Site-Data", "\"cache\", \"storage\"");
return Task.CompletedTask;
});
// Call the next middleware in the pipeline
await _next(context);
}
} but how would I know when I actually cleaned the cache/localStorage for every user? As long as this script is active, the user cannot switch the theme in the app. |
You could run a JS Script immediately in your "index.html" page (detecting if this localstorage is a Json or not) |
@dvoituron I don't have an |
You must have a "first HTML" page: where the "Blazor.js" file is defined. All Blazor components are initialized after these JS files. |
But we can merge your PR. It's more about the technical reason for this request. |
Pull Request
📖 Description
This PR adds a try-catch block to
FluentDesignTheme.razor.js
to prevent site crashing on invalid data within the localStorage. This makes it easier to migrate to FluentUI in existing projects which uses the same localStorage keys to store theme information.For example execute this in the browsers console:
You'll notice that the website will crash until you manually clear the localStorage.
I had this issue already when switching from certain Bootstrap Frameworks to FluentUI. This PR prevents this crashes and will automatically clear the localStorage for the theme when
JSON.parse
fails👩💻 Reviewer Notes
You can get around with it by providing the
FluentDesignTheme
another key. But in my opinion it should check use try-catch anyways in case the object will get changed in the future.📑 Test Plan
✅ Checklist
General
Component-specific
⏭ Next Steps