Skip to content

Commit

Permalink
skip fallback generation if prerendering is disabled - fixes #4492
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 13, 2022
1 parent 68f3650 commit fc6d9f9
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/kit/src/core/build/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export async function prerender({ config, entries, files, log }) {
paths: []
};

if (!config.kit.prerender.enabled) {
return prerendered;
}

installFetch();

const server_root = join(config.kit.outDir, 'output');
Expand Down Expand Up @@ -84,10 +88,6 @@ export async function prerender({ config, entries, files, log }) {
mkdirp(dirname(file));
writeFileSync(file, await rendered.text());

if (!config.kit.prerender.enabled) {
return prerendered;
}

const error = normalise_error_handler(log, config.kit.prerender.onError);

const q = queue(config.kit.prerender.concurrency);
Expand Down
20 changes: 20 additions & 0 deletions packages/kit/test/prerendering/disabled/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "prerendering-test-basics",
"private": true,
"version": "0.0.2-next.0",
"scripts": {
"dev": "node ../../cli.js dev",
"build": "node ../../cli.js build",
"preview": "node ../../cli.js preview",
"check": "tsc && svelte-check",
"test": "npm run build"
},
"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/disabled/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>
1 change: 1 addition & 0 deletions packages/kit/test/prerendering/disabled/src/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('this file should not be loaded if prerendering is disabled');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>not prerendered</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "answer": 42 }
27 changes: 27 additions & 0 deletions packages/kit/test/prerendering/disabled/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path';
import adapter from '../../../../adapter-static/index.js';

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

prerender: {
enabled: false
},

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

export default config;
117 changes: 117 additions & 0 deletions packages/kit/test/prerendering/disabled/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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 /', () => {
const content = read('index.html');
assert.ok(content.includes('<h1>hello</h1>'));
});

test('renders a redirect', () => {
const content = read('redirect.html');
assert.equal(
content,
'<meta http-equiv="refresh" content="0;url=https://example.com/redirected">'
);
});

test('does not double-encode redirect locations', () => {
const content = read('redirect-encoded.html');
assert.equal(
content,
'<meta http-equiv="refresh" content="0;url=https://example.com/redirected?returnTo=%2Ffoo%3Fbar%3Dbaz">'
);
});

test('escapes characters in redirect', () => {
const content = read('redirect-malicious.html');
assert.equal(
content,
'<meta http-equiv="refresh" content="0;url=https://example.com/</script>alert(&quot;pwned&quot;)">'
);
});

test('inserts http-equiv tag for cache-control headers', () => {
const content = read('max-age.html');
assert.ok(content.includes('<meta http-equiv="cache-control" content="max-age=300">'));
});

test('renders page with data from endpoint', () => {
const content = read('fetch-endpoint/buffered.html');
assert.ok(content.includes('<h1>the answer is 42</h1>'));

const json = read('fetch-endpoint/buffered.json');
assert.equal(json, JSON.stringify({ answer: 42 }));
});

test('renders page with unbuffered data from endpoint', () => {
const content = read('fetch-endpoint/not-buffered.html');
assert.ok(content.includes('<h1>content-type: application/json; charset=utf-8</h1>'), content);

const json = read('fetch-endpoint/not-buffered.json');
assert.equal(json, JSON.stringify({ answer: 42 }));
});

test('loads a file with spaces in the filename', () => {
const content = read('load-file-with-spaces.html');
assert.ok(content.includes('<h1>answer: 42</h1>'), content);
});

test('generates __data.json file for shadow endpoints', () => {
assert.equal(read('__data.json'), JSON.stringify({ message: 'hello' }));
assert.equal(read('shadowed-get/__data.json'), JSON.stringify({ answer: 42 }));
});

test('does not prerender page with shadow endpoint with non-GET handler', () => {
assert.ok(!fs.existsSync(`${build}/shadowed-post.html`));
assert.ok(!fs.existsSync(`${build}/shadowed-post/__data.json`));
});

test('decodes paths when writing files', () => {
let content = read('encoding/path with spaces.html');
assert.ok(content.includes('<p id="a">path with spaces</p>'));
assert.ok(content.includes('<p id="b">path with encoded spaces</p>'));

content = read('encoding/dynamic path with spaces.html');
assert.ok(
content.includes('<h1>dynamic path with spaces / /encoding/dynamic%20path%20with%20spaces</h1>')
);

content = read('encoding/dynamic path with encoded spaces.html');
assert.ok(
content.includes(
'<h1>dynamic path with encoded spaces / /encoding/dynamic%20path%20with%20encoded%20spaces</h1>'
)
);

content = read('encoding/redirected path with encoded spaces.html');
assert.ok(
content.includes(
'<h1>redirected path with encoded spaces / /encoding/redirected%20path%20with%20encoded%20spaces</h1>'
)
);

content = read('encoding/path with spaces.json');
assert.equal(content, JSON.stringify({ path: 'path with spaces' }));

content = read('encoding/path with encoded spaces.json');
assert.equal(content, JSON.stringify({ path: 'path with encoded spaces' }));
});

test('prerendering is set to true in global code of hooks.js', () => {
const content = read('prerendering-true.html');
assert.ok(content.includes('<h1>prerendering: true/true</h1>'), content);
});

test('fetching missing content results in a 404', () => {
const content = read('fetch-404.html');
assert.ok(content.includes('<h1>status: 404</h1>'), content);
});

test.run();
14 changes: 14 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 fc6d9f9

Please sign in to comment.