Skip to content

fix: ensure store updates go through scheduler during SPA navigation #7725

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 30 additions & 21 deletions packages/qwik-city/src/runtime/src/qwik-city-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
_weakSerialize,
useStyles$,
_waitUntilRendered,
untrack,
type QRL,
} from '@builder.io/qwik';
import { isBrowser, isDev, isServer } from '@builder.io/qwik';
Expand Down Expand Up @@ -291,7 +292,9 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
}

actionState.value = undefined;
routeLocation.isNavigating = true;
untrack(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you put all the assignments into a single untrack?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right @wmertens !
I fixed the SPA navigation bug by implementing a single untrack() block for all store mutations, ensuring proper scheduling and preventing unnecessary re-executions during navigation.

routeLocation.isNavigating = true;
});

return new Promise<void>((resolve) => {
navResolver.r = resolve;
Expand Down Expand Up @@ -389,31 +392,35 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
}

// Update route location
if (!isSamePath(trackUrl, prevUrl)) {
routeLocation.prevUrl = prevUrl;
}

routeLocation.url = trackUrl;
routeLocation.params = { ...params };
untrack(() => {
if (!isSamePath(trackUrl, prevUrl)) {
routeLocation.prevUrl = prevUrl;
}
routeLocation.url = trackUrl;
routeLocation.params = { ...params };

// Needs to be done after routeLocation is updated
const resolvedHead = resolveHead(clientPageData!, routeLocation, contentModules, locale);

// Update content
content.headings = pageModule.headings;
content.menu = menu;
contentInternal.value = noSerialize(contentModules);

// Update document head
documentHead.links = resolvedHead.links;
documentHead.meta = resolvedHead.meta;
documentHead.styles = resolvedHead.styles;
documentHead.scripts = resolvedHead.scripts;
documentHead.title = resolvedHead.title;
documentHead.frontmatter = resolvedHead.frontmatter;
});

(routeInternal as any).untrackedValue = { type: navType, dest: trackUrl };

// Needs to be done after routeLocation is updated
const resolvedHead = resolveHead(clientPageData!, routeLocation, contentModules, locale);

// Update content
content.headings = pageModule.headings;
content.menu = menu;
contentInternal.value = noSerialize(contentModules);

// Update document head
documentHead.links = resolvedHead.links;
documentHead.meta = resolvedHead.meta;
documentHead.styles = resolvedHead.styles;
documentHead.scripts = resolvedHead.scripts;
documentHead.title = resolvedHead.title;
documentHead.frontmatter = resolvedHead.frontmatter;

if (isBrowser) {
if (props.viewTransition !== false) {
// mark next DOM render to use startViewTransition API
Expand Down Expand Up @@ -617,7 +624,9 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
saveScrollHistory(scrollState);
win._qCityScrollEnabled = true;

routeLocation.isNavigating = false;
untrack(() => {
routeLocation.isNavigating = false;
});
navResolver.r?.();
});
}
Expand Down