-
-
Notifications
You must be signed in to change notification settings - Fork 881
fix(prerender): warn and keep first output when two routes collide #4491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sobol-sudo
wants to merge
2
commits into
nitrojs:main
Choose a base branch
from
sobol-sudo:fix/prerender-output-collision
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
β1
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,54 @@ | ||
| import { mkdir, readFile, rm } from "node:fs/promises"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { join } from "pathe"; | ||
| import { build, copyPublicAssets, createNitro, prepare, prerender } from "nitro/builder"; | ||
| import { afterAll, describe, expect, it } from "vitest"; | ||
|
|
||
| const fixtureDir = fileURLToPath(new URL("./fixture", import.meta.url)); | ||
| const tmpDir = fileURLToPath(new URL("./.tmp", import.meta.url)); | ||
|
|
||
| describe("prerender output collision", () => { | ||
| afterAll(async () => { | ||
| await rm(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("keeps the first route's output and warns instead of overwriting it", async () => { | ||
| const outDir = join(tmpDir, "output"); | ||
| await rm(outDir, { recursive: true, force: true }); | ||
| await mkdir(outDir, { recursive: true }); | ||
|
|
||
| const nitro = await createNitro({ | ||
| rootDir: fixtureDir, | ||
| preset: "static", | ||
| output: { dir: outDir }, | ||
| prerender: { | ||
| crawlLinks: false, | ||
| // both resolve to `other/index.html` | ||
| routes: ["/other", "/other/index.html"], | ||
| }, | ||
| }); | ||
|
|
||
| const warnings: string[] = []; | ||
| nitro.logger.warn = ((...args: unknown[]) => { | ||
| warnings.push(args.map(String).join(" ")); | ||
| }) as typeof nitro.logger.warn; | ||
|
|
||
| await prepare(nitro); | ||
| await copyPublicAssets(nitro); | ||
| await prerender(nitro); | ||
| await build(nitro); | ||
| await nitro.close(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| const collisionWarnings = warnings.filter((w) => w.includes("both prerender to")); | ||
| expect(collisionWarnings).toHaveLength(1); | ||
| expect(collisionWarnings[0]).toContain("other/index.html"); | ||
|
|
||
| // only one of the two routes may claim the file | ||
| const written = nitro._prerenderedRoutes!.filter((r) => r.fileName === "/other/index.html"); | ||
| expect(written).toHaveLength(1); | ||
|
|
||
| // and the file holds that route's render, whole | ||
| const contents = await readFile(join(outDir, "public/other/index.html"), "utf8"); | ||
| expect(contents).toBe(`<!DOCTYPE html><html><body>rendered ${written[0].route}</body></html>`); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| }, 120_000); | ||
| }); | ||
This file contains hidden or 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,8 @@ | ||
| export default { | ||
| fetch(req: Request) { | ||
| const { pathname } = new URL(req.url); | ||
| return new Response(`<!DOCTYPE html><html><body>rendered ${pathname}</body></html>`, { | ||
| headers: { "content-type": "text/html" }, | ||
| }); | ||
| }, | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.