From 46b445f8b21409540a66973be8fb693cce7812dc Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 9 Aug 2022 18:20:18 +0200 Subject: [PATCH] fix(prerender): prerender `x-nitro` links without crawlLinks option --- src/prerender.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/prerender.ts b/src/prerender.ts index ed3394c9e7..1db602e412 100644 --- a/src/prerender.ts +++ b/src/prerender.ts @@ -78,15 +78,11 @@ export async function prerender (nitro: Nitro) { await writeFile(filePath, _route.contents) // Crawl route links - if ( - !_route.error && - nitro.options.prerender.crawlLinks && - isImplicitHTML - ) { - const crawledRoutes = extractLinks(_route.contents, route, res) - for (const crawledRoute of crawledRoutes) { - if (canPrerender(crawledRoute)) { - routes.add(crawledRoute) + if (!_route.error && isImplicitHTML) { + const extractedLinks = extractLinks(_route.contents, route, res, nitro.options.prerender.crawlLinks) + for (const _link of extractedLinks) { + if (canPrerender(_link)) { + routes.add(_link) } } } @@ -114,16 +110,18 @@ export async function prerender (nitro: Nitro) { const LINK_REGEX = /href=['"]?([^'" >]+)/g -function extractLinks (html: string, from: string, res: Response) { +function extractLinks (html: string, from: string, res: Response, crawlLinks: boolean) { const links: string[] = [] const _links: string[] = [] - // Extract from any - _links.push( - ...Array.from(html.matchAll(LINK_REGEX)) - .map(m => m[1]) - .filter(link => allowedExtensions.has(getExtension(link))) - ) + // Extract from any to crawl + if (crawlLinks) { + _links.push( + ...Array.from(html.matchAll(LINK_REGEX)) + .map(m => m[1]) + .filter(link => allowedExtensions.has(getExtension(link))) + ) + } // Extract from x-nitro-prerender headers const header = res.headers.get('x-nitro-prerender') || ''