@@ -13,8 +13,10 @@ import {
13
13
import { getClientNavPath , shouldPreload } from './utils' ;
14
14
import { loadClientData } from './use-endpoint' ;
15
15
import { useLocation , useNavigate } from './use-functions' ;
16
- import { prefetchSymbols } from './client-navigate' ;
16
+ import { preloadRouteBundles } from './client-navigate' ;
17
17
import { isDev } from '@builder.io/qwik' ;
18
+ // @ts -expect-error we don't have types for the preloader yet
19
+ import { p as preload } from '@builder.io/qwik/preloader' ;
18
20
19
21
/** @public */
20
22
export const Link = component$ < LinkProps > ( ( props ) => {
@@ -51,28 +53,30 @@ export const Link = component$<LinkProps>((props) => {
51
53
52
54
if ( elm && elm . href ) {
53
55
const url = new URL ( elm . href ) ;
54
- prefetchSymbols ( url . pathname ) ;
56
+ preloadRouteBundles ( url . pathname ) ;
55
57
56
58
if ( elm . hasAttribute ( 'data-prefetch' ) ) {
57
59
loadClientData ( url , elm , {
58
- prefetchSymbols : false ,
60
+ preloadRouteBundles : false ,
59
61
isPrefetch : true ,
60
62
} ) ;
61
63
}
62
64
}
63
65
} )
64
66
: undefined ;
67
+
65
68
const preventDefault = clientNavPath
66
69
? sync$ ( ( event : MouseEvent , target : HTMLAnchorElement ) => {
67
70
if ( ! ( event . metaKey || event . ctrlKey || event . shiftKey || event . altKey ) ) {
68
71
event . preventDefault ( ) ;
69
72
}
70
73
} )
71
74
: undefined ;
72
- const handleClick = clientNavPath
75
+
76
+ const handleClientSideNavigation = clientNavPath
73
77
? $ ( async ( event : Event , elm : HTMLAnchorElement ) => {
74
78
if ( event . defaultPrevented ) {
75
- // If default was prevented, than it is up to us to make client side navigation.
79
+ // If default was prevented, then it is up to us to make client side navigation.
76
80
if ( elm . hasAttribute ( 'q:nbs' ) ) {
77
81
// Allow bootstrapping into useNavigate.
78
82
await nav ( location . href , { type : 'popstate' } ) ;
@@ -85,6 +89,11 @@ export const Link = component$<LinkProps>((props) => {
85
89
} )
86
90
: undefined ;
87
91
92
+ const handlePreload = $ ( ( _ : any , elm : HTMLAnchorElement ) => {
93
+ const url = new URL ( elm . href ) ;
94
+ preloadRouteBundles ( url . pathname , 1 ) ;
95
+ } ) ;
96
+
88
97
useVisibleTask$ ( ( { track } ) => {
89
98
track ( ( ) => loc . url . pathname ) ;
90
99
// We need to trigger the onQVisible$ in the visible task for it to fire on subsequent route navigations
@@ -115,7 +124,12 @@ export const Link = component$<LinkProps>((props) => {
115
124
// Attr 'q:link' is used as a selector for bootstrapping into spa after context loss
116
125
{ ...{ 'q:link' : ! ! clientNavPath } }
117
126
{ ...linkProps }
118
- onClick$ = { [ preventDefault , onClick$ , handleClick ] }
127
+ onClick$ = { [
128
+ preventDefault ,
129
+ handlePreload , // needs to be in between preventDefault and onClick$ to ensure it starts asap.
130
+ onClick$ ,
131
+ handleClientSideNavigation ,
132
+ ] }
119
133
data-prefetch = { prefetchData }
120
134
onMouseOver$ = { [ linkProps . onMouseOver$ , handlePrefetch ] }
121
135
onFocus$ = { [ linkProps . onFocus$ , handlePrefetch ] }
0 commit comments