@@ -39,29 +39,43 @@ export async function renderPage(
3939 ) )
4040 const pageData = JSON . parse ( __pageData )
4141
42- const preloadLinks = (
43- config . mpa
44- ? appChunk
45- ? [ appChunk . fileName ]
46- : [ ]
47- : result && appChunk
48- ? [
49- ...new Set ( [
50- // resolve imports for index.js + page.md.js and inject script tags for
51- // them as well so we fetch everything as early as possible without having
52- // to wait for entry chunks to parse
53- ...resolvePageImports ( config , page , result , appChunk ) ,
54- pageClientJsFileName ,
55- appChunk . fileName
56- ] )
57- ]
42+ let preloadLinks = config . mpa
43+ ? appChunk
44+ ? [ appChunk . fileName ]
5845 : [ ]
59- )
46+ : result && appChunk
47+ ? [
48+ ...new Set ( [
49+ // resolve imports for index.js + page.md.js and inject script tags for
50+ // them as well so we fetch everything as early as possible without having
51+ // to wait for entry chunks to parse
52+ ...resolvePageImports ( config , page , result , appChunk ) ,
53+ pageClientJsFileName ,
54+ appChunk . fileName
55+ ] )
56+ ]
57+ : [ ]
58+
59+ let prefetchLinks : string [ ] = [ ]
60+
61+ const { shouldPreload } = config
62+ if ( shouldPreload ) {
63+ prefetchLinks = preloadLinks . filter ( ( link ) => ! shouldPreload ( link , page ) )
64+ preloadLinks = preloadLinks . filter ( ( link ) => shouldPreload ( link , page ) )
65+ }
66+
67+ const preloadLinksString = preloadLinks
6068 . map ( ( file ) => {
6169 return `<link rel="modulepreload" href="${ siteData . base } ${ file } ">`
6270 } )
6371 . join ( '\n ' )
6472
73+ const prefetchLinkString = prefetchLinks
74+ . map ( ( file ) => {
75+ return `<link rel="prefetch" href="${ siteData . base } ${ file } ">`
76+ } )
77+ . join ( '\n ' )
78+
6579 const stylesheetLink = cssChunk
6680 ? `<link rel="stylesheet" href="${ siteData . base } ${ cssChunk . fileName } ">`
6781 : ''
@@ -105,7 +119,8 @@ export async function renderPage(
105119 pageData . description || siteData . description
106120 } ">
107121 ${ stylesheetLink }
108- ${ preloadLinks }
122+ ${ preloadLinksString }
123+ ${ prefetchLinkString }
109124 ${ renderHead ( head ) }
110125 </head>
111126 <body>
@@ -135,7 +150,7 @@ function resolvePageImports(
135150 appChunk : OutputChunk
136151) {
137152 // find the page's js chunk and inject script tags for its imports so that
138- // they are start fetching as early as possible
153+ // they start fetching as early as possible
139154 const srcPath = normalizePath (
140155 fs . realpathSync ( path . resolve ( config . srcDir , page ) )
141156 )
0 commit comments