Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 22, 2022
2 parents 72bb086 + b875d5b commit fa47b14
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-mayflies-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Error if handle hook returns something other than a Response
5 changes: 5 additions & 0 deletions .changeset/modern-sloths-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Insert <meta http-equiv> cache-control header when prerendering
2 changes: 2 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"brave-seas-invent",
"brave-turkeys-bathe",
"brave-weeks-allow",
"breezy-games-count",
"breezy-sheep-dress",
"breezy-students-refuse",
"bright-cherries-hug",
Expand Down Expand Up @@ -320,6 +321,7 @@
"modern-dryers-join",
"modern-moose-greet",
"modern-news-rhyme",
"modern-sloths-greet",
"moody-laws-draw",
"moody-queens-sell",
"moody-scissors-love",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/smooth-comics-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fixed prerendering with base path configured
6 changes: 3 additions & 3 deletions documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ You can add call multiple `handle` functions with [the `sequence` helper functio

```js
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, resolve }) {
const response = await resolve(request, {
ssr: !request.url.pathname.startsWith('/admin')
export async function handle({ event, resolve }) {
const response = await resolve(event, {
ssr: !event.url.pathname.startsWith('/admin')
});

return response;
Expand Down
7 changes: 5 additions & 2 deletions documentation/migrating/99-integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ const minification_options = {
export async function handle({ event, resolve }) {
const response = await resolve(event);

if (prerendering && response.headers['content-type'] === 'text/html') {
response.body = minify(response.body, minification_options);
if (prerendering && response.headers.get('content-type') === 'text/html') {
return new Response(minify(await response.text(), minification_options), {
status: response.status,
headers: response.headers
});
}

return response;
Expand Down
12 changes: 12 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @sveltejs/kit

## 1.0.0-next.239

### Patch Changes

- Insert <meta http-equiv> cache-control header when prerendering ([#3493](/~https://github.com/sveltejs/kit/pull/3493))

## 1.0.0-next.238

### Patch Changes

- Escape prerendered redirect locations, instead of encoding them ([#3456](/~https://github.com/sveltejs/kit/pull/3456))

## 1.0.0-next.237

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.237",
"version": "1.0.0-next.239",
"repository": {
"type": "git",
"url": "/~https://github.com/sveltejs/kit",
Expand Down
5 changes: 4 additions & 1 deletion packages/kit/src/core/adapt/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
async function visit(path, decoded_path, referrer) {
/** @type {Map<string, Response>} */
const dependencies = new Map();
const render_path = config.kit.paths?.base
? `http://sveltekit-prerender${config.kit.paths.base}${path === '/' ? '' : path}`
: `http://sveltekit-prerender${path}`;

const rendered = await app.render(new Request(`http://sveltekit-prerender${path}`), {
const rendered = await app.render(new Request(render_path), {
prerender: {
all,
dependencies
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ export async function create_plugin(config, cwd) {
target: config.kit.target,
template: ({ head, body, assets }) => {
let rendered = load_template(cwd, config)
.replace(/%svelte\.assets%/g, assets)
// head and body must be replaced last, in case someone tries to sneak in %svelte.assets% etc
.replace('%svelte.head%', () => head)
.replace('%svelte.body%', () => body)
.replace(/%svelte\.assets%/g, assets);
.replace('%svelte.body%', () => body);

if (amp) {
const result = amp.validateString(rendered);
Expand Down
9 changes: 8 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function respond(request, options, state = {}) {
let ssr = true;

try {
return await options.hooks.handle({
const response = await options.hooks.handle({
event,
resolve: async (event, opts) => {
if (opts && 'ssr' in opts) ssr = /** @type {boolean} */ (opts.ssr);
Expand Down Expand Up @@ -195,6 +195,13 @@ export async function respond(request, options, state = {}) {
throw new Error('request in handle has been replaced with event' + details);
}
});

// TODO for 1.0, change the error message to point to docs rather than PR
if (response && !(response instanceof Response)) {
throw new Error('handle must return a Response object' + details);
}

return response;
} catch (/** @type {unknown} */ e) {
const error = coalesce_to_error(e);

Expand Down
6 changes: 6 additions & 0 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export async function render_response({

const inlined_style = Array.from(styles.values()).join('\n');

if (state.prerender) {
if (maxage) {
head += `<meta http-equiv="cache-control" content="max-age=${maxage}">`;
}
}

if (options.amp) {
head += `
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
Expand Down
10 changes: 10 additions & 0 deletions packages/kit/test/prerendering/basics/src/routes/max-age.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export function load() {
return {
maxage: 300
};
}
</script>

<h1>This page will be cached for 5 minutes</h1>
5 changes: 5 additions & 0 deletions packages/kit/test/prerendering/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ test('escapes characters in redirect', () => {
);
});

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

test.run();
17 changes: 17 additions & 0 deletions packages/kit/test/prerendering/options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "prerendering-test-options",
"private": true,
"version": "0.0.1",
"scripts": {
"dev": "node ../../cli.js dev",
"build": "node ../../cli.js build",
"preview": "node ../../cli.js preview",
"test": "npm run build && uvu test"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"svelte": "^3.43.0",
"uvu": "^0.5.2"
},
"type": "module"
}
11 changes: 11 additions & 0 deletions packages/kit/test/prerendering/options/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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>hello</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>nested hello</h1>
26 changes: 26 additions & 0 deletions packages/kit/test/prerendering/options/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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',
assets: 'https://cdn.example.com/stuff'
},
vite: {
build: {
minify: false
},
clearScreen: false,
server: {
fs: {
allow: [path.resolve('../../../src')]
}
}
}
}
};

export default config;
23 changes: 23 additions & 0 deletions packages/kit/test/prerendering/options/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.ok(content.includes('<h1>hello</h1>'));
assert.ok(content.includes('http://sveltekit-prerender/path-base'));
});

test('prerenders nested /path-base', () => {
const content = read('/nested/index.html');
assert.ok(content.includes('<h1>nested hello</h1>'));
assert.ok(content.includes('http://sveltekit-prerender/path-base/nested'));
});

test.run();

0 comments on commit fa47b14

Please sign in to comment.