From fa53fbb01795926d1bda19c73539851e2e8b1dd4 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 21:08:43 -0600 Subject: [PATCH 01/20] feat: Move hooks.js initialization into Server.init --- packages/kit/src/vite/build/build_server.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index cb126b83c40d..f41f261707b9 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -84,7 +84,7 @@ export class Server { }; } - init({ env }) { + async init({ env }) { const entries = Object.entries(env); const prv = Object.fromEntries(entries.filter(([k]) => !k.startsWith('${ @@ -99,14 +99,8 @@ export class Server { set_public_env(pub); this.options.public_env = pub; - } - - async respond(request, options = {}) { - if (!(request instanceof Request)) { - throw new Error('The first argument to server.respond must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details'); - } - if (!this.options.hooks) { + if (!this.options.hooks) { const module = await import(${s(hooks)}); this.options.hooks = { handle: module.handle || (({ event, resolve }) => resolve(event)), @@ -114,6 +108,12 @@ export class Server { externalFetch: module.externalFetch || fetch }; } + } + + async respond(request, options = {}) { + if (!(request instanceof Request)) { + throw new Error('The first argument to server.respond must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details'); + } return respond(request, this.options, options); } From 02c576463701e16c0fcfd9d67544a4f4df7c8540 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 21:13:59 -0600 Subject: [PATCH 02/20] fix: Server type --- packages/kit/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 1b7f0f51c68e..e32f8482563f 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -263,7 +263,7 @@ export interface ResolveOptions { export class Server { constructor(manifest: SSRManifest); - init(options: ServerInitOptions): void; + init(options: ServerInitOptions): Promise; respond(request: Request, options: RequestOptions): Promise; } From 3bfd40c5cc51f3ac212a1e9e34bc6411ec5498c6 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 21:18:39 -0600 Subject: [PATCH 03/20] feat: Await Server.init in adapters --- packages/adapter-cloudflare-workers/files/entry.js | 2 +- packages/adapter-cloudflare/src/worker.js | 2 +- packages/adapter-netlify/src/serverless.js | 11 ++++++++++- packages/adapter-node/src/handler.js | 13 ++++++++++++- packages/adapter-vercel/files/serverless.js | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/adapter-cloudflare-workers/files/entry.js b/packages/adapter-cloudflare-workers/files/entry.js index 0646c7acc380..fe1c45a246ea 100644 --- a/packages/adapter-cloudflare-workers/files/entry.js +++ b/packages/adapter-cloudflare-workers/files/entry.js @@ -15,7 +15,7 @@ export default { * @param {any} context */ async fetch(req, env, context) { - server.init({ env }); + await server.init({ env }); const url = new URL(req.url); diff --git a/packages/adapter-cloudflare/src/worker.js b/packages/adapter-cloudflare/src/worker.js index 20993ee11beb..a31267aef944 100644 --- a/packages/adapter-cloudflare/src/worker.js +++ b/packages/adapter-cloudflare/src/worker.js @@ -9,7 +9,7 @@ const prefix = `/${manifest.appDir}/`; /** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */ const worker = { async fetch(req, env, context) { - server.init({ env }); + await server.init({ env }); // skip cache if "cache-control: no-cache" in request let pragma = req.headers.get('cache-control') || ''; let res = !pragma.includes('no-cache') && (await Cache.lookup(req)); diff --git a/packages/adapter-netlify/src/serverless.js b/packages/adapter-netlify/src/serverless.js index aec29eef562a..599b1ef8209b 100644 --- a/packages/adapter-netlify/src/serverless.js +++ b/packages/adapter-netlify/src/serverless.js @@ -9,11 +9,20 @@ import { split_headers } from './headers'; export function init(manifest) { const server = new Server(manifest); - server.init({ + let init_promise = server.init({ env: process.env }); return async (event, context) => { + if (init_promise !== null) { + // dear Rich, + // should we just await it indiscriminately? + // sincerely, + // me + await init_promise; + init_promise = null; + } + const response = await server.respond(to_request(event), { platform: { context }, getClientAddress() { diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index e5e67bfa3af9..62d1cd6853d0 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -11,7 +11,9 @@ import { env } from './env.js'; /* global ENV_PREFIX */ const server = new Server(manifest); -server.init({ env: process.env }); +// Dear Rich, +// it says top-level await isn't available. Should it be? +let init_promise = server.init({ env: process.env }); const origin = env('ORIGIN', undefined); const xff_depth = parseInt(env('XFF_DEPTH', '1')); @@ -21,6 +23,14 @@ const host_header = env('HOST_HEADER', 'host').toLowerCase(); const __dirname = path.dirname(fileURLToPath(import.meta.url)); +/** @type {import('polka').Middleware} */ +const init = async () => { + if (init_promise !== null) { + await init_promise; + init_promise = null; + } +}; + /** * @param {string} path * @param {boolean} client @@ -132,6 +142,7 @@ function get_origin(headers) { export const handler = sequence( [ + init, serve(path.join(__dirname, '/client'), true), serve(path.join(__dirname, '/static')), serve(path.join(__dirname, '/prerendered')), diff --git a/packages/adapter-vercel/files/serverless.js b/packages/adapter-vercel/files/serverless.js index 639275b9b7a1..c647fd8b1f9b 100644 --- a/packages/adapter-vercel/files/serverless.js +++ b/packages/adapter-vercel/files/serverless.js @@ -7,7 +7,7 @@ installPolyfills(); const server = new Server(manifest); -server.init({ +await server.init({ env: process.env }); From 286648ae55d997f80e1865a36af34aeb53b72e50 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 21:21:13 -0600 Subject: [PATCH 04/20] feat: Await server.init in preview --- packages/kit/src/vite/preview/index.js | 2 +- packages/kit/types/internal.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/vite/preview/index.js b/packages/kit/src/vite/preview/index.js index 56c47191360e..561d22cb1e36 100644 --- a/packages/kit/src/vite/preview/index.js +++ b/packages/kit/src/vite/preview/index.js @@ -45,7 +45,7 @@ export async function preview(vite, vite_config, svelte_config) { }); const server = new Server(manifest); - server.init({ + await server.init({ env: loadEnv(vite_config.mode, process.cwd(), '') }); diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index b3677941b849..33b37edb3106 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -101,7 +101,7 @@ export interface ImportNode { } export class InternalServer extends Server { - init(options: ServerInitOptions): void; + init(options: ServerInitOptions): Promise; respond( request: Request, options: RequestOptions & { From 204acb1ded76f2c0f807611378cd0f26ec632b61 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 21:44:40 -0600 Subject: [PATCH 05/20] fix: Init server during prerender --- packages/kit/src/core/prerender/prerender.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index 93d535efdd6c..f00a8fd17098 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -159,6 +159,8 @@ export async function prerender() { }); const server = new Server(manifest); + const env = /** @type {Record} */ (process.env); + await server.init({ env }); const error = normalise_error_handler(log, config); From 16cdc7be671c9a414eec997c1b601dd5e578f2bd Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 21:44:59 -0600 Subject: [PATCH 06/20] feat: Init hooks when dev mode starts instead of on first request --- packages/kit/src/vite/dev/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js index 9b739aa976dc..a79460c29477 100644 --- a/packages/kit/src/vite/dev/index.js +++ b/packages/kit/src/vite/dev/index.js @@ -32,6 +32,11 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { sync.init(svelte_config, vite_config.mode); + /** @type {Partial} */ + const user_hooks = resolve_entry(svelte_config.kit.files.hooks) + ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) + : {}; + /** @type {import('types').Respond} */ const respond = (await import(`${runtime_prefix}/server/index.js`)).respond; @@ -318,11 +323,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { ); } - /** @type {Partial} */ - const user_hooks = resolve_entry(svelte_config.kit.files.hooks) - ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) - : {}; - const handle = user_hooks.handle || (({ event, resolve }) => resolve(event)); /** @type {import('types').Hooks} */ From f8bd4131712422dc14c37b24f5b40cc819e87d0f Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 22:17:58 -0600 Subject: [PATCH 07/20] fix: Prerendering, but better --- packages/kit/src/core/prerender/prerender.js | 5 ++--- packages/kit/src/vite/index.js | 8 +++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js index f00a8fd17098..883a705e54a1 100644 --- a/packages/kit/src/core/prerender/prerender.js +++ b/packages/kit/src/core/prerender/prerender.js @@ -15,7 +15,7 @@ import { load_config } from '../config/index.js'; * @typedef {import('types').Logger} Logger */ -const [, , client_out_dir, results_path, manifest_path, verbose] = process.argv; +const [, , client_out_dir, results_path, manifest_path, verbose, env] = process.argv; prerender(); @@ -159,8 +159,7 @@ export async function prerender() { }); const server = new Server(manifest); - const env = /** @type {Record} */ (process.env); - await server.init({ env }); + await server.init({ env: JSON.parse(env) }); const error = normalise_error_handler(log, config); diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index 34be9ca114ae..bee1fb40a59b 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -411,7 +411,13 @@ function kit() { const child = fork( script, - [vite_config.build.outDir, results_path, manifest_path, '' + verbose], + [ + vite_config.build.outDir, + results_path, + manifest_path, + '' + verbose, + JSON.stringify({ ...env.private, ...env.public }) + ], { stdio: 'inherit' } From ea9c2184a7cf77f183e81e205392372358b1d201 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 22:18:10 -0600 Subject: [PATCH 08/20] feat: Test env vars in prerender --- packages/kit/test/prerendering/basics/.env | 5 ++ .../kit/test/prerendering/basics/.gitignore | 1 + .../kit/test/prerendering/basics/src/hooks.js | 4 ++ .../basics/src/routes/env/+page.server.js | 9 +++ .../basics/src/routes/env/+page.svelte | 13 ++++ .../kit/test/prerendering/basics/test/test.js | 15 +++++ pnpm-lock.yaml | 64 ------------------- 7 files changed, 47 insertions(+), 64 deletions(-) create mode 100644 packages/kit/test/prerendering/basics/.env create mode 100644 packages/kit/test/prerendering/basics/.gitignore create mode 100644 packages/kit/test/prerendering/basics/src/routes/env/+page.server.js create mode 100644 packages/kit/test/prerendering/basics/src/routes/env/+page.svelte diff --git a/packages/kit/test/prerendering/basics/.env b/packages/kit/test/prerendering/basics/.env new file mode 100644 index 000000000000..1597af1243ec --- /dev/null +++ b/packages/kit/test/prerendering/basics/.env @@ -0,0 +1,5 @@ +PRIVATE_STATIC="accessible to server-side code/replaced at build time" +PRIVATE_DYNAMIC="accessible to server-side code/evaluated at run time" + +PUBLIC_STATIC="accessible anywhere/replaced at build time" +PUBLIC_DYNAMIC="accessible anywhere/evaluated at run time" diff --git a/packages/kit/test/prerendering/basics/.gitignore b/packages/kit/test/prerendering/basics/.gitignore new file mode 100644 index 000000000000..1e18f275e97c --- /dev/null +++ b/packages/kit/test/prerendering/basics/.gitignore @@ -0,0 +1 @@ +!.env \ No newline at end of file diff --git a/packages/kit/test/prerendering/basics/src/hooks.js b/packages/kit/test/prerendering/basics/src/hooks.js index b8df4f86b0a8..4a0771a2aadb 100644 --- a/packages/kit/test/prerendering/basics/src/hooks.js +++ b/packages/kit/test/prerendering/basics/src/hooks.js @@ -1,5 +1,9 @@ import { prerendering } from '$app/env'; +import { env } from '$env/dynamic/private'; + +console.log(env); + const initial_prerendering = prerendering; /** @type {import('@sveltejs/kit').Handle} */ diff --git a/packages/kit/test/prerendering/basics/src/routes/env/+page.server.js b/packages/kit/test/prerendering/basics/src/routes/env/+page.server.js new file mode 100644 index 000000000000..617a8542fd21 --- /dev/null +++ b/packages/kit/test/prerendering/basics/src/routes/env/+page.server.js @@ -0,0 +1,9 @@ +import { PRIVATE_STATIC } from '$env/static/private'; +import { env } from '$env/dynamic/private'; + +export function load() { + return { + PRIVATE_STATIC, + PRIVATE_DYNAMIC: env.PRIVATE_DYNAMIC + }; +} diff --git a/packages/kit/test/prerendering/basics/src/routes/env/+page.svelte b/packages/kit/test/prerendering/basics/src/routes/env/+page.svelte new file mode 100644 index 000000000000..4ee2f424da67 --- /dev/null +++ b/packages/kit/test/prerendering/basics/src/routes/env/+page.svelte @@ -0,0 +1,13 @@ + + +

PRIVATE_STATIC: {data.PRIVATE_STATIC}

+

PRIVATE_DYNAMIC: {data.PRIVATE_DYNAMIC}

+ +

PUBLIC_STATIC: {PUBLIC_STATIC}

+

PUBLIC_DYNAMIC: {env.PUBLIC_DYNAMIC}

diff --git a/packages/kit/test/prerendering/basics/test/test.js b/packages/kit/test/prerendering/basics/test/test.js index e79573f0f333..c65e334e936f 100644 --- a/packages/kit/test/prerendering/basics/test/test.js +++ b/packages/kit/test/prerendering/basics/test/test.js @@ -190,4 +190,19 @@ test('respects config.prerender.origin', () => { assert.ok(content.includes('

http://example.com

')); }); +test('$env - includes environment variables', () => { + const content = read('env.html'); + + assert.match( + content, + /.*PRIVATE_STATIC: accessible to server-side code\/replaced at build time.*/gs + ); + assert.match( + content, + /.*PRIVATE_DYNAMIC: accessible to server-side code\/evaluated at run time.*/gs + ); + assert.match(content, /.*PUBLIC_STATIC: accessible anywhere\/replaced at build time.*/gs); + assert.match(content, /.*PUBLIC_DYNAMIC: accessible anywhere\/evaluated at run time.*/gs); +}); + test.run(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40d809061811..fbe7ccdf681d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -433,70 +433,6 @@ importers: typescript: 4.7.4 vite: 3.0.8 - packages/kit/test/meta: - specifiers: - uvu: ^0.5.6 - devDependencies: - uvu: 0.5.6 - - packages/kit/test/meta/apps/private-dynamic-env: - specifiers: - '@sveltejs/kit': workspace:* - svelte: ^3.44.0 - svelte-check: ^2.7.1 - typescript: ^4.7.4 - vite: ^3.0.4 - devDependencies: - '@sveltejs/kit': link:../../../.. - svelte: 3.48.0 - svelte-check: 2.8.0_svelte@3.48.0 - typescript: 4.7.4 - vite: 3.0.8 - - packages/kit/test/meta/apps/private-dynamic-env-dynamic-import: - specifiers: - '@sveltejs/kit': workspace:* - svelte: ^3.44.0 - svelte-check: ^2.7.1 - typescript: ^4.7.4 - vite: ^3.0.4 - devDependencies: - '@sveltejs/kit': link:../../../.. - svelte: 3.48.0 - svelte-check: 2.8.0_svelte@3.48.0 - typescript: 4.7.4 - vite: 3.0.8 - - packages/kit/test/meta/apps/private-static-env: - specifiers: - '@sveltejs/kit': workspace:* - cross-env: ^7.0.3 - svelte: ^3.44.0 - svelte-check: ^2.7.1 - typescript: ^4.7.4 - vite: ^3.0.4 - devDependencies: - '@sveltejs/kit': link:../../../.. - cross-env: 7.0.3 - svelte: 3.48.0 - svelte-check: 2.8.0_svelte@3.48.0 - typescript: 4.7.4 - vite: 3.0.8 - - packages/kit/test/meta/apps/private-static-env-dynamic-import: - specifiers: - '@sveltejs/kit': workspace:* - svelte: ^3.44.0 - svelte-check: ^2.7.1 - typescript: ^4.7.4 - vite: ^3.0.4 - devDependencies: - '@sveltejs/kit': link:../../../.. - svelte: 3.48.0 - svelte-check: 2.8.0_svelte@3.48.0 - typescript: 4.7.4 - vite: 3.0.8 - packages/kit/test/prerendering/basics: specifiers: '@sveltejs/kit': workspace:* From c6a7f6d9d9a4e8cc776686d35851f360a260c120 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 22:27:07 -0600 Subject: [PATCH 09/20] fix: Remove console.log --- packages/kit/test/prerendering/basics/src/hooks.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/kit/test/prerendering/basics/src/hooks.js b/packages/kit/test/prerendering/basics/src/hooks.js index 4a0771a2aadb..b8df4f86b0a8 100644 --- a/packages/kit/test/prerendering/basics/src/hooks.js +++ b/packages/kit/test/prerendering/basics/src/hooks.js @@ -1,9 +1,5 @@ import { prerendering } from '$app/env'; -import { env } from '$env/dynamic/private'; - -console.log(env); - const initial_prerendering = prerendering; /** @type {import('@sveltejs/kit').Handle} */ From 7f31360757c852ee9ac8ad01bcd0ae77cf3377fc Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 22:40:03 -0600 Subject: [PATCH 10/20] fix: Import in hooks.js of basics test app --- packages/kit/test/apps/basics/src/hooks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/test/apps/basics/src/hooks.js b/packages/kit/test/apps/basics/src/hooks.js index d6b9caa9d274..fe4b94752d81 100644 --- a/packages/kit/test/apps/basics/src/hooks.js +++ b/packages/kit/test/apps/basics/src/hooks.js @@ -1,6 +1,6 @@ import fs from 'fs'; import cookie from 'cookie'; -import { sequence } from '../../../../src/hooks'; +import { sequence } from '@sveltejs/kit/hooks'; /** @type {import('@sveltejs/kit').HandleError} */ export const handleError = ({ event, error }) => { From 3fba16e7e8b94c4e77f65d4205fae1c7e4a8f768 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Mon, 22 Aug 2022 22:46:01 -0600 Subject: [PATCH 11/20] changeset --- .changeset/curvy-students-march.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .changeset/curvy-students-march.md diff --git a/.changeset/curvy-students-march.md b/.changeset/curvy-students-march.md new file mode 100644 index 000000000000..f413cf8e53c3 --- /dev/null +++ b/.changeset/curvy-students-march.md @@ -0,0 +1,10 @@ +--- +'@sveltejs/adapter-cloudflare': patch +'@sveltejs/adapter-cloudflare-workers': patch +'@sveltejs/adapter-netlify': patch +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-vercel': patch +'@sveltejs/kit': patch +--- + +[feat] Moved hooks.js initialization from Server.respond into Server.init From 2c7e2a8f5c36afbc9650409d637573a7f49c9653 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 09:42:00 -0600 Subject: [PATCH 12/20] Update packages/adapter-netlify/src/serverless.js Co-authored-by: Rich Harris --- packages/adapter-netlify/src/serverless.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/adapter-netlify/src/serverless.js b/packages/adapter-netlify/src/serverless.js index 599b1ef8209b..f898796b8902 100644 --- a/packages/adapter-netlify/src/serverless.js +++ b/packages/adapter-netlify/src/serverless.js @@ -15,10 +15,6 @@ export function init(manifest) { return async (event, context) => { if (init_promise !== null) { - // dear Rich, - // should we just await it indiscriminately? - // sincerely, - // me await init_promise; init_promise = null; } From 9583fbcc7f476cebabcb8151bdb8d3e009b68f82 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 09:42:48 -0600 Subject: [PATCH 13/20] Update packages/kit/src/vite/build/build_server.js Co-authored-by: Rich Harris --- packages/kit/src/vite/build/build_server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index f41f261707b9..623a7735acce 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -100,7 +100,7 @@ export class Server { this.options.public_env = pub; - if (!this.options.hooks) { + if (!this.options.hooks) { const module = await import(${s(hooks)}); this.options.hooks = { handle: module.handle || (({ event, resolve }) => resolve(event)), From 6cb779ca202814d62acfb7edbfe115c1fb437191 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 10:36:29 -0600 Subject: [PATCH 14/20] feat: Enable top-level await in `adapter-node` --- packages/adapter-node/src/handler.js | 11 +---------- packages/adapter-node/tsconfig.json | 3 ++- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 62d1cd6853d0..380af8922e6d 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -13,7 +13,7 @@ import { env } from './env.js'; const server = new Server(manifest); // Dear Rich, // it says top-level await isn't available. Should it be? -let init_promise = server.init({ env: process.env }); +await server.init({ env: process.env }); const origin = env('ORIGIN', undefined); const xff_depth = parseInt(env('XFF_DEPTH', '1')); @@ -23,14 +23,6 @@ const host_header = env('HOST_HEADER', 'host').toLowerCase(); const __dirname = path.dirname(fileURLToPath(import.meta.url)); -/** @type {import('polka').Middleware} */ -const init = async () => { - if (init_promise !== null) { - await init_promise; - init_promise = null; - } -}; - /** * @param {string} path * @param {boolean} client @@ -142,7 +134,6 @@ function get_origin(headers) { export const handler = sequence( [ - init, serve(path.join(__dirname, '/client'), true), serve(path.join(__dirname, '/static')), serve(path.join(__dirname, '/prerendered')), diff --git a/packages/adapter-node/tsconfig.json b/packages/adapter-node/tsconfig.json index 89ca95cf92bd..3048ddd3034a 100644 --- a/packages/adapter-node/tsconfig.json +++ b/packages/adapter-node/tsconfig.json @@ -6,7 +6,8 @@ "noImplicitAny": true, "allowSyntheticDefaultImports": true, "moduleResolution": "node", - "module": "es2020", + "module": "es2022", + "target": "es2017", "baseUrl": ".", "paths": { "@sveltejs/kit": ["../kit/types/index"] From 8ef2f83f40750673cf4c58de8a7e58db293bfab3 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 10:36:56 -0600 Subject: [PATCH 15/20] feat: Add warning comment for future devs --- packages/kit/src/vite/build/build_server.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index f41f261707b9..dcf485ef0439 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -84,6 +84,11 @@ export class Server { }; } + /** + * Take care: Some adapters may have to call \`Server.init\` per-request to set env vars, + * so anything that shouldn't be rerun should be wrapped in an \`if\` block to make sure it hasn't + * been done already. + */ async init({ env }) { const entries = Object.entries(env); From 7afab3ee0a9c85fb93abf6ce8f16c056cc996f3e Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 10:37:13 -0600 Subject: [PATCH 16/20] fix: Init env prior to hooks --- packages/kit/src/vite/dev/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js index a79460c29477..a60952cfd358 100644 --- a/packages/kit/src/vite/dev/index.js +++ b/packages/kit/src/vite/dev/index.js @@ -32,6 +32,13 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { sync.init(svelte_config, vite_config.mode); + const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`); + const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`); + + const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix); + set_private_env(env.private); + set_public_env(env.public); + /** @type {Partial} */ const user_hooks = resolve_entry(svelte_config.kit.files.hooks) ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) @@ -282,13 +289,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { } }); - const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`); - const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`); - - const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix); - set_private_env(env.private); - set_public_env(env.public); - return () => { const serve_static_middleware = vite.middlewares.stack.find( (middleware) => From a8c62b2403de881a0a1bdc5ccbe203e443933260 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 10:38:35 -0600 Subject: [PATCH 17/20] fix: Remove dear Rich --- packages/adapter-node/src/handler.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 380af8922e6d..d90bce52d996 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -11,8 +11,6 @@ import { env } from './env.js'; /* global ENV_PREFIX */ const server = new Server(manifest); -// Dear Rich, -// it says top-level await isn't available. Should it be? await server.init({ env: process.env }); const origin = env('ORIGIN', undefined); const xff_depth = parseInt(env('XFF_DEPTH', '1')); From 580ffd294dbf61bee8ebb37a5e6c5680976bb5b1 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 10:47:03 -0600 Subject: [PATCH 18/20] fix: Apparently didn't like my tsconfig --- packages/adapter-node/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-node/tsconfig.json b/packages/adapter-node/tsconfig.json index 3048ddd3034a..63ea97316689 100644 --- a/packages/adapter-node/tsconfig.json +++ b/packages/adapter-node/tsconfig.json @@ -7,7 +7,7 @@ "allowSyntheticDefaultImports": true, "moduleResolution": "node", "module": "es2022", - "target": "es2017", + "target": "es2017", "baseUrl": ".", "paths": { "@sveltejs/kit": ["../kit/types/index"] From e9286adf42c1063c1f95bc260f294db5f4e4e813 Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 23 Aug 2022 13:00:11 -0600 Subject: [PATCH 19/20] fix: Move env and hooks initialization later --- packages/kit/src/vite/dev/index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js index 29dbc80350d9..51c1f0ba9e86 100644 --- a/packages/kit/src/vite/dev/index.js +++ b/packages/kit/src/vite/dev/index.js @@ -32,18 +32,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { sync.init(svelte_config, vite_config.mode); - const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`); - const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`); - - const env = get_env(svelte_config.kit.env, vite_config.mode); - set_private_env(env.private); - set_public_env(env.public); - - /** @type {Partial} */ - const user_hooks = resolve_entry(svelte_config.kit.files.hooks) - ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) - : {}; - /** @type {import('types').Respond} */ const respond = (await import(`${runtime_prefix}/server/index.js`)).respond; @@ -289,6 +277,18 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { } }); + const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`); + const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`); + + const env = get_env(svelte_config.kit.env, vite_config.mode); + set_private_env(env.private); + set_public_env(env.public); + + /** @type {Partial} */ + const user_hooks = resolve_entry(svelte_config.kit.files.hooks) + ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) + : {}; + return () => { const serve_static_middleware = vite.middlewares.stack.find( (middleware) => From 5b9b0db6007abc75aa35a33cc55069582e0f3686 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 24 Aug 2022 10:23:01 -0400 Subject: [PATCH 20/20] move user_hooks init back from whence it came --- packages/kit/src/vite/dev/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/vite/dev/index.js b/packages/kit/src/vite/dev/index.js index 51c1f0ba9e86..9cbfb128e65d 100644 --- a/packages/kit/src/vite/dev/index.js +++ b/packages/kit/src/vite/dev/index.js @@ -284,11 +284,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { set_private_env(env.private); set_public_env(env.public); - /** @type {Partial} */ - const user_hooks = resolve_entry(svelte_config.kit.files.hooks) - ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) - : {}; - return () => { const serve_static_middleware = vite.middlewares.stack.find( (middleware) => @@ -323,6 +318,11 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) { ); } + /** @type {Partial} */ + const user_hooks = resolve_entry(svelte_config.kit.files.hooks) + ? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`) + : {}; + const handle = user_hooks.handle || (({ event, resolve }) => resolve(event)); /** @type {import('types').Hooks} */