-
Notifications
You must be signed in to change notification settings - Fork 25.1k
[Blazor] Lazy loading - Complete example - Prevent repeated loads #34243
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
Conversation
🤔This wasn't part of Safia's original bits for the feature ... ... and it never made its way into the test component (but they might want to add it after they see what you wrote) ... ... and it didn't come up for discussion on the docs issue or PR ...
I've added the Blazor engineering group to see if anyone is free to take a look at this. If we don't hear back within a couple of days, I'll reach out offline to get someone on here. |
|
Adding this guard here is mainly beneficial for the sample code, since otherwise (like @hakenr mentions) it ends up duplicating data. That's not an issue in the product itself; it's just a quirk of the sample code in the docs. Whether or not people want to use a guard clause to ensure they don't see any flash of "loading..." content is up to the app developer. Altogether the changes suggested here are good and I think they should be added to the docs, as it gives readers more of a sense of things they might want to do to get good end-to-end behavior in their own apps. |
| if (args.Path == "robot") | ||
| if ((args.Path == "robot") && !grantImaharaRobotControlsAssemblyLoaded) | ||
| { | ||
| grantImaharaRobotControlsAssemblyLoaded = true; |
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.
NIT question on the location of this line ...
Because the component survives an exception on the next two lines, does it make sense to move this line (grantImaharaRobotControlsAssemblyLoaded = true;) to the end after the lazyLoadedAssemblies.AddRange(assemblies); line (i.e., it's only true if we know those two lines executed)?
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.
I'm going to go ahead and move the line down to get this in. I'm about to shoot a bunch of PRs into the live branch.
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.
@guardrex Neither of the two alternatives is perfect. I usually prefer to set the flag before an awaited operation - because with the await there, another navigation might occur while waiting for the assembly to load, leaving the flag still false for the second run.
In this case, since the awaited assembly load won’t cause a critical failure if called repeatedly, I agree that your version is better.
guardrex
left a comment
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.
Even though repeated calls to
AssemblyLoader.LoadAssembliesAsync()don't download the assembly multiple times or throw exception:<Navigating>content is rendered in the meantime, causing unnecessary UI flickering when navigating to the lazy-loaded area repeatedly.lazyLoadedAssembliescollection gets populated with duplicate entries.Therefore, lazy-loading should be guarded with an "already loaded" flag.
Internal previews