Skip to content

[#31] Fix Pop-In in LazyRouter #32

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

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/LazyComponents/LazyRoute/LazyRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class LazyRouter : IComponent, IHandleAfterRender, IDisposable

[Inject] private IAssemblyLoader _assemblyLoader { get; set; } = null!;

[Parameter] public RenderFragment<string> Loading { get; set; } = null!;
[Parameter] public RenderFragment<string?> Loading { get; set; } = null!;

private bool isFirstRender = true;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -77,6 +79,7 @@ public void Attach(RenderHandle renderHandle)
_renderHandle = renderHandle;
_baseUri = NavigationManager.BaseUri;
_locationAbsolute = NavigationManager.Uri;

NavigationManager.LocationChanged += OnLocationChanged;
}

Expand Down Expand Up @@ -113,7 +116,8 @@ public async Task SetParametersAsync(ParameterView parameters)
throw new InvalidOperationException($"The {nameof(LazyRouter)} component requires a value for the parameter {nameof(NotFound)}.");
}

await RefreshRouteTable().ConfigureAwait(true); // only render thread
await PerformInitialRouting();
await RefreshLazyRouteTableAsync();
}

public void Dispose()
Expand Down Expand Up @@ -163,7 +167,7 @@ private async Task PerformModuleLoadAsync(string moduleName)

if (lazyPageAssembly == null)
{
_renderHandle.Render(Loading(moduleName)); // show loading RenderFragment
Render(Loading(moduleName)); // show loading RenderFragment

lazyPageAssembly = await _assemblyLoader
.LoadAssemblyByNameAsync(new AssemblyName
Expand All @@ -183,12 +187,23 @@ private async Task PerformModuleLoadAsync(string moduleName)
? new[] { lazyPageAssembly }
: AdditionalAssemblies.Concat(new[] { lazyPageAssembly });

await RefreshRouteTable(); // from render thread
await RefreshLazyRouteTableAsync();
}

private async Task RefreshRouteTable()
private Task PerformInitialRouting()
{
var createLazyRoutesTask = GetLazyRoutes().ConfigureAwait(true); // continue in render thread
var assemblies = AdditionalAssemblies == null
? new[] { AppAssembly }
: new[] { AppAssembly }.Concat(AdditionalAssemblies);

Routes = RouteTableFactory.Create(assemblies);

return PerformNavigationAsync(isNavigationIntercepted: false); // from render thread
}

private async Task RefreshLazyRouteTableAsync()
{
var createLazyRoutesTask = GetLazyRoutes(); // continue in render thread

var assemblies = AdditionalAssemblies == null
? new[] { AppAssembly }
Expand Down Expand Up @@ -258,7 +273,7 @@ private Task PerformNavigationAsync(bool isNavigationIntercepted)
typeHandler,
context.Parameters ?? _emptyParametersDictionary);

_renderHandle.Render(Found(routeData));
Render(Found(routeData));
return Task.CompletedTask;
}

Expand All @@ -270,20 +285,27 @@ private Task PerformNavigationAsync(bool isNavigationIntercepted)
}
}

if (!isNavigationIntercepted)
if (isFirstRender)
{
// We did not find a Component that matches the route.
// Only show the NotFound content if the application developer programatically got us here i.e we did not
// intercept the navigation. In all other cases, force a browser navigation since this could be non-Blazor content.
_renderHandle.Render(NotFound);
Render(Loading(null));
return Task.CompletedTask;
}
else

if (!isNavigationIntercepted)
{
NavigationManager.NavigateTo(_locationAbsolute, forceLoad: true);
Render(NotFound);
}

NavigationManager.NavigateTo(_locationAbsolute, forceLoad: true);

return Task.CompletedTask;
}

private void Render(RenderFragment fragment)
{
_renderHandle.Render(fragment);
isFirstRender = false;
}
}

internal class LazyRouteHandler
Expand Down