Skip to content

Commit

Permalink
fix(prerender): prerender x-nitro links without crawlLinks option
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 9, 2022
1 parent fbd4290 commit 46b445f
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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 <TAG href="">
_links.push(
...Array.from(html.matchAll(LINK_REGEX))
.map(m => m[1])
.filter(link => allowedExtensions.has(getExtension(link)))
)
// Extract from any <TAG href=""> 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') || ''
Expand Down

0 comments on commit 46b445f

Please sign in to comment.