Skip to content

Commit

Permalink
only skip files that were already written when prerendering (#4928)
Browse files Browse the repository at this point in the history
* only skip files that were already written when prerendering - fixes #4927

* update lockfile
  • Loading branch information
Rich-Harris authored May 15, 2022
1 parent 3d34924 commit ae416be
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/wise-wombats-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

only skip files that were already written when prerendering
4 changes: 3 additions & 1 deletion packages/kit/src/core/build/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export async function prerender({ config, entries, files, log }) {
const dest = `${config.kit.outDir}/output/prerendered/${category}/${file}`;

if (written.has(file)) return;
written.add(file);

if (response_type === REDIRECT) {
const location = response.headers.get('location');
Expand All @@ -216,6 +215,8 @@ export async function prerender({ config, entries, files, log }) {
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
);

written.add(file);

if (!prerendered.redirects.has(decoded)) {
prerendered.redirects.set(decoded, {
status: response.status,
Expand All @@ -237,6 +238,7 @@ export async function prerender({ config, entries, files, log }) {

log.info(`${response.status} ${decoded}`);
writeFileSync(dest, body);
written.add(file);

if (is_html) {
prerendered.pages.set(decoded, {
Expand Down
20 changes: 20 additions & 0 deletions packages/kit/test/prerendering/paths-base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "prerendering-test-paths-base",
"private": true,
"version": "0.0.1",
"scripts": {
"dev": "node ../../cli.js dev",
"build": "node ../../cli.js build --verbose",
"preview": "node ../../cli.js preview",
"check": "tsc && svelte-check",
"test": "npm run build && uvu test"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"svelte": "^3.43.0",
"svelte-check": "^2.5.0",
"typescript": "~4.6.2",
"uvu": "^0.5.2"
},
"type": "module"
}
11 changes: 11 additions & 0 deletions packages/kit/test/prerendering/paths-base/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
%svelte.body%
</body>
</html>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { page } from '$app/stores';
</script>

<h1>{$page.params.slug}</h1>
10 changes: 10 additions & 0 deletions packages/kit/test/prerendering/paths-base/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script context="module">
import { base } from '$app/paths';
export function load() {
return {
status: 301,
redirect: `${base}/dynamic/foo`
};
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>nested hello</h1>
31 changes: 31 additions & 0 deletions packages/kit/test/prerendering/paths-base/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import path from 'path';
import adapter from '../../../../adapter-static/index.js';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),

paths: {
base: '/path-base',
},

prerender: {
default: true
},

vite: {
build: {
minify: false
},
clearScreen: false,
server: {
fs: {
allow: [path.resolve('../../../src')]
}
}
}
}
};

export default config;
21 changes: 21 additions & 0 deletions packages/kit/test/prerendering/paths-base/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs';
import { fileURLToPath } from 'url';
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const build = fileURLToPath(new URL('../build', import.meta.url));

/** @param {string} file */
const read = (file) => fs.readFileSync(`${build}/${file}`, 'utf-8');

test('prerenders /path-base', () => {
const content = read('index.html');
assert.equal(content, '<meta http-equiv="refresh" content="0;url=/path-base/dynamic/foo">');
});

test('prerenders /path-base/dynamic/foo', () => {
const content = read('dynamic/foo.html');
assert.ok(content.includes('<h1>foo</h1>'));
});

test.run();
11 changes: 11 additions & 0 deletions packages/kit/test/prerendering/paths-base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"noEmit": true,
"paths": {
"$lib": ["src/lib"],
"$lib/*": ["src/lib/*"],
"types": ["../../../types/internal"]
}
},
"extends": "./.svelte-kit/tsconfig.json"
}
93 changes: 93 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ae416be

Please sign in to comment.