Skip to content

Commit 274d038

Browse files
authored
Merge pull request #7612 from maiieul/Link-onQVisible-handling
2 parents 336a54d + 27c7bda commit 274d038

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

.changeset/witty-spoons-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
🩹 SPA Link now handle subsequent onQVisible$ passed as props.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
useSignal,
88
useVisibleTask$,
99
untrack,
10+
type EventHandler,
11+
type QwikVisibleEvent,
1012
} from '@builder.io/qwik';
1113
import { getClientNavPath, shouldPreload } from './utils';
1214
import { loadClientData } from './use-endpoint';
@@ -85,6 +87,22 @@ export const Link = component$<LinkProps>((props) => {
8587

8688
useVisibleTask$(({ track }) => {
8789
track(() => loc.url.pathname);
90+
// We need to trigger the onQVisible$ in the visible task for it to fire on subsequent route navigations
91+
const handler = linkProps.onQVisible$;
92+
if (handler) {
93+
const event = new CustomEvent('qvisible') as QwikVisibleEvent;
94+
95+
if (Array.isArray(handler)) {
96+
(handler as any)
97+
.flat(10)
98+
.forEach((handler: EventHandler<QwikVisibleEvent, HTMLAnchorElement>) =>
99+
handler?.(event, anchorRef.value!)
100+
);
101+
} else {
102+
handler?.(event, anchorRef.value!);
103+
}
104+
}
105+
88106
// Don't prefetch on visible in dev mode
89107
if (!isDev && anchorRef.value) {
90108
handlePrefetch?.(undefined, anchorRef.value!);
@@ -101,6 +119,8 @@ export const Link = component$<LinkProps>((props) => {
101119
data-prefetch={prefetchData}
102120
onMouseOver$={[linkProps.onMouseOver$, handlePrefetch]}
103121
onFocus$={[linkProps.onFocus$, handlePrefetch]}
122+
// We need to prevent the onQVisible$ from being called twice since it is handled in the visible task
123+
onQVisible$={[]}
104124
>
105125
<Slot />
106126
</a>

starters/apps/preloader-test/src/routes/counters/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { component$ } from "@builder.io/qwik";
2-
2+
import { Link } from "@builder.io/qwik-city";
33
import Counter1 from "~/components/generated/counter1";
44
import Counter2 from "~/components/generated/counter2";
55
import Counter3 from "~/components/generated/counter3";
@@ -213,6 +213,12 @@ export default component$(() => {
213213
<Counter98 />
214214
<Counter99 />
215215
<Counter100 />
216+
<Link
217+
href="/hidden"
218+
onQVisible$={() => console.log("visible below fold")}
219+
>
220+
Home
221+
</Link>
216222
</div>
217223
);
218224
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { component$ } from "@builder.io/qwik";
2+
3+
export default component$(() => {
4+
return <div>Hidden</div>;
5+
});

starters/apps/preloader-test/src/routes/layout.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default component$(() => {
5656
}
5757
`);
5858

59-
const isSPA = useSignal(false);
59+
const isSPA = useSignal(true);
6060
const LinkCmp = isSPA.value ? Link : "a";
6161

6262
return (
@@ -67,7 +67,12 @@ export default component$(() => {
6767
<LinkCmp href="/">Home</LinkCmp>
6868
<LinkCmp href="/form">Form</LinkCmp>
6969
<LinkCmp href="/about">About</LinkCmp>
70-
<LinkCmp href="/counters">Counters</LinkCmp>
70+
<LinkCmp
71+
href="/counters"
72+
onQVisible$={() => console.log("visible")}
73+
>
74+
Counters
75+
</LinkCmp>
7176
</nav>
7277
<label class="toggle-label">
7378
<input type="checkbox" bind:checked={isSPA} />

starters/apps/preloader-test/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function createBulkPlugin(): Plugin {
2525

2626
// Create bulk with an exponential-like distribution between 0kb and 50kb
2727
// Most files will be closer to 0kb, with a few reaching 50kb
28-
const maxSize = 50000;
28+
const maxSize = 500;
2929
const x = hash[0] / 255; // Normalize first byte to 0-1
3030
const exp = Math.pow(x, 6); // Skew distribution
3131
const bulkSize = Math.floor(maxSize * exp);

0 commit comments

Comments
 (0)