Skip to content

Commit d15c53c

Browse files
committed
fix(city): nav handling
also: - remove unused context - listen on document for click handler, sometimes body is missing
1 parent ff8c4c1 commit d15c53c

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
lines changed

packages/qwik-city/src/runtime/src/contexts.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
RouteLocation,
88
RouteNavigate,
99
RoutePreventNavigate,
10-
RouteStateInternal,
1110
} from './types';
1211

1312
export const RouteStateContext = /*#__PURE__*/ createContextId<Record<string, any>>('qc-s');
@@ -24,8 +23,5 @@ export const RouteNavigateContext = /*#__PURE__*/ createContextId<RouteNavigate>
2423

2524
export const RouteActionContext = /*#__PURE__*/ createContextId<RouteAction>('qc-a');
2625

27-
export const RouteInternalContext =
28-
/*#__PURE__*/ createContextId<Signal<RouteStateInternal>>('qc-ir');
29-
3026
export const RoutePreventNavigateContext =
3127
/*#__PURE__*/ createContextId<RoutePreventNavigate>('qc-p');

packages/qwik-city/src/runtime/src/qwik-city-component.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
ContentInternalContext,
2424
DocumentHeadContext,
2525
RouteActionContext,
26-
RouteInternalContext,
2726
RouteLocationContext,
2827
RouteNavigateContext,
2928
RoutePreventNavigateContext,
@@ -229,7 +228,7 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
229228
// The initial value of routeInternal is derived from the server env,
230229
// which in the case of SSG may not match the actual origin the site
231230
// is deployed on.
232-
if (isBrowser && routeInternal.value.type === 'initial') {
231+
if (isBrowser && type === 'link' && routeInternal.value.type === 'initial') {
233232
routeInternal.value.dest = new URL(window.location.href);
234233
}
235234

@@ -320,12 +319,12 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
320319
useContextProvider(RouteNavigateContext, goto);
321320
useContextProvider(RouteStateContext, loaderState);
322321
useContextProvider(RouteActionContext, actionState);
323-
useContextProvider(RouteInternalContext, routeInternal);
324322
useContextProvider<any>(RoutePreventNavigateContext, registerPreventNav);
325323

326324
useTask$(({ track }) => {
327325
async function run() {
328-
const [navigation, action] = track(() => [routeInternal.value, actionState.value]);
326+
const navigation = track(routeInternal);
327+
const action = track(actionState);
329328

330329
const locale = getLocale('');
331330
const prevUrl = routeLocation.url;
@@ -387,6 +386,7 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
387386
try {
388387
loadedRoute = await loadRoutePromise;
389388
} catch (e) {
389+
console.error(e);
390390
window.location.href = newHref;
391391
return;
392392
}
@@ -523,7 +523,7 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
523523
// Firefox only does it once and no more, but will still scroll. It also sets state to null.
524524
// Any <a> tags w/ #hash href will break SPA state in Firefox.
525525
// We patch these events and direct them to Link pipeline during SPA.
526-
document.body.addEventListener('click', (event) => {
526+
document.addEventListener('click', (event) => {
527527
if (event.defaultPrevented) {
528528
return;
529529
}
@@ -561,7 +561,7 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
561561
}
562562
});
563563

564-
document.body.removeEventListener('click', win._qCityInitAnchors!);
564+
document.removeEventListener('click', win._qCityInitAnchors!);
565565
win._qCityInitAnchors = undefined;
566566

567567
// TODO Remove block after Navigation API PR.
@@ -678,7 +678,6 @@ export const QwikCityMockProvider = component$<QwikCityMockProps>((props) => {
678678
);
679679

680680
const loaderState = useSignal({});
681-
const routeInternal = useSignal<RouteStateInternal>({ type: 'initial', dest: url });
682681

683682
const goto: RouteNavigate =
684683
props.goto ??
@@ -707,7 +706,6 @@ export const QwikCityMockProvider = component$<QwikCityMockProps>((props) => {
707706
useContextProvider(RouteNavigateContext, goto);
708707
useContextProvider(RouteStateContext, loaderState);
709708
useContextProvider(RouteActionContext, actionState);
710-
useContextProvider(RouteInternalContext, routeInternal);
711709

712710
return <Slot />;
713711
});

packages/qwik-city/src/runtime/src/spa-init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export default event$((_: Event, el: Element) => {
204204
setTimeout(() => {
205205
win.addEventListener('popstate', win[initPopstate]!);
206206
win.addEventListener('scroll', win[initScroll]!, { passive: true });
207-
document.body.addEventListener('click', win[initAnchors]!);
207+
document.addEventListener('click', win[initAnchors]!);
208208

209209
if (!(win as any).navigation) {
210210
document.addEventListener('visibilitychange', win[initVisibility]!, {

0 commit comments

Comments
 (0)