Skip to content

Commit

Permalink
fix(prerender): extract links from explicit html routes (#2517)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jun 13, 2024
1 parent 9efa3a5 commit e1f87c5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/prerender/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export async function prerender(nitro: Nitro) {
}

// Crawl route links
if (!_route.error && isImplicitHTML) {
if (!_route.error && (isImplicitHTML || route.endsWith(".html"))) {
const extractedLinks = extractLinks(
dataBuff.toString("utf8"),
route,
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default defineNitroConfig({
ignore: [
// '/api/param/'
],
routes: ["/prerender", "/404"],
routes: ["/prerender", "/prerender-custom.html", "/404"],
},
experimental: {
openAPI: true,
Expand Down
17 changes: 17 additions & 0 deletions test/fixture/routes/prerender-custom.html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default defineEventHandler((event) => {
const links = ["/api/hello", "/api/param/foo.json", "/api/param/foo.css"];

return `<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Prerendered routes test</title>
</head>
<body>
<h1>Prerendered routes test #2:</h1>
<ul>
${links.map((link) => ` <li><a href="${link}">${link}</a></li>`).join("\n")}
</ul>
</body>
</html>`;
});
3 changes: 0 additions & 3 deletions test/fixture/routes/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ export default defineEventHandler((event) => {
const links = [
"/404",
"https://about.google/products/",
"/api/hello",
"/api/hello?bar=baz",
"/api/hello?bar=baz&bing=bap",
"/api/hello?bar=baz&amp;foo=qux",
"/prerender#foo",
"../api/hey",
"/api/param/foo.json",
"/api/param/foo.css",
"/json-string",
event.path.includes("?") ? "/api/param/hidden" : "/prerender?withQuery",
];
Expand Down
4 changes: 4 additions & 0 deletions test/presets/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ describe(
"redirect": "/.auth/logout",
"route": "/logout",
},
{
"rewrite": "/prerender-custom.html",
"route": "/prerender-custom",
},
{
"rewrite": "/api/hey/index.html",
"route": "/api/hey",
Expand Down
1 change: 1 addition & 0 deletions test/presets/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe.skipIf(isWindows)("nitro:preset:cloudflare-pages", async () => {
"/favicon.ico",
"/json-string",
"/prerender",
"/prerender-custom",
"/api/hello",
"/api/hey",
"/prerender/index.html.br",
Expand Down

0 comments on commit e1f87c5