Skip to content

Commit c78e62b

Browse files
Start up interactive components after enhanced nav. This is not a complete implementation of #48763 but will be OK for preview 6.
1 parent 2e91fff commit c78e62b

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/Components/Web.JS/src/Boot.Web.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,45 @@ import { shouldAutoStart } from './BootCommon';
1515
import { Blazor } from './GlobalExports';
1616
import { WebStartOptions } from './Platform/WebStartOptions';
1717
import { attachStreamingRenderingListener } from './Rendering/StreamingRendering';
18-
import { attachProgressivelyEnhancedNavigationListener } from './Services/NavigationEnhancement';
18+
import { attachProgressivelyEnhancedNavigationListener, detachProgressivelyEnhancedNavigationListener } from './Services/NavigationEnhancement';
1919
import { WebAssemblyComponentDescriptor } from './Services/ComponentDescriptorDiscovery';
2020
import { ServerComponentDescriptor, discoverComponents } from './Services/ComponentDescriptorDiscovery';
2121

2222
let started = false;
23+
let webStartOptions: Partial<WebStartOptions> | undefined;
2324

2425
async function boot(options?: Partial<WebStartOptions>): Promise<void> {
2526
if (started) {
2627
throw new Error('Blazor has already started.');
2728
}
2829
started = true;
29-
await activateInteractiveComponents(options);
30+
webStartOptions = options;
3031

3132
attachStreamingRenderingListener(options?.ssr);
32-
attachProgressivelyEnhancedNavigationListener();
33+
attachProgressivelyEnhancedNavigationListener(activateInteractiveComponents);
34+
await activateInteractiveComponents();
3335
}
3436

35-
async function activateInteractiveComponents(options?: Partial<WebStartOptions>) {
37+
async function activateInteractiveComponents() {
3638
const serverComponents = discoverComponents(document, 'server') as ServerComponentDescriptor[];
3739
const webAssemblyComponents = discoverComponents(document, 'webassembly') as WebAssemblyComponentDescriptor[];
3840

3941
if (serverComponents.length) {
40-
await startCircuit(options?.circuit, serverComponents);
42+
// TEMPORARY until https://github.com/dotnet/aspnetcore/issues/48763 is implemented
43+
// As soon we we see you have interactive components, we'll stop doing enhanced nav even if you don't have an interactive router
44+
// This is because, otherwise, we would need a way to add new interactive root components to an existing circuit and that's #48763
45+
detachProgressivelyEnhancedNavigationListener();
46+
47+
await startCircuit(webStartOptions?.circuit, serverComponents);
4148
}
4249

4350
if (webAssemblyComponents.length) {
44-
await startWebAssembly(options?.webAssembly, webAssemblyComponents);
51+
// TEMPORARY until https://github.com/dotnet/aspnetcore/issues/48763 is implemented
52+
// As soon we we see you have interactive components, we'll stop doing enhanced nav even if you don't have an interactive router
53+
// This is because, otherwise, we would need a way to add new interactive root components to an existing WebAssembly runtime and that's #48763
54+
detachProgressivelyEnhancedNavigationListener();
55+
56+
await startWebAssembly(webStartOptions?.webAssembly, webAssemblyComponents);
4557
}
4658
}
4759

src/Components/Web.JS/src/Services/NavigationEnhancement.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ different bundles that only contain minimal content.
2929
*/
3030

3131
let currentEnhancedNavigationAbortController: AbortController | null;
32+
let onDocumentUpdatedCallback: Function = () => {};
3233

33-
export function attachProgressivelyEnhancedNavigationListener() {
34+
export function attachProgressivelyEnhancedNavigationListener(onDocumentUpdated: Function) {
35+
onDocumentUpdatedCallback = onDocumentUpdated;
3436
document.body.addEventListener('click', onBodyClicked);
3537
window.addEventListener('popstate', onPopState);
3638
}
3739

40+
export function detachProgressivelyEnhancedNavigationListener() {
41+
document.body.removeEventListener('click', onBodyClicked);
42+
window.removeEventListener('popstate', onPopState);
43+
}
44+
3845
function onBodyClicked(event: MouseEvent) {
3946
if (hasInteractiveRouter()) {
4047
return;
@@ -88,6 +95,13 @@ async function performEnhancedPageLoad(internalDestinationHref: string) {
8895
});
8996

9097
if (!abortSignal.aborted) {
98+
// TEMPORARY until https://github.com/dotnet/aspnetcore/issues/48763 is implemented
99+
// We should really be doing this on the `onInitialDocument` callback *and* inside the <blazor-ssr> custom element logic
100+
// so we can add interactive components immediately on each update. Until #48763 is implemented, the stopgap implementation
101+
// is just to do it when the enhanced nav process completes entirely, and then if we do add any interactive components, we
102+
// disable enhanced nav completely.
103+
onDocumentUpdatedCallback();
104+
91105
// The whole response including any streaming SSR is now finished, and it was not aborted (no other navigation
92106
// has since started). So finally, recreate the native "scroll to hash" behavior.
93107
const hashPosition = internalDestinationHref.indexOf('#');

0 commit comments

Comments
 (0)