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

Add try catch in FluentDesignTheme #2204

Merged
merged 1 commit into from
Jun 19, 2024
Merged

Add try catch in FluentDesignTheme #2204

merged 1 commit into from
Jun 19, 2024

Conversation

MarvinKlein1508
Copy link
Contributor

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:

localStorage.setItem("theme", "light");

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

  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

Component-specific

  • I have added a new component
  • I have added Unit Tests for my new compontent
  • I have modified an existing component
  • I have validated the Unit Tests for an existing component

⏭ Next Steps

Copy link
Collaborator

@vnbaaij vnbaaij left a 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.

@vnbaaij vnbaaij added this to the v4.8.1 milestone Jun 17, 2024
Copy link
Collaborator

@dvoituron dvoituron left a 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 :-)

@MarvinKlein1508
Copy link
Contributor Author

MarvinKlein1508 commented Jun 19, 2024

@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 theme.

Every old user visiting the site will get an exception now until they clear their localStorage.

@dvoituron
Copy link
Collaborator

Yep. It's not a FluentUI Blazor issue 😀 You could add a script to clean your user environments.

@MarvinKlein1508
Copy link
Contributor Author

@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.

@dvoituron
Copy link
Collaborator

You could run a JS Script immediately in your "index.html" page (detecting if this localstorage is a Json or not)

@MarvinKlein1508
Copy link
Contributor Author

@dvoituron I don't have an index.html only App.razor. Also doesn't get the JS from the component get called before my script will get the chance to execute?

@dvoituron
Copy link
Collaborator

@dvoituron I don't have an index.html only App.razor. Also doesn't get the JS from the component get called before my script will get the chance to execute?

You must have a "first HTML" page: where the "Blazor.js" file is defined. All Blazor components are initialized after these JS files.

@dvoituron
Copy link
Collaborator

But we can merge your PR. It's more about the technical reason for this request.

@dvoituron dvoituron merged commit f87ecab into microsoft:dev Jun 19, 2024
2 checks passed
@MarvinKlein1508 MarvinKlein1508 deleted the add-try-catch-fluentdesigntheme branch June 19, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants