Skip to content

Commit

Permalink
prvide better error when prerendered routes conflict with each other -
Browse files Browse the repository at this point in the history
…closes #9560 (#9692)

Co-authored-by: Rich Harris <git@rich-harris.dev>
  • Loading branch information
Rich-Harris and Rich Harris committed Apr 17, 2023
1 parent 5e4820f commit 84a5250
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-items-speak.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: provide better error when prerendered routes conflict with each other
19 changes: 17 additions & 2 deletions packages/kit/src/core/postbuild/prerender.js
@@ -1,4 +1,4 @@
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { installPolyfills } from '../../exports/node/polyfills.js';
Expand Down Expand Up @@ -338,7 +338,22 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
}

if (response.status === 200) {
mkdirp(dirname(dest));
if (existsSync(dest) && statSync(dest).isDirectory()) {
throw new Error(
`Cannot save ${decoded} as it is already a directory. See https://kit.svelte.dev/docs/page-options#prerender-route-conflicts for more information`
);
}

const dir = dirname(dest);

if (existsSync(dir) && !statSync(dir).isDirectory()) {
const parent = decoded.split('/').slice(0, -1).join('/');
throw new Error(
`Cannot save ${decoded} as ${parent} is already a file. See https://kit.svelte.dev/docs/page-options#prerender-route-conflicts for more information`
);
}

mkdirp(dir);

log.info(`${response.status} ${decoded}`);
writeFileSync(dest, body);
Expand Down

0 comments on commit 84a5250

Please sign in to comment.