@@ -38,15 +38,26 @@ export function getKeyedLinksForMatches(
38
38
return dedupeLinkDescriptors ( descriptors , preloads ) ;
39
39
}
40
40
41
+ function getRouteCssDescriptors ( route : EntryRoute ) : HtmlLinkDescriptor [ ] {
42
+ if ( ! route . css ) return [ ] ;
43
+ return route . css . map ( ( href ) => ( { rel : "stylesheet" , href } ) ) ;
44
+ }
45
+
46
+ export async function prefetchRouteCss ( route : EntryRoute ) : Promise < void > {
47
+ if ( ! route . css ) return ;
48
+ let descriptors = getRouteCssDescriptors ( route ) ;
49
+ await Promise . all ( descriptors . map ( prefetchStyleLink ) ) ;
50
+ }
51
+
41
52
export async function prefetchStyleLinks (
42
53
route : EntryRoute ,
43
54
routeModule : RouteModule
44
55
) : Promise < void > {
45
56
if ( ( ! route . css && ! routeModule . links ) || ! isPreloadSupported ( ) ) return ;
46
57
47
- let descriptors = [ ] ;
58
+ let descriptors : LinkDescriptor [ ] = [ ] ;
48
59
if ( route . css ) {
49
- descriptors . push ( ...route . css . map ( ( href ) => ( { rel : "stylesheet" , href } ) ) ) ;
60
+ descriptors . push ( ...getRouteCssDescriptors ( route ) ) ;
50
61
}
51
62
if ( routeModule . links ) {
52
63
descriptors . push ( ...routeModule . links ( ) ) ;
@@ -64,21 +75,24 @@ export async function prefetchStyleLinks(
64
75
}
65
76
}
66
77
67
- // don't block for non-matching media queries, or for stylesheets that are
68
- // already in the DOM (active route revalidations)
69
- let matchingLinks = styleLinks . filter (
70
- ( link ) =>
71
- ( ! link . media || window . matchMedia ( link . media ) . matches ) &&
72
- ! document . querySelector ( `link[rel="stylesheet"][href="${ link . href } "]` )
73
- ) ;
74
-
75
- await Promise . all ( matchingLinks . map ( prefetchStyleLink ) ) ;
78
+ await Promise . all ( styleLinks . map ( prefetchStyleLink ) ) ;
76
79
}
77
80
78
81
async function prefetchStyleLink (
79
82
descriptor : HtmlLinkDescriptor
80
83
) : Promise < void > {
81
84
return new Promise ( ( resolve ) => {
85
+ // don't prefetch non-matching media queries, or stylesheets that are
86
+ // already in the DOM (active route revalidations)
87
+ if (
88
+ ( descriptor . media && ! window . matchMedia ( descriptor . media ) . matches ) ||
89
+ document . querySelector (
90
+ `link[rel="stylesheet"][href="${ descriptor . href } "]`
91
+ )
92
+ ) {
93
+ return resolve ( ) ;
94
+ }
95
+
82
96
let link = document . createElement ( "link" ) ;
83
97
Object . assign ( link , descriptor ) ;
84
98
0 commit comments