diff --git a/src/node/build/render.ts b/src/node/build/render.ts index ab479c29..ae851343 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -39,29 +39,43 @@ export async function renderPage( )) const pageData = JSON.parse(__pageData) - const preloadLinks = ( - config.mpa - ? appChunk - ? [appChunk.fileName] - : [] - : result && appChunk - ? [ - ...new Set([ - // resolve imports for index.js + page.md.js and inject script tags for - // them as well so we fetch everything as early as possible without having - // to wait for entry chunks to parse - ...resolvePageImports(config, page, result, appChunk), - pageClientJsFileName, - appChunk.fileName - ]) - ] + let preloadLinks = config.mpa + ? appChunk + ? [appChunk.fileName] : [] - ) + : result && appChunk + ? [ + ...new Set([ + // resolve imports for index.js + page.md.js and inject script tags for + // them as well so we fetch everything as early as possible without having + // to wait for entry chunks to parse + ...resolvePageImports(config, page, result, appChunk), + pageClientJsFileName, + appChunk.fileName + ]) + ] + : [] + + let prefetchLinks: string[] = [] + + const { shouldPreload } = config + if (shouldPreload) { + prefetchLinks = preloadLinks.filter((link) => !shouldPreload(link, page)) + preloadLinks = preloadLinks.filter((link) => shouldPreload(link, page)) + } + + const preloadLinksString = preloadLinks .map((file) => { return `` }) .join('\n ') + const prefetchLinkString = prefetchLinks + .map((file) => { + return `` + }) + .join('\n ') + const stylesheetLink = cssChunk ? `` : '' @@ -105,7 +119,8 @@ export async function renderPage( pageData.description || siteData.description }"> ${stylesheetLink} - ${preloadLinks} + ${preloadLinksString} + ${prefetchLinkString} ${renderHead(head)}
@@ -135,7 +150,7 @@ function resolvePageImports( appChunk: OutputChunk ) { // find the page's js chunk and inject script tags for its imports so that - // they are start fetching as early as possible + // they start fetching as early as possible const srcPath = normalizePath( fs.realpathSync(path.resolve(config.srcDir, page)) ) diff --git a/src/node/config.ts b/src/node/config.ts index 4fdc2117..d3ff1c70 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -47,6 +47,7 @@ export interface UserConfig