Skip to content

Commit e55bc44

Browse files
authored
[fix] revert change to rendering options (#2008) (#2047)
1 parent 958d8dd commit e55bc44

File tree

16 files changed

+158
-192
lines changed

16 files changed

+158
-192
lines changed

.changeset/chilled-bees-develop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[fix] revert change to rendering options (#2008)

documentation/docs/11-ssr-and-javascript.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ By default, SvelteKit will render any component first on the server and send it
66

77
You can control each of these on a per-app or per-page basis. Note that each of the per-page settings use [`context="module"`](https://svelte.dev/docs#script_context_module), and only apply to page components, _not_ [layout](#layouts) components.
88

9-
The app-wide config options take a function, which lets you set configure the option in an advanced manner on a per-request and per-page basis. E.g. you could disable SSR for `/admin` or enable SSR only for search engine crawlers (aka [dynamic rendering](https://developers.google.com/search/docs/advanced/javascript/dynamic-rendering)).
10-
11-
Each setting can be controlled independently, but `ssr` and `hydrate` cannot both be `false` since that would result in nothing being rendered at all.
9+
If both are specified, per-page settings override per-app settings in case of conflicts. Each setting can be controlled independently, but `ssr` and `hydrate` cannot both be `false` since that would result in nothing being rendered at all.
1210

1311
### ssr
1412

15-
Disabling [server-side rendering](#appendix-ssr) effectively turns your SvelteKit app into a [**single-page app** or SPA](#appendix-csr-and-spa). The default app-wide config option value is a function which reads the page value. Reading the page value causes the page to be loaded on the server. If you'd like to avoid this because you're building a SPA, you will need to set a value such as a boolean for each of the four rendering options which does not access the page-level settings.
13+
Disabling [server-side rendering](#appendix-ssr) effectively turns your SvelteKit app into a [**single-page app** or SPA](#appendix-csr-and-spa).
1614

1715
> In most situations this is not recommended: see [the discussion in the appendix](#appendix-ssr). Consider whether it's truly appropriate to disable and don't simply disable SSR because you've hit an issue with it.
1816
19-
SSR can be configured with app-wide [`ssr` config option](#configuration-ssr), or a page-level `ssr` export:
17+
You can disable SSR app-wide with the [`ssr` config option](#configuration-ssr), or a page-level `ssr` export:
2018

2119
```html
2220
<script context="module">
@@ -28,7 +26,7 @@ SSR can be configured with app-wide [`ssr` config option](#configuration-ssr), o
2826

2927
SvelteKit includes a [client-side router](#appendix-routing) that intercepts navigations (from the user clicking on links, or interacting with the back/forward buttons) and updates the page contents, rather than letting the browser handle the navigation by reloading.
3028

31-
In certain circumstances you might need to configure [client-side routing](#appendix-routing) with the app-wide [`router` config option](#configuration-router) or the page-level `router` export:
29+
In certain circumstances you might need to disable [client-side routing](#appendix-routing) with the app-wide [`router` config option](#configuration-router) or the page-level `router` export:
3230

3331
```html
3432
<script context="module">

documentation/docs/14-configuration.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ const config = {
2828
floc: false,
2929
host: null,
3030
hostHeader: null,
31-
// hydrate unless disabled on page
32-
hydrate: async ({ page }) => {
33-
const leaf = await page;
34-
return 'hydrate' in leaf ? !!leaf.hydrate : true;
35-
},
31+
hydrate: true,
3632
package: {
3733
dir: 'package',
3834
emitTypes: true,
@@ -51,24 +47,15 @@ const config = {
5147
},
5248
prerender: {
5349
crawl: true,
54-
// don't prerender unless enabled on page
55-
enabled: expect_page_scriptable(async ({ page }) => !!(await page).prerender),
50+
enabled: true,
5651
onError: 'fail',
5752
pages: ['*']
5853
},
59-
// route unless disabled on page
60-
router: async ({ page }) => {
61-
const leaf = await page;
62-
return 'router' in leaf ? !!leaf.router : true;
63-
},
54+
router: true,
6455
serviceWorker: {
6556
exclude: []
6657
},
67-
// do SSR unless disabled on page
68-
ssr: async ({ page }) => {
69-
const leaf = await page;
70-
return 'ssr' in leaf ? !!leaf.ssr : true;
71-
},
58+
ssr: true,
7259
target: null,
7360
trailingSlash: 'never',
7461
vite: () => ({})

documentation/faq/80-integrations.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Please use SDK v9 which provides a modular SDK approach that's currently in beta
1818

1919
### How do I use a client-side only library that depends on `document` or `window`?
2020

21-
Vite will attempt to process all imported libraries and may fail when encountering a library that is not compatible with SSR. This occurs even when SSR is disabled unless you also override the default values for `hydrate`, `router`, and `prerender.enabled` [as mentioned in the docs](docs#ssr-and-javascript-ssr).
21+
Vite will attempt to process all imported libraries and may fail when encountering a library that is not compatible with SSR. [This currently occurs even when SSR is disabled](https://github.com/sveltejs/kit/issues/754).
2222

2323
If you need access to the `document` or `window` variables or otherwise need it to run only on the client-side you can wrap it in a `browser` check:
2424

@@ -30,17 +30,20 @@ if (browser) {
3030
}
3131
```
3232

33-
You can also run code in `onMount` if you'd like to run it after the component has been first rendered to the DOM.
33+
You can also run code in `onMount` if you'd like to run it after the component has been first rendered to the DOM. In this case, you may still find a benefit of including a `browser` check as shown below because Vite may otherwise attempt to optimize the dependency and fail on it. [We hope to make this unnecessary in the future](https://github.com/sveltejs/svelte/issues/6372).
3434

3535
```html
3636
<script>
3737
import { onMount } from 'svelte';
38+
import { browser } from '$app/env';
3839
3940
let lib;
4041
41-
onMount(async () => {
42-
lib = (await import('some-browser-only-library')).default;
43-
});
42+
if (browser) {
43+
onMount(async () => {
44+
lib = (await import('some-browser-only-library')).default;
45+
});
46+
}
4447
</script>
4548
```
4649

packages/kit/src/core/build/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,16 @@ async function build_server(
335335
error.stack = options.get_stack(error);
336336
},
337337
hooks: get_hooks(user_hooks),
338-
hydrate: ${config.kit.hydrate},
338+
hydrate: ${s(config.kit.hydrate)},
339339
initiator: undefined,
340340
load_component,
341341
manifest,
342342
paths: settings.paths,
343-
prerender: ${config.kit.prerender && config.kit.prerender.enabled},
344343
read: settings.read,
345344
root,
346345
service_worker: ${service_worker_entry_file ? "'/service-worker.js'" : 'null'},
347-
router: ${config.kit.router},
348-
ssr: ${config.kit.ssr},
346+
router: ${s(config.kit.router)},
347+
ssr: ${s(config.kit.ssr)},
349348
target: ${s(config.kit.target)},
350349
template,
351350
trailing_slash: ${s(config.kit.trailingSlash)}

packages/kit/src/core/config/index.spec.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { deep_merge, validate_config } from './index.js';
44

55
test('fills in defaults', () => {
66
const validated = validate_config({});
7-
delete_complex_opts(validated);
7+
8+
// @ts-expect-error
9+
delete validated.kit.vite;
810

911
assert.equal(validated, {
1012
compilerOptions: null,
@@ -25,6 +27,7 @@ test('fills in defaults', () => {
2527
floc: false,
2628
host: null,
2729
hostHeader: null,
30+
hydrate: true,
2831
package: {
2932
dir: 'package',
3033
exports: {
@@ -46,11 +49,14 @@ test('fills in defaults', () => {
4649
},
4750
prerender: {
4851
crawl: true,
52+
enabled: true,
4953
// TODO: remove this for the 1.0 release
5054
force: undefined,
5155
onError: 'fail',
5256
pages: ['*']
5357
},
58+
router: true,
59+
ssr: true,
5460
target: null,
5561
trailingSlash: 'never'
5662
},
@@ -101,7 +107,8 @@ test('fills in partial blanks', () => {
101107

102108
assert.equal(validated.kit.vite(), {});
103109

104-
delete_complex_opts(validated);
110+
// @ts-expect-error
111+
delete validated.kit.vite;
105112

106113
assert.equal(validated, {
107114
compilerOptions: null,
@@ -122,6 +129,7 @@ test('fills in partial blanks', () => {
122129
floc: false,
123130
host: null,
124131
hostHeader: null,
132+
hydrate: true,
125133
package: {
126134
dir: 'package',
127135
exports: {
@@ -143,11 +151,14 @@ test('fills in partial blanks', () => {
143151
},
144152
prerender: {
145153
crawl: true,
154+
enabled: true,
146155
// TODO: remove this for the 1.0 release
147156
force: undefined,
148157
onError: 'fail',
149158
pages: ['*']
150159
},
160+
router: true,
161+
ssr: true,
151162
target: null,
152163
trailingSlash: 'never'
153164
},
@@ -506,17 +517,3 @@ deepMergeSuite('merge including toString', () => {
506517
});
507518

508519
deepMergeSuite.run();
509-
510-
/** @param {import('types/config').ValidatedConfig} validated */
511-
function delete_complex_opts(validated) {
512-
// @ts-expect-error
513-
delete validated.kit.vite;
514-
// @ts-expect-error
515-
delete validated.kit.hydrate;
516-
// @ts-expect-error
517-
delete validated.kit.prerender.enabled;
518-
// @ts-expect-error
519-
delete validated.kit.router;
520-
// @ts-expect-error
521-
delete validated.kit.ssr;
522-
}

packages/kit/src/core/config/options.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ const options = {
7878

7979
hostHeader: expect_string(null),
8080

81-
hydrate: expect_page_scriptable(async ({ page }) => {
82-
const leaf = await page;
83-
return 'hydrate' in leaf ? !!leaf.hydrate : true;
84-
}),
81+
hydrate: expect_boolean(true),
8582
serviceWorker: {
8683
type: 'branch',
8784
children: {
@@ -123,7 +120,7 @@ const options = {
123120
type: 'branch',
124121
children: {
125122
crawl: expect_boolean(true),
126-
enabled: expect_page_scriptable(async ({ page }) => !!(await page).prerender),
123+
enabled: expect_boolean(true),
127124
// TODO: remove this for the 1.0 release
128125
force: {
129126
type: 'leaf',
@@ -173,15 +170,9 @@ const options = {
173170
}
174171
},
175172

176-
router: expect_page_scriptable(async ({ page }) => {
177-
const leaf = await page;
178-
return 'router' in leaf ? !!leaf.router : true;
179-
}),
173+
router: expect_boolean(true),
180174

181-
ssr: expect_page_scriptable(async ({ page }) => {
182-
const leaf = await page;
183-
return 'ssr' in leaf ? !!leaf.ssr : true;
184-
}),
175+
ssr: expect_boolean(true),
185176

186177
target: expect_string(null),
187178

@@ -268,23 +259,6 @@ function expect_boolean(boolean) {
268259
};
269260
}
270261

271-
/**
272-
* @param {import('types/config').ScriptablePageOpt<boolean>} value
273-
* @returns {ConfigDefinition}
274-
*/
275-
function expect_page_scriptable(value) {
276-
return {
277-
type: 'leaf',
278-
default: value,
279-
validate: (option, keypath) => {
280-
if (typeof option !== 'boolean' && typeof option !== 'function') {
281-
throw new Error(`${keypath} should be a boolean or function that returns one`);
282-
}
283-
return option;
284-
}
285-
};
286-
}
287-
288262
/**
289263
* @param {string[]} options
290264
* @returns {ConfigDefinition}

packages/kit/src/core/config/test/index.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ async function testLoadDefaultConfig(path) {
1414
const cwd = join(__dirname, 'fixtures', path);
1515

1616
const config = await load_config({ cwd });
17-
delete_complex_opts(config);
17+
18+
// @ts-expect-error
19+
delete config.kit.vite; // can't test equality of a function
1820

1921
assert.equal(config, {
2022
compilerOptions: null,
@@ -35,6 +37,7 @@ async function testLoadDefaultConfig(path) {
3537
floc: false,
3638
host: null,
3739
hostHeader: null,
40+
hydrate: true,
3841
package: {
3942
dir: 'package',
4043
exports: {
@@ -51,7 +54,9 @@ async function testLoadDefaultConfig(path) {
5154
exclude: []
5255
},
5356
paths: { base: '', assets: '/.' },
54-
prerender: { crawl: true, force: undefined, onError: 'fail', pages: ['*'] },
57+
prerender: { crawl: true, enabled: true, force: undefined, onError: 'fail', pages: ['*'] },
58+
router: true,
59+
ssr: true,
5560
target: null,
5661
trailingSlash: 'never'
5762
},
@@ -98,17 +103,3 @@ test('errors on loading config with incorrect default export', async () => {
98103
});
99104

100105
test.run();
101-
102-
/** @param {import('types/config').ValidatedConfig} validated */
103-
function delete_complex_opts(validated) {
104-
// @ts-expect-error
105-
delete validated.kit.vite;
106-
// @ts-expect-error
107-
delete validated.kit.hydrate;
108-
// @ts-expect-error
109-
delete validated.kit.prerender.enabled;
110-
// @ts-expect-error
111-
delete validated.kit.router;
112-
// @ts-expect-error
113-
delete validated.kit.ssr;
114-
}

packages/kit/src/core/dev/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ async function create_handler(vite, config, dir, cwd, manifest) {
396396
};
397397
},
398398
manifest,
399-
prerender: config.kit.prerender.enabled,
400399
read: (file) => fs.readFileSync(path.join(config.kit.files.assets, file)),
401400
root,
402401
router: config.kit.router,

packages/kit/src/runtime/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function respond(incoming, options, state = {}) {
4848
return await render_response({
4949
options,
5050
$session: await options.hooks.getSession(request),
51-
page_config: { ssr: false, router: true, hydrate: true, prerender: true },
51+
page_config: { ssr: false, router: true, hydrate: true },
5252
status: 200,
5353
branch: []
5454
});

packages/kit/src/runtime/server/page/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const s = JSON.stringify;
1010
* @param {{
1111
* options: import('types/internal').SSRRenderOptions;
1212
* $session: any;
13-
* page_config: import('types/config').PageOpts;
13+
* page_config: { hydrate: boolean, router: boolean, ssr: boolean };
1414
* status: number;
1515
* error?: Error,
1616
* branch?: Array<import('./types').Loaded>;

0 commit comments

Comments
 (0)