Skip to content

Commit 52aa032

Browse files
committed
feat: add Link component fallbackToMpa prop
1 parent 19b9149 commit 52aa032

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@ export const Link = component$<LinkProps>((props) => {
2424
const loc = useLocation();
2525
const originalHref = props.href;
2626
const anchorRef = useSignal<HTMLAnchorElement>();
27+
2728
const {
2829
onClick$,
2930
prefetch: prefetchProp,
3031
reload,
3132
replaceState,
3233
scroll,
34+
fallbackToMpa: fallbackToMpaProp,
3335
...linkProps
3436
} = (() => props)();
37+
38+
// We need an RFC to assess whether or not we want to provide the ability to pass a number to customize the MPA fallback threshold.
39+
// This depends on how well this feature is received in real projects, but also on what the threshold is based on: number of bundles needed to be preloaded for the next route, or the size of these bundles.
40+
// In the future, we might be able to speed up SPA so much that falling back to MPA will never make sense.
41+
const fallbackToMpa = untrack(() => Boolean(fallbackToMpaProp ?? true));
42+
3543
const clientNavPath = untrack(() => getClientNavPath({ ...linkProps, reload }, loc));
3644
linkProps.href = clientNavPath || originalHref;
3745

@@ -68,7 +76,9 @@ export const Link = component$<LinkProps>((props) => {
6876
const preventDefault = clientNavPath
6977
? sync$((event: MouseEvent, target: HTMLAnchorElement) => {
7078
if (!(event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)) {
71-
setMpaFallbackHref(target.href);
79+
if (fallbackToMpa) {
80+
setMpaFallbackHref(target.href);
81+
}
7282
event.preventDefault();
7383
}
7484
})
@@ -86,7 +96,9 @@ export const Link = component$<LinkProps>((props) => {
8696
await nav(elm.href, { forceReload: reload, replaceState, scroll });
8797
elm.removeAttribute('aria-pressed');
8898
}
89-
setMpaFallbackHref(null);
99+
if (fallbackToMpa) {
100+
setMpaFallbackHref(null);
101+
}
90102
}
91103
})
92104
: undefined;
@@ -168,4 +180,11 @@ export interface LinkProps extends AnchorAttributes {
168180
reload?: boolean;
169181
replaceState?: boolean;
170182
scroll?: boolean;
183+
184+
/**
185+
* **Defaults to _true_.**
186+
*
187+
* Whether Qwik should fallback to MPA navigation if too many bundles are queued for preloading.
188+
*/
189+
fallbackToMpa?: boolean;
171190
}

0 commit comments

Comments
 (0)