From c90fc22e867c54b21c4867c07e6c2d577573c9e6 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 08:50:25 -0300 Subject: [PATCH 01/32] add 'never' option to ssr --- packages/kit/types/config.d.ts | 4 ++-- packages/kit/types/internal.d.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 3e509fa0981d..cf4d6c4d740d 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -1,6 +1,6 @@ import { UserConfig as ViteConfig } from 'vite'; import { RecursiveRequired } from './helper'; -import { Logger, TrailingSlash } from './internal'; +import { Logger, SSROption, TrailingSlash } from './internal'; export interface AdapterUtils { log: Logger; @@ -68,7 +68,7 @@ export interface Config { serviceWorker?: { files?(filepath: string): boolean; }; - ssr?: boolean; + ssr?: SSROption; target?: string; trailingSlash?: TrailingSlash; vite?: ViteConfig | (() => ViteConfig); diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 9fe00ab6d98d..f6fc4508f2f7 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -20,6 +20,8 @@ export interface Logger { info(msg: string): void; } +export type SSROption = boolean | 'never'; + export interface SSRComponent { ssr?: boolean; router?: boolean; @@ -28,7 +30,9 @@ export interface SSRComponent { preload?: any; // TODO remove for 1.0 load: Load; default: { - render(props: Record): { + render( + props: Record + ): { html: string; head: string; css: { @@ -130,7 +134,7 @@ export interface SSRRenderOptions { root: SSRComponent['default']; router: boolean; service_worker?: string; - ssr: boolean; + ssr: SSROption; target: string; template({ head, body }: { head: string; body: string }): string; trailing_slash: TrailingSlash; From ba8b6480febabb0c8da7e7b96b1e955d61298c53 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 08:51:09 -0300 Subject: [PATCH 02/32] update options validator --- packages/kit/src/core/config/options.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/kit/src/core/config/options.js b/packages/kit/src/core/config/options.js index 1a76bb14e113..fe857434e377 100644 --- a/packages/kit/src/core/config/options.js +++ b/packages/kit/src/core/config/options.js @@ -165,7 +165,7 @@ const options = object( files: fun((filename) => !/\.DS_STORE/.test(filename)) }), - ssr: boolean(true), + ssr: list([true, false, 'never']), target: string(null), @@ -275,16 +275,21 @@ function boolean(fallback) { } /** - * @param {string[]} options + * @param {unknown[]} options * @returns {Validator} */ function list(options, fallback = options[0]) { return validate(fallback, (input, keypath) => { + /** @param {unknown} i */ + const stringify = (i) => (typeof i === 'string' ? `"${i}"` : `${i}`); if (!options.includes(input)) { - // prettier-ignore - const msg = options.length > 2 - ? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"` - : `${keypath} should be either "${options[0]}" or "${options[1]}"`; + const msg = + options.length > 2 + ? `${keypath} should be one of ${options + .slice(0, -1) + .map(stringify) + .join(', ')} or ${stringify(options[options.length - 1])}` + : `${keypath} should be either ${stringify(options[0])} or ${stringify(options[1])}`; throw new Error(msg); } From 7aab5d491772708e1f9ad92ec4cc42700dee4453 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 08:56:22 -0300 Subject: [PATCH 03/32] update page renderer --- .../kit/src/runtime/server/page/render.js | 13 +++++--- .../kit/src/runtime/server/page/respond.js | 30 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js index ea126a86d741..87f0525f6247 100644 --- a/packages/kit/src/runtime/server/page/render.js +++ b/packages/kit/src/runtime/server/page/render.js @@ -12,7 +12,11 @@ const s = JSON.stringify; * branch: Array; * options: import('types/internal').SSRRenderOptions; * $session: any; - * page_config: { hydrate: boolean, router: boolean, ssr: boolean }; + * page_config: { + * hydrate: boolean, + * router: boolean, + * ssr: import('types/internal').SSROption + * }; * status: number; * error?: Error, * page?: import('types/page').Page @@ -43,7 +47,8 @@ export async function render_response({ error.stack = options.get_stack(error); } - if (page_config.ssr) { + // excludes false and 'never' + if (page_config.ssr === true) { branch.forEach(({ node, loaded, fetched, uses_credentials }) => { if (node.css) node.css.forEach((url) => css.add(url)); if (node.js) node.js.forEach((url) => js.add(url)); @@ -124,9 +129,9 @@ export async function render_response({ })}, host: ${page && page.host ? s(page.host) : 'location.host'}, route: ${!!page_config.router}, - spa: ${!page_config.ssr}, + spa: ${page_config.ssr === 'never' || !page_config.ssr}, trailing_slash: ${s(options.trailing_slash)}, - hydrate: ${page_config.ssr && page_config.hydrate ? `{ + hydrate: ${page_config.ssr === true && page_config.hydrate ? `{ status: ${status}, error: ${serialize_error(error)}, nodes: [ diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 5e260ce04225..9239497498d8 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -29,7 +29,11 @@ export async function respond(opts) { let nodes; try { - nodes = await Promise.all(route.a.map((id) => (id ? options.load_component(id) : undefined))); + const ssr = options.ssr !== 'never'; + + nodes = await Promise.all( + route.a.map((id) => (id && ssr ? options.load_component(id) : undefined)) + ); } catch (err) { const error = coalesce_to_error(err); @@ -72,7 +76,7 @@ export async function respond(opts) { /** @type {string[]} */ let set_cookie_headers = []; - ssr: if (page_config.ssr) { + ssr: if (page_config.ssr === true) { let stuff = {}; for (let i = 0; i < nodes.length; i += 1) { @@ -138,18 +142,16 @@ export async function respond(opts) { try { // there's no fallthough on an error page, so we know it's not undefined - const error_loaded = /** @type {import('./types').Loaded} */ ( - await load_node({ - ...opts, - node: error_node, - stuff: node_loaded.stuff, - prerender_enabled: is_prerender_enabled(options, error_node, state), - is_leaf: false, - is_error: true, - status, - error - }) - ); + const error_loaded = /** @type {import('./types').Loaded} */ (await load_node({ + ...opts, + node: error_node, + stuff: node_loaded.stuff, + prerender_enabled: is_prerender_enabled(options, error_node, state), + is_leaf: false, + is_error: true, + status, + error + })); if (error_loaded.loaded.error) { continue; From dc28d890b2f4925a2b7d32a7e30da93b77a1e5b4 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 10:59:11 -0300 Subject: [PATCH 04/32] fix no ssr response --- .../kit/src/runtime/server/page/respond.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 9239497498d8..316445e95aec 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -24,16 +24,25 @@ import { coalesce_to_error } from '../../../utils/error.js'; */ export async function respond(opts) { const { request, options, state, $session, route } = opts; + if (options.ssr === 'never') { + return await render_response({ + branch: [], + $session, + options, + page_config: { + hydrate: true, + router: true, + ssr: false + }, + status: 200 + }); + } /** @type {Array} */ let nodes; try { - const ssr = options.ssr !== 'never'; - - nodes = await Promise.all( - route.a.map((id) => (id && ssr ? options.load_component(id) : undefined)) - ); + nodes = await Promise.all(route.a.map((id) => (id ? options.load_component(id) : undefined))); } catch (err) { const error = coalesce_to_error(err); From ce61f663fb3dc45815b4aa5e6c338175bbd94abd Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 13:16:07 -0300 Subject: [PATCH 05/32] remove ssr union check --- packages/kit/src/runtime/server/page/respond.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 316445e95aec..1bdef333dac3 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -85,7 +85,7 @@ export async function respond(opts) { /** @type {string[]} */ let set_cookie_headers = []; - ssr: if (page_config.ssr === true) { + ssr: if (page_config.ssr) { let stuff = {}; for (let i = 0; i < nodes.length; i += 1) { From 1e6185ec6dbcdde3c3230fe486714a7b5c9561a2 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 13:18:44 -0300 Subject: [PATCH 06/32] fix formatting --- .../kit/src/runtime/server/page/respond.js | 22 ++++++++++--------- packages/kit/types/internal.d.ts | 4 +--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 1bdef333dac3..62afc04ae1e6 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -151,16 +151,18 @@ export async function respond(opts) { try { // there's no fallthough on an error page, so we know it's not undefined - const error_loaded = /** @type {import('./types').Loaded} */ (await load_node({ - ...opts, - node: error_node, - stuff: node_loaded.stuff, - prerender_enabled: is_prerender_enabled(options, error_node, state), - is_leaf: false, - is_error: true, - status, - error - })); + const error_loaded = /** @type {import('./types').Loaded} */ ( + await load_node({ + ...opts, + node: error_node, + stuff: node_loaded.stuff, + prerender_enabled: is_prerender_enabled(options, error_node, state), + is_leaf: false, + is_error: true, + status, + error + }) + ); if (error_loaded.loaded.error) { continue; diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index f6fc4508f2f7..32108fb6e34b 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -30,9 +30,7 @@ export interface SSRComponent { preload?: any; // TODO remove for 1.0 load: Load; default: { - render( - props: Record - ): { + render(props: Record): { html: string; head: string; css: { From 7fd45bd0c2fedf02b77e4e44eff3a62ef436b4e2 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 08:50:25 -0300 Subject: [PATCH 07/32] add 'never' option to ssr --- packages/kit/types/config.d.ts | 4 ++-- packages/kit/types/internal.d.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 3e509fa0981d..cf4d6c4d740d 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -1,6 +1,6 @@ import { UserConfig as ViteConfig } from 'vite'; import { RecursiveRequired } from './helper'; -import { Logger, TrailingSlash } from './internal'; +import { Logger, SSROption, TrailingSlash } from './internal'; export interface AdapterUtils { log: Logger; @@ -68,7 +68,7 @@ export interface Config { serviceWorker?: { files?(filepath: string): boolean; }; - ssr?: boolean; + ssr?: SSROption; target?: string; trailingSlash?: TrailingSlash; vite?: ViteConfig | (() => ViteConfig); diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 9fe00ab6d98d..f6fc4508f2f7 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -20,6 +20,8 @@ export interface Logger { info(msg: string): void; } +export type SSROption = boolean | 'never'; + export interface SSRComponent { ssr?: boolean; router?: boolean; @@ -28,7 +30,9 @@ export interface SSRComponent { preload?: any; // TODO remove for 1.0 load: Load; default: { - render(props: Record): { + render( + props: Record + ): { html: string; head: string; css: { @@ -130,7 +134,7 @@ export interface SSRRenderOptions { root: SSRComponent['default']; router: boolean; service_worker?: string; - ssr: boolean; + ssr: SSROption; target: string; template({ head, body }: { head: string; body: string }): string; trailing_slash: TrailingSlash; From 4ee0a9ce5694584999ad3454504c90884a775c7b Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 08:51:09 -0300 Subject: [PATCH 08/32] update options validator --- packages/kit/src/core/config/options.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/kit/src/core/config/options.js b/packages/kit/src/core/config/options.js index 1a76bb14e113..fe857434e377 100644 --- a/packages/kit/src/core/config/options.js +++ b/packages/kit/src/core/config/options.js @@ -165,7 +165,7 @@ const options = object( files: fun((filename) => !/\.DS_STORE/.test(filename)) }), - ssr: boolean(true), + ssr: list([true, false, 'never']), target: string(null), @@ -275,16 +275,21 @@ function boolean(fallback) { } /** - * @param {string[]} options + * @param {unknown[]} options * @returns {Validator} */ function list(options, fallback = options[0]) { return validate(fallback, (input, keypath) => { + /** @param {unknown} i */ + const stringify = (i) => (typeof i === 'string' ? `"${i}"` : `${i}`); if (!options.includes(input)) { - // prettier-ignore - const msg = options.length > 2 - ? `${keypath} should be one of ${options.slice(0, -1).map(input => `"${input}"`).join(', ')} or "${options[options.length - 1]}"` - : `${keypath} should be either "${options[0]}" or "${options[1]}"`; + const msg = + options.length > 2 + ? `${keypath} should be one of ${options + .slice(0, -1) + .map(stringify) + .join(', ')} or ${stringify(options[options.length - 1])}` + : `${keypath} should be either ${stringify(options[0])} or ${stringify(options[1])}`; throw new Error(msg); } From bc35a2ce01db1564e17ad40de25a166b2683ff94 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 08:56:22 -0300 Subject: [PATCH 09/32] update page renderer --- .../kit/src/runtime/server/page/render.js | 13 +++++--- .../kit/src/runtime/server/page/respond.js | 30 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js index ea126a86d741..87f0525f6247 100644 --- a/packages/kit/src/runtime/server/page/render.js +++ b/packages/kit/src/runtime/server/page/render.js @@ -12,7 +12,11 @@ const s = JSON.stringify; * branch: Array; * options: import('types/internal').SSRRenderOptions; * $session: any; - * page_config: { hydrate: boolean, router: boolean, ssr: boolean }; + * page_config: { + * hydrate: boolean, + * router: boolean, + * ssr: import('types/internal').SSROption + * }; * status: number; * error?: Error, * page?: import('types/page').Page @@ -43,7 +47,8 @@ export async function render_response({ error.stack = options.get_stack(error); } - if (page_config.ssr) { + // excludes false and 'never' + if (page_config.ssr === true) { branch.forEach(({ node, loaded, fetched, uses_credentials }) => { if (node.css) node.css.forEach((url) => css.add(url)); if (node.js) node.js.forEach((url) => js.add(url)); @@ -124,9 +129,9 @@ export async function render_response({ })}, host: ${page && page.host ? s(page.host) : 'location.host'}, route: ${!!page_config.router}, - spa: ${!page_config.ssr}, + spa: ${page_config.ssr === 'never' || !page_config.ssr}, trailing_slash: ${s(options.trailing_slash)}, - hydrate: ${page_config.ssr && page_config.hydrate ? `{ + hydrate: ${page_config.ssr === true && page_config.hydrate ? `{ status: ${status}, error: ${serialize_error(error)}, nodes: [ diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 5e260ce04225..9239497498d8 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -29,7 +29,11 @@ export async function respond(opts) { let nodes; try { - nodes = await Promise.all(route.a.map((id) => (id ? options.load_component(id) : undefined))); + const ssr = options.ssr !== 'never'; + + nodes = await Promise.all( + route.a.map((id) => (id && ssr ? options.load_component(id) : undefined)) + ); } catch (err) { const error = coalesce_to_error(err); @@ -72,7 +76,7 @@ export async function respond(opts) { /** @type {string[]} */ let set_cookie_headers = []; - ssr: if (page_config.ssr) { + ssr: if (page_config.ssr === true) { let stuff = {}; for (let i = 0; i < nodes.length; i += 1) { @@ -138,18 +142,16 @@ export async function respond(opts) { try { // there's no fallthough on an error page, so we know it's not undefined - const error_loaded = /** @type {import('./types').Loaded} */ ( - await load_node({ - ...opts, - node: error_node, - stuff: node_loaded.stuff, - prerender_enabled: is_prerender_enabled(options, error_node, state), - is_leaf: false, - is_error: true, - status, - error - }) - ); + const error_loaded = /** @type {import('./types').Loaded} */ (await load_node({ + ...opts, + node: error_node, + stuff: node_loaded.stuff, + prerender_enabled: is_prerender_enabled(options, error_node, state), + is_leaf: false, + is_error: true, + status, + error + })); if (error_loaded.loaded.error) { continue; From a3c0a303c73d730dffcae8fe4120183e32aaf4c3 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 10:59:11 -0300 Subject: [PATCH 10/32] fix no ssr response --- .../kit/src/runtime/server/page/respond.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 9239497498d8..316445e95aec 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -24,16 +24,25 @@ import { coalesce_to_error } from '../../../utils/error.js'; */ export async function respond(opts) { const { request, options, state, $session, route } = opts; + if (options.ssr === 'never') { + return await render_response({ + branch: [], + $session, + options, + page_config: { + hydrate: true, + router: true, + ssr: false + }, + status: 200 + }); + } /** @type {Array} */ let nodes; try { - const ssr = options.ssr !== 'never'; - - nodes = await Promise.all( - route.a.map((id) => (id && ssr ? options.load_component(id) : undefined)) - ); + nodes = await Promise.all(route.a.map((id) => (id ? options.load_component(id) : undefined))); } catch (err) { const error = coalesce_to_error(err); From 73ebf0dd4e4cc13eccb76a6b68e6835a92ca14a2 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 13:16:07 -0300 Subject: [PATCH 11/32] remove ssr union check --- packages/kit/src/runtime/server/page/respond.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 316445e95aec..1bdef333dac3 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -85,7 +85,7 @@ export async function respond(opts) { /** @type {string[]} */ let set_cookie_headers = []; - ssr: if (page_config.ssr === true) { + ssr: if (page_config.ssr) { let stuff = {}; for (let i = 0; i < nodes.length; i += 1) { From 64947d09103f17f5b26037dd3ee42047b9d0dac7 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Thu, 30 Sep 2021 13:18:44 -0300 Subject: [PATCH 12/32] fix formatting --- .../kit/src/runtime/server/page/respond.js | 22 ++++++++++--------- packages/kit/types/internal.d.ts | 4 +--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 1bdef333dac3..62afc04ae1e6 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -151,16 +151,18 @@ export async function respond(opts) { try { // there's no fallthough on an error page, so we know it's not undefined - const error_loaded = /** @type {import('./types').Loaded} */ (await load_node({ - ...opts, - node: error_node, - stuff: node_loaded.stuff, - prerender_enabled: is_prerender_enabled(options, error_node, state), - is_leaf: false, - is_error: true, - status, - error - })); + const error_loaded = /** @type {import('./types').Loaded} */ ( + await load_node({ + ...opts, + node: error_node, + stuff: node_loaded.stuff, + prerender_enabled: is_prerender_enabled(options, error_node, state), + is_leaf: false, + is_error: true, + status, + error + }) + ); if (error_loaded.loaded.error) { continue; diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index f6fc4508f2f7..32108fb6e34b 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -30,9 +30,7 @@ export interface SSRComponent { preload?: any; // TODO remove for 1.0 load: Load; default: { - render( - props: Record - ): { + render(props: Record): { html: string; head: string; css: { From caf1d52b25884fab85684933c3e3759a351ad663 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 2 Oct 2021 10:11:07 -0300 Subject: [PATCH 13/32] ensure ssr is boolean on get_page_config --- packages/kit/src/runtime/server/page/respond.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 62afc04ae1e6..fafa5ecf4323 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -240,7 +240,7 @@ export async function respond(opts) { */ function get_page_config(leaf, options) { return { - ssr: 'ssr' in leaf ? !!leaf.ssr : options.ssr, + ssr: 'ssr' in leaf ? !!leaf.ssr : options.ssr === true, router: 'router' in leaf ? !!leaf.router : options.router, hydrate: 'hydrate' in leaf ? !!leaf.hydrate : options.hydrate }; From b78c01b453e948911f8c454a57136027aa3890fb Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 2 Oct 2021 14:09:27 -0300 Subject: [PATCH 14/32] mention "never" on docs --- documentation/docs/11-ssr-and-javascript.md | 2 ++ documentation/docs/14-configuration.md | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/documentation/docs/11-ssr-and-javascript.md b/documentation/docs/11-ssr-and-javascript.md index fe9b3e0721a7..62ca6295525f 100644 --- a/documentation/docs/11-ssr-and-javascript.md +++ b/documentation/docs/11-ssr-and-javascript.md @@ -22,6 +22,8 @@ You can disable SSR app-wide with the [`ssr` config option](#configuration-ssr), ``` +Note: `"never"` is not supported on page level `ssr` exports. + ### router 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. diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index e7e8d5a775e1..08cc58545ec6 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -199,7 +199,8 @@ An object containing zero or more of the following values: ### ssr -Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide. +- `true` or `false` — Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide. +- `"never"` — Disables server-side rendering app-wide, and does NOT allow pages to be evaluated on the server, allowing the use of browser only variables without an environment check. ### target From 7ecd93e9326f78084e0908b646577ac5012963e5 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 2 Oct 2021 14:29:53 -0300 Subject: [PATCH 15/32] mention "never" on integrations faq --- documentation/faq/80-integrations.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/documentation/faq/80-integrations.md b/documentation/faq/80-integrations.md index 7b7d866c5fa7..0d2005318604 100644 --- a/documentation/faq/80-integrations.md +++ b/documentation/faq/80-integrations.md @@ -85,6 +85,24 @@ onMount(() => { }); ``` +But if your app doesn't use SSR, you can set the `ssr` option to `'never'`: + +```js +export default { + kit: { + // ... + ssr: 'never' + // ... + } +} +``` + +```js +import { method } from 'some-browser-only-library'; + +method('hello world!'); + +``` ### How do I use Firebase? Please use SDK v9 which provides a modular SDK approach that's currently in beta. The old versions are very difficult to get working especially with SSR and also resulted in a much larger client download size. Even with v9, most users need to set `kit.ssr: false` until [vite#4425](https://github.com/vitejs/vite/issues/4425) and [firebase-js-sdk#4846](https://github.com/firebase/firebase-js-sdk/issues/4846) are solved. From bc144a0ed4ccac888e057cd2412a295d9728824b Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 2 Oct 2021 14:49:35 -0300 Subject: [PATCH 16/32] avoid prerendering when ssr = 'never' --- packages/kit/src/core/adapt/prerender.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index 0defa1a03465..9e669c893bdc 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -94,7 +94,7 @@ const REDIRECT = 3; * }} opts */ export async function prerender({ cwd, out, log, config, build_data, fallback, all }) { - if (!config.kit.prerender.enabled && !fallback) { + if (config.kit.ssr === 'never' || (!config.kit.prerender.enabled && !fallback)) { return; } From 5701dd85c33f5fd17e6e3045c14b88c4cc2ed0b4 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 3 Oct 2021 15:33:09 -0300 Subject: [PATCH 17/32] avoid importing components on server build --- packages/kit/src/core/build/index.js | 65 +++++++++++++++------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 0921e1a809a0..a67acf692b45 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -259,32 +259,35 @@ async function build_server( } } + const allow_ssr = config.kit.ssr !== 'never'; + /** @type {Record} */ const metadata_lookup = {}; - - manifest.components.forEach((file) => { - const js_deps = new Set(); - const css_deps = new Set(); - - find_deps(file, js_deps, css_deps); - - const js = Array.from(js_deps); - const css = Array.from(css_deps); - - const styles = config.kit.amp - ? Array.from(css_deps).map((url) => { - const resolved = `${output_dir}/client/${config.kit.appDir}/${url}`; - return fs.readFileSync(resolved, 'utf-8'); - }) - : []; - - metadata_lookup[file] = { - entry: client_manifest[file].file, - css, - js, - styles - }; - }); + if (allow_ssr) { + manifest.components.forEach((file) => { + const js_deps = new Set(); + const css_deps = new Set(); + + find_deps(file, js_deps, css_deps); + + const js = Array.from(js_deps); + const css = Array.from(css_deps); + + const styles = config.kit.amp + ? Array.from(css_deps).map((url) => { + const resolved = `${output_dir}/client/${config.kit.appDir}/${url}`; + return fs.readFileSync(resolved, 'utf-8'); + }) + : []; + + metadata_lookup[file] = { + entry: client_manifest[file].file, + css, + js, + styles + }; + }); + } /** @type {Set} */ const entry_js = new Set(); @@ -412,21 +415,23 @@ async function build_server( externalFetch: hooks.externalFetch || fetch }); - const module_lookup = { - ${manifest.components.map(file => `${s(file)}: () => import(${s(app_relative(file))})`)} - }; + ${allow_ssr ? + `const module_lookup = { + ${manifest.components.map((file) => `${s(file)}: () => import(${s(app_relative(file))})`)} + };` : ''} - const metadata_lookup = ${s(metadata_lookup)}; + ${allow_ssr ? `const metadata_lookup = ${s(metadata_lookup)};` : ''} async function load_component(file) { - const { entry, css, js, styles } = metadata_lookup[file]; + ${allow_ssr ? + `const { entry, css, js, styles } = metadata_lookup[file]; return { module: await module_lookup[file](), entry: assets + ${s(prefix)} + entry, css: css.map(dep => assets + ${s(prefix)} + dep), js: js.map(dep => assets + ${s(prefix)} + dep), styles - }; + };` : 'throw new Error(\'Cannot use ssr when config.kit.ssr is "never"\')'} } export function render(request, { From d2f7b3149685d4de82c65f3f509cf87241e915cc Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 3 Oct 2021 15:48:11 -0300 Subject: [PATCH 18/32] clarify "never" option on docs --- documentation/docs/14-configuration.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index 08cc58545ec6..bea5b416090c 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -200,7 +200,8 @@ An object containing zero or more of the following values: ### ssr - `true` or `false` — Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide. -- `"never"` — Disables server-side rendering app-wide, and does NOT allow pages to be evaluated on the server, allowing the use of browser only variables without an environment check. +- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender +). Using this option is recommended when building an SPA. ### target From e04e707b0908b96629f2bb04fccce46232092bdf Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 3 Oct 2021 15:56:10 -0300 Subject: [PATCH 19/32] fix typo on docs --- documentation/docs/14-configuration.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index bea5b416090c..94c42acde9c6 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -200,8 +200,7 @@ An object containing zero or more of the following values: ### ssr - `true` or `false` — Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide. -- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender -). Using this option is recommended when building an SPA. +- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender). Using this option is recommended when building an SPA. ### target From 87edb89a43f2b421f2825338667a246a32edd700 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Mon, 4 Oct 2021 10:28:24 -0300 Subject: [PATCH 20/32] add changeset --- .changeset/bright-tips-beam.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/bright-tips-beam.md diff --git a/.changeset/bright-tips-beam.md b/.changeset/bright-tips-beam.md new file mode 100644 index 000000000000..ca0870a8eac8 --- /dev/null +++ b/.changeset/bright-tips-beam.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Added a "never" value to the config.kit.ssr option, it prevents pages from being evaluated on the server on both ssr and prerendering,n From 0e83be92c60142a8b4afe712558f34c455b8ecde Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Mon, 4 Oct 2021 13:48:11 -0300 Subject: [PATCH 21/32] Update changeset --- .changeset/bright-tips-beam.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/bright-tips-beam.md b/.changeset/bright-tips-beam.md index ca0870a8eac8..831f230aa994 100644 --- a/.changeset/bright-tips-beam.md +++ b/.changeset/bright-tips-beam.md @@ -2,4 +2,4 @@ '@sveltejs/kit': patch --- -Added a "never" value to the config.kit.ssr option, it prevents pages from being evaluated on the server on both ssr and prerendering,n +Added a "never" value to the config.kit.ssr option that prevents pages from being evaluated on the server on both ssr and prerendering. From 25ded6c3bc17795ceeaae3ad315198c16d86b6b7 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Tue, 5 Oct 2021 07:29:58 -0300 Subject: [PATCH 22/32] update docs --- documentation/docs/11-ssr-and-javascript.md | 4 ++-- documentation/docs/14-configuration.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/documentation/docs/11-ssr-and-javascript.md b/documentation/docs/11-ssr-and-javascript.md index 62ca6295525f..572604b0d4b3 100644 --- a/documentation/docs/11-ssr-and-javascript.md +++ b/documentation/docs/11-ssr-and-javascript.md @@ -14,7 +14,7 @@ Disabling [server-side rendering](#appendix-ssr) effectively turns your SvelteKi > 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. -You can disable SSR app-wide with the [`ssr` config option](#configuration-ssr), or a page-level `ssr` export: +You can disable SSR on a page-level with a `ssr` export. Page-level `ssr` exports must be boolean values, if another value is provided it will be cast into a boolean. ```html ``` -Note: `"never"` is not supported on page level `ssr` exports. +You can also disable SSR app-wide with the [`ssr` config option](#configuration-ssr), using a boolean or `"never"`. In case you use `"never"`, all page-level `ssr` exports will be completely ignored regardless of their value. ### router diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index 94c42acde9c6..518014e49a79 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -199,8 +199,8 @@ An object containing zero or more of the following values: ### ssr -- `true` or `false` — Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide. -- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender). Using this option is recommended when building an SPA. +- `true` or `false` — Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide and allows pages to override it locally. +- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender), can't be overriden by page-level exports. Using this option is recommended when building an SPA. ### target From edbd99460d76e58a60b4817b2354efae6face363 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Fri, 8 Oct 2021 18:01:10 -0300 Subject: [PATCH 23/32] add spa tests --- packages/kit/test/apps/spa/package.json | 16 ++++++++++++++ packages/kit/test/apps/spa/src/app.html | 13 +++++++++++ .../spa/src/routes/client-code/_client_dep.js | 1 + .../apps/spa/src/routes/client-code/_tests.js | 22 +++++++++++++++++++ .../spa/src/routes/client-code/dep.svelte | 5 +++++ .../spa/src/routes/client-code/index.svelte | 5 +++++ packages/kit/test/apps/spa/svelte.config.js | 9 ++++++++ 7 files changed, 71 insertions(+) create mode 100644 packages/kit/test/apps/spa/package.json create mode 100644 packages/kit/test/apps/spa/src/app.html create mode 100644 packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js create mode 100644 packages/kit/test/apps/spa/src/routes/client-code/_tests.js create mode 100644 packages/kit/test/apps/spa/src/routes/client-code/dep.svelte create mode 100644 packages/kit/test/apps/spa/src/routes/client-code/index.svelte create mode 100644 packages/kit/test/apps/spa/svelte.config.js diff --git a/packages/kit/test/apps/spa/package.json b/packages/kit/test/apps/spa/package.json new file mode 100644 index 000000000000..f87215fbeda0 --- /dev/null +++ b/packages/kit/test/apps/spa/package.json @@ -0,0 +1,16 @@ +{ + "name": "test-spa", + "private": true, + "version": "0.0.1", + "scripts": { + "dev": "../../../svelte-kit.js dev", + "build": "../../../svelte-kit.js build", + "preview": "../../../svelte-kit.js preview" + }, + "devDependencies": { + "@sveltejs/kit": "workspace:*", + "@sveltejs/adapter-node": "workspace:*", + "svelte": "^3.43.0" + }, + "type": "module" +} diff --git a/packages/kit/test/apps/spa/src/app.html b/packages/kit/test/apps/spa/src/app.html new file mode 100644 index 000000000000..97f318d90764 --- /dev/null +++ b/packages/kit/test/apps/spa/src/app.html @@ -0,0 +1,13 @@ + + + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js b/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js new file mode 100644 index 000000000000..9991ef98327e --- /dev/null +++ b/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js @@ -0,0 +1 @@ +export const root = /** @type {HTMLElement}*/ (document.getElementById('svelte')); diff --git a/packages/kit/test/apps/spa/src/routes/client-code/_tests.js b/packages/kit/test/apps/spa/src/routes/client-code/_tests.js new file mode 100644 index 000000000000..6f692ba59c78 --- /dev/null +++ b/packages/kit/test/apps/spa/src/routes/client-code/_tests.js @@ -0,0 +1,22 @@ +import * as assert from 'uvu/assert'; + +/** @type {import('test').TestMaker} */ +export default function (test) { + test('page with client only code', '/client-code', async ({ page, js }) => { + if (js) { + await page.waitForSelector('span'); + assert.equal(await page.textContent('span'), 'App root is div#svelte'); + } else { + assert.ok(await page.evaluate(() => !document.querySelector('span'))); + } + }); + + test('page with client only dependency', '/client-code/dep', async ({ page, js }) => { + if (js) { + await page.waitForSelector('span'); + assert.equal(await page.textContent('span'), 'App root is div#svelte'); + } else { + assert.ok(await page.evaluate(() => !document.querySelector('span'))); + } + }); +} diff --git a/packages/kit/test/apps/spa/src/routes/client-code/dep.svelte b/packages/kit/test/apps/spa/src/routes/client-code/dep.svelte new file mode 100644 index 000000000000..6b6ee2f7290f --- /dev/null +++ b/packages/kit/test/apps/spa/src/routes/client-code/dep.svelte @@ -0,0 +1,5 @@ + + +App root is {root.localName}#{root.id} diff --git a/packages/kit/test/apps/spa/src/routes/client-code/index.svelte b/packages/kit/test/apps/spa/src/routes/client-code/index.svelte new file mode 100644 index 000000000000..9aab6fb8ec50 --- /dev/null +++ b/packages/kit/test/apps/spa/src/routes/client-code/index.svelte @@ -0,0 +1,5 @@ + + +App root is {appRoot.localName}#{appRoot.id} diff --git a/packages/kit/test/apps/spa/svelte.config.js b/packages/kit/test/apps/spa/svelte.config.js new file mode 100644 index 000000000000..0efb6d12ee5c --- /dev/null +++ b/packages/kit/test/apps/spa/svelte.config.js @@ -0,0 +1,9 @@ +/** @type {import('@sveltejs/kit').Config} */ +const config = { + kit: { + ssr: 'never', + target: '#svelte' + } +}; + +export default config; From 2c8649456a043926d0e8abb2096cfd89e3f48d8b Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Fri, 8 Oct 2021 18:06:54 -0300 Subject: [PATCH 24/32] fix formatting --- packages/kit/test/apps/spa/package.json | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/kit/test/apps/spa/package.json b/packages/kit/test/apps/spa/package.json index f87215fbeda0..b75528dfea7f 100644 --- a/packages/kit/test/apps/spa/package.json +++ b/packages/kit/test/apps/spa/package.json @@ -1,16 +1,16 @@ { - "name": "test-spa", - "private": true, - "version": "0.0.1", - "scripts": { - "dev": "../../../svelte-kit.js dev", - "build": "../../../svelte-kit.js build", - "preview": "../../../svelte-kit.js preview" - }, - "devDependencies": { - "@sveltejs/kit": "workspace:*", - "@sveltejs/adapter-node": "workspace:*", - "svelte": "^3.43.0" - }, - "type": "module" + "name": "test-spa", + "private": true, + "version": "0.0.1", + "scripts": { + "dev": "../../../svelte-kit.js dev", + "build": "../../../svelte-kit.js build", + "preview": "../../../svelte-kit.js preview" + }, + "devDependencies": { + "@sveltejs/kit": "workspace:*", + "@sveltejs/adapter-node": "workspace:*", + "svelte": "^3.43.0" + }, + "type": "module" } From 86d2d19bd1cf87b786beb5beb3cafb3d9ca7179a Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 23 Oct 2021 15:17:30 -0300 Subject: [PATCH 25/32] remove unused prerendering check --- packages/kit/src/core/adapt/prerender.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index 9e669c893bdc..0defa1a03465 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -94,7 +94,7 @@ const REDIRECT = 3; * }} opts */ export async function prerender({ cwd, out, log, config, build_data, fallback, all }) { - if (config.kit.ssr === 'never' || (!config.kit.prerender.enabled && !fallback)) { + if (!config.kit.prerender.enabled && !fallback) { return; } From f5d46455548f4f75283e2d02ef2c7e917cb2a477 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 23 Oct 2021 16:21:04 -0300 Subject: [PATCH 26/32] check for ssr option in respond_with_error --- .../runtime/server/page/respond_with_error.js | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/packages/kit/src/runtime/server/page/respond_with_error.js b/packages/kit/src/runtime/server/page/respond_with_error.js index 986a788bb37e..481fdc283e34 100644 --- a/packages/kit/src/runtime/server/page/respond_with_error.js +++ b/packages/kit/src/runtime/server/page/respond_with_error.js @@ -20,9 +20,6 @@ import { coalesce_to_error } from '../../../utils/error.js'; * }} opts */ export async function respond_with_error({ request, options, state, $session, status, error }) { - const default_layout = await options.load_component(options.manifest.layout); - const default_error = await options.load_component(options.manifest.error); - const page = { host: request.host, path: request.path, @@ -30,43 +27,51 @@ export async function respond_with_error({ request, options, state, $session, st params: {} }; - // error pages don't fall through, so we know it's not undefined - const loaded = /** @type {Loaded} */ ( - await load_node({ - request, - options, - state, - route: null, - page, - node: default_layout, - $session, - stuff: {}, - prerender_enabled: is_prerender_enabled(options, default_error, state), - is_leaf: false, - is_error: false - }) - ); + /** @type {Loaded[]} */ + let branch = []; - const branch = [ - loaded, - /** @type {Loaded} */ ( + if (options.ssr !== 'never') { + const default_layout = await options.load_component(options.manifest.layout); + const default_error = await options.load_component(options.manifest.error); + + // error pages don't fall through, so we know it's not undefined + const loaded = /** @type {Loaded} */ ( await load_node({ request, options, state, route: null, page, - node: default_error, + node: default_layout, $session, - stuff: loaded ? loaded.stuff : {}, + stuff: {}, prerender_enabled: is_prerender_enabled(options, default_error, state), is_leaf: false, - is_error: true, - status, - error + is_error: false }) - ) - ]; + ); + + branch = [ + loaded, + /** @type {Loaded} */ ( + await load_node({ + request, + options, + state, + route: null, + page, + node: default_error, + $session, + stuff: loaded ? loaded.stuff : {}, + prerender_enabled: is_prerender_enabled(options, default_error, state), + is_leaf: false, + is_error: true, + status, + error + }) + ) + ]; + } try { return await render_response({ From 9dad824d018576d34267e97109cb33ae041b14ac Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sat, 23 Oct 2021 17:08:37 -0300 Subject: [PATCH 27/32] warn users about using "never" --- documentation/docs/11-ssr-and-javascript.md | 2 +- documentation/docs/14-configuration.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/documentation/docs/11-ssr-and-javascript.md b/documentation/docs/11-ssr-and-javascript.md index 572604b0d4b3..9e4315bb6993 100644 --- a/documentation/docs/11-ssr-and-javascript.md +++ b/documentation/docs/11-ssr-and-javascript.md @@ -22,7 +22,7 @@ You can disable SSR on a page-level with a `ssr` export. Page-level `ssr` export ``` -You can also disable SSR app-wide with the [`ssr` config option](#configuration-ssr), using a boolean or `"never"`. In case you use `"never"`, all page-level `ssr` exports will be completely ignored regardless of their value. +You can also disable SSR app-wide with the [`ssr` config option](#configuration-ssr), using a boolean or `"never"`. In case you use `"never"`, all page-level `ssr` exports will be completely ignored regardless of their value, only consider using this if you are sure you don't need SSR. ### router diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index 518014e49a79..fbebd31ef803 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -200,7 +200,7 @@ An object containing zero or more of the following values: ### ssr - `true` or `false` — Enables or disables [server-side rendering](#ssr-and-javascript-ssr) app-wide and allows pages to override it locally. -- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender), can't be overriden by page-level exports. Using this option is recommended when building an SPA. +- `"never"` — Prevents all pages from being evaluated on the server, on both [server-side rendering](#ssr-and-javascript-ssr) and [prerendering](#ssr-and-javascript-prerender), can't be overriden by page-level exports. Only consider using this option if you don't need SSR (like when building an SPA). ### target From 5d9a94a6ccb2b587add1b3063595698ab7852b54 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:43:39 -0300 Subject: [PATCH 28/32] replace tabs by spaces on page/render.js --- packages/kit/src/runtime/server/page/render.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js index 87f0525f6247..ef9958fb2fa3 100644 --- a/packages/kit/src/runtime/server/page/render.js +++ b/packages/kit/src/runtime/server/page/render.js @@ -13,9 +13,9 @@ const s = JSON.stringify; * options: import('types/internal').SSRRenderOptions; * $session: any; * page_config: { - * hydrate: boolean, - * router: boolean, - * ssr: import('types/internal').SSROption + * hydrate: boolean, + * router: boolean, + * ssr: import('types/internal').SSROption * }; * status: number; * error?: Error, From 023ca9bbc7fa0c73db40e6be6fe2ddc9bc5a24e2 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:48:06 -0300 Subject: [PATCH 29/32] group module and metadata lookups --- packages/kit/src/core/build/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index a67acf692b45..7fdde8c0b292 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -418,9 +418,8 @@ async function build_server( ${allow_ssr ? `const module_lookup = { ${manifest.components.map((file) => `${s(file)}: () => import(${s(app_relative(file))})`)} - };` : ''} - - ${allow_ssr ? `const metadata_lookup = ${s(metadata_lookup)};` : ''} + }; + const metadata_lookup = ${s(metadata_lookup)};` : ''} async function load_component(file) { ${allow_ssr ? From cfc5eae74eab8c867365ee629ae089178a22f185 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:48:20 -0300 Subject: [PATCH 30/32] more concise spa check --- packages/kit/src/runtime/server/page/render.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js index ef9958fb2fa3..29a535e6c5c8 100644 --- a/packages/kit/src/runtime/server/page/render.js +++ b/packages/kit/src/runtime/server/page/render.js @@ -129,7 +129,7 @@ export async function render_response({ })}, host: ${page && page.host ? s(page.host) : 'location.host'}, route: ${!!page_config.router}, - spa: ${page_config.ssr === 'never' || !page_config.ssr}, + spa: ${page_config.ssr !== true}, trailing_slash: ${s(options.trailing_slash)}, hydrate: ${page_config.ssr === true && page_config.hydrate ? `{ status: ${status}, From 1b4720ef013e01492e59a1e71801e4b1b4912076 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:48:30 -0300 Subject: [PATCH 31/32] type cast formatting --- .../kit/test/apps/spa/src/routes/client-code/_client_dep.js | 2 +- packages/kit/test/apps/spa/src/routes/client-code/index.svelte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js b/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js index 9991ef98327e..6ab4531427b5 100644 --- a/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js +++ b/packages/kit/test/apps/spa/src/routes/client-code/_client_dep.js @@ -1 +1 @@ -export const root = /** @type {HTMLElement}*/ (document.getElementById('svelte')); +export const root = /** @type {HTMLElement} */ (document.getElementById('svelte')); diff --git a/packages/kit/test/apps/spa/src/routes/client-code/index.svelte b/packages/kit/test/apps/spa/src/routes/client-code/index.svelte index 9aab6fb8ec50..158a776a26c3 100644 --- a/packages/kit/test/apps/spa/src/routes/client-code/index.svelte +++ b/packages/kit/test/apps/spa/src/routes/client-code/index.svelte @@ -1,5 +1,5 @@ App root is {appRoot.localName}#{appRoot.id} From be017552e20d09aad22df2a4fc561659e6eb6582 Mon Sep 17 00:00:00 2001 From: JeanJPNM <61994401+JeanJPNM@users.noreply.github.com> Date: Tue, 26 Oct 2021 07:42:10 -0300 Subject: [PATCH 32/32] show the file name in the error message Shows the name of the file that was attempted to be loaded while ssr was set to "never" --- packages/kit/src/core/build/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 7fdde8c0b292..a3d215cffdcb 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -430,7 +430,7 @@ async function build_server( css: css.map(dep => assets + ${s(prefix)} + dep), js: js.map(dep => assets + ${s(prefix)} + dep), styles - };` : 'throw new Error(\'Cannot use ssr when config.kit.ssr is "never"\')'} + };` : 'throw new Error(`Cannot evaluate pages on the server when config.kit.ssr is "never". (${file})`)'} } export function render(request, {