-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
108 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { defineNitroConfig } from '../src' | ||
|
||
export default defineNitroConfig({ | ||
renderer: './app', | ||
prerender: { | ||
routes: [ | ||
'/api/test', | ||
'/api/cache' | ||
] | ||
crawlLinks: true | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { resolve, join } from 'pathe' | ||
import { createNitro } from './nitro' | ||
import { build } from './build' | ||
import type { Nitro } from './types' | ||
import { writeFile } from './utils' | ||
|
||
const CRAWL_EXTENSIONS = new Set(['', '.html', '.json']) | ||
|
||
export async function prerender (nitro: Nitro) { | ||
// Skip if no prerender routes specified | ||
const routes = new Set(nitro.options.prerender.routes) | ||
if (nitro.options.prerender.crawlLinks && !routes.size) { | ||
routes.add('/') | ||
} | ||
if (!routes.size) { | ||
return | ||
} | ||
// Build with prerender preset | ||
nitro = await createNitro({ | ||
rootDir: nitro.options.rootDir, | ||
preset: 'prerender' | ||
}) | ||
await build(nitro) | ||
|
||
// Import renderer entry | ||
const app = await import(resolve(nitro.options.output.serverDir, 'index.mjs')) | ||
|
||
// Start prerendering | ||
const generatedRoutes = new Set() | ||
const generateRoute = async (route: string) => { | ||
const res = await app.localFetch(route) | ||
const contents = await res.text() | ||
|
||
const additionalExtension = getExtension(route) ? '' : guessExt(res.headers.get('content-type')) | ||
const routeWithIndex = route.endsWith('/') ? route + 'index' : route | ||
const fileName = routeWithIndex + additionalExtension | ||
await writeFile(join(nitro.options.output.publicDir, fileName), contents, true) | ||
|
||
// Crawl Links | ||
if (nitro.options.prerender.crawlLinks && fileName.endsWith('.html')) { | ||
const extractedLinks = extractLinks(contents, route) | ||
.filter(link => | ||
!generatedRoutes.has(link) && | ||
link.match(/^\//g) && | ||
CRAWL_EXTENSIONS.has(getExtension(link)) | ||
) | ||
for (const route of extractedLinks) { routes.add(route) } | ||
} | ||
} | ||
|
||
const generateAll = async () => { | ||
const results = await Promise.all(Array.from(routes).map(async (route) => { | ||
if (generatedRoutes.has(route)) { return false } | ||
generatedRoutes.add(route) | ||
await generateRoute(route).catch(console.error) | ||
return true | ||
})) | ||
return results.filter(Boolean).length // At least one route generated | ||
} | ||
|
||
for (let i = 0; i < 100; i++) { | ||
const routesGenerated = await generateAll() | ||
if (!routesGenerated) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
const LINK_REGEX = /href=['"]?([^'" >]+)/g | ||
function extractLinks (html: string, _url: string) { | ||
const links: string[] = [] | ||
for (const match of html.matchAll(LINK_REGEX)) { | ||
links.push(match[1]) | ||
} | ||
return links | ||
} | ||
|
||
const EXT_REGEX = /\.[a-z0-9]+$/ | ||
function getExtension (path: string): string { | ||
return (path.match(EXT_REGEX) || [])[0] || '' | ||
} | ||
|
||
const ContentTypeToExt = { | ||
json: '.json', | ||
html: '.html' | ||
} | ||
function guessExt (contentType: string = ''): string { | ||
for (const pattern in ContentTypeToExt) { | ||
if (contentType.includes(pattern)) { | ||
return ContentTypeToExt[pattern] | ||
} | ||
} | ||
return '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
import type { NitroPreset } from './types' | ||
|
||
export function defineNitroPreset (preset: NitroPreset) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ export interface NitroOptions { | |
}, | ||
|
||
prerender: { | ||
crawlLinks: boolean | ||
routes: string[] | ||
}, | ||
|
||
|