diff --git a/.changeset/late-bobcats-brake.md b/.changeset/late-bobcats-brake.md new file mode 100644 index 000000000000..3f151db82ea4 --- /dev/null +++ b/.changeset/late-bobcats-brake.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 +--- + +[breaking] rename `app.render` to `server.respond` diff --git a/packages/adapter-cloudflare-workers/files/entry.d.ts b/packages/adapter-cloudflare-workers/files/entry.d.ts index 9a61c190ac9e..9df439aab626 100644 --- a/packages/adapter-cloudflare-workers/files/entry.d.ts +++ b/packages/adapter-cloudflare-workers/files/entry.d.ts @@ -1,5 +1,5 @@ -declare module 'APP' { - export { App } from '@sveltejs/kit'; +declare module 'SERVER' { + export { Server } from '@sveltejs/kit'; } declare module 'MANIFEST' { diff --git a/packages/adapter-cloudflare-workers/files/entry.js b/packages/adapter-cloudflare-workers/files/entry.js index 2ce945ce377c..633e28eab676 100644 --- a/packages/adapter-cloudflare-workers/files/entry.js +++ b/packages/adapter-cloudflare-workers/files/entry.js @@ -1,8 +1,8 @@ -import { App } from 'APP'; +import { Server } from 'SERVER'; import { manifest, prerendered } from 'MANIFEST'; import { getAssetFromKV } from '@cloudflare/kv-asset-handler'; -const app = new App(manifest); +const server = new Server(manifest); const prefix = `/${manifest.appDir}/`; @@ -50,7 +50,7 @@ async function handle(event) { // dynamically-generated pages try { - return await app.render(request); + return await server.respond(request); } catch (e) { return new Response('Error rendering route:' + (e.message || e.toString()), { status: 500 }); } diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index e7e61a3735e3..745bbf006b11 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -40,7 +40,7 @@ export default function () { builder.copy(`${files}/entry.js`, `${tmp}/entry.js`, { replace: { - APP: `${relativePath}/app.js`, + SERVER: `${relativePath}/index.js`, MANIFEST: './manifest.js' } }); diff --git a/packages/adapter-cloudflare/files/worker.d.ts b/packages/adapter-cloudflare/files/worker.d.ts index ea54b4bd2e83..3689c3f5e51e 100644 --- a/packages/adapter-cloudflare/files/worker.d.ts +++ b/packages/adapter-cloudflare/files/worker.d.ts @@ -1,5 +1,5 @@ -declare module 'APP' { - export { App } from '@sveltejs/kit'; +declare module 'SERVER' { + export { Server } from '@sveltejs/kit'; } declare module 'MANIFEST' { diff --git a/packages/adapter-cloudflare/files/worker.js b/packages/adapter-cloudflare/files/worker.js index fd256f13b1dd..d41d6c27c269 100644 --- a/packages/adapter-cloudflare/files/worker.js +++ b/packages/adapter-cloudflare/files/worker.js @@ -1,7 +1,7 @@ -import { App } from 'APP'; +import { Server } from 'SERVER'; import { manifest, prerendered } from 'MANIFEST'; -const app = new App(manifest); +const server = new Server(manifest); const prefix = `/${manifest.appDir}/`; @@ -50,7 +50,7 @@ export default { // dynamically-generated pages try { - return await app.render(req, { platform: { env, context } }); + return await server.respond(req, { platform: { env, context } }); } catch (e) { return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 }); } diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index a44a55697d9d..9af0566f94ce 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -32,7 +32,7 @@ export default function (options = {}) { builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, { replace: { - APP: `${relativePath}/app.js`, + SERVER: `${relativePath}/index.js`, MANIFEST: './manifest.js' } }); diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 3ebb45324d03..a37894a88c75 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -51,7 +51,7 @@ export default function ({ split = false } = {}) { const redirects = []; const replace = { - '0APP': './server/app.js' // digit prefix prevents CJS build from using this as a variable name, which would also get replaced + '0SERVER': './server/index.js' // digit prefix prevents CJS build from using this as a variable name, which would also get replaced }; if (esm) { diff --git a/packages/adapter-netlify/src/handler.d.ts b/packages/adapter-netlify/src/handler.d.ts index 75ea691df713..2501e38bf35a 100644 --- a/packages/adapter-netlify/src/handler.d.ts +++ b/packages/adapter-netlify/src/handler.d.ts @@ -1,3 +1,3 @@ -declare module '0APP' { - export { App } from '@sveltejs/kit'; +declare module '0SERVER' { + export { Server } from '@sveltejs/kit'; } diff --git a/packages/adapter-netlify/src/handler.js b/packages/adapter-netlify/src/handler.js index e23c8f96d17b..fcf928145ccf 100644 --- a/packages/adapter-netlify/src/handler.js +++ b/packages/adapter-netlify/src/handler.js @@ -1,15 +1,15 @@ import './shims'; -import { App } from '0APP'; +import { Server } from '0SERVER'; /** * @param {import('@sveltejs/kit').SSRManifest} manifest * @returns {import('@netlify/functions').Handler} */ export function init(manifest) { - const app = new App(manifest); + const server = new Server(manifest); return async (event) => { - const rendered = await app.render(to_request(event)); + const rendered = await server.respond(to_request(event)); const partial_response = { statusCode: rendered.status, diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 70898ffcd033..8fd7059511d8 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -49,7 +49,7 @@ export default function ({ builder.copy(files, out, { replace: { - APP: './server/app.js', + SERVER: './server/index.js', MANIFEST: './manifest.js', PATH_ENV: JSON.stringify(path_env), HOST_ENV: JSON.stringify(host_env), diff --git a/packages/adapter-node/rollup.config.js b/packages/adapter-node/rollup.config.js index 21da6b8acaa4..9bd5c9970fab 100644 --- a/packages/adapter-node/rollup.config.js +++ b/packages/adapter-node/rollup.config.js @@ -14,6 +14,6 @@ export default [ format: 'esm' }, plugins: [nodeResolve(), commonjs(), json()], - external: ['APP', 'MANIFEST', ...require('module').builtinModules] + external: ['SERVER', 'MANIFEST', ...require('module').builtinModules] } ]; diff --git a/packages/adapter-node/src/ambient.d.ts b/packages/adapter-node/src/ambient.d.ts index 42c4f9f12309..537f7cc041d1 100644 --- a/packages/adapter-node/src/ambient.d.ts +++ b/packages/adapter-node/src/ambient.d.ts @@ -1,5 +1,5 @@ -declare module 'APP' { - export { App } from '@sveltejs/kit'; +declare module 'SERVER' { + export { Server } from '@sveltejs/kit'; } declare module 'MANIFEST' { diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 3357a9e2120d..f3b9662dddd9 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -4,12 +4,12 @@ import path from 'path'; import sirv from 'sirv'; import { fileURLToPath } from 'url'; import { getRequest, setResponse } from '@sveltejs/kit/node'; -import { App } from 'APP'; +import { Server } from 'SERVER'; import { manifest } from 'MANIFEST'; /* global ORIGIN, PROTOCOL_HEADER, HOST_HEADER */ -const app = new App(manifest); +const server = new Server(manifest); const origin = ORIGIN; const protocol_header = PROTOCOL_HEADER && process.env[PROTOCOL_HEADER]; const host_header = (HOST_HEADER && process.env[HOST_HEADER]) || 'host'; @@ -45,7 +45,7 @@ const ssr = async (req, res) => { return res.end(err.reason || 'Invalid request body'); } - setResponse(res, await app.render(request)); + setResponse(res, await server.respond(request)); }; /** @param {import('polka').Middleware[]} handlers */ diff --git a/packages/adapter-vercel/files/ambient.d.ts b/packages/adapter-vercel/files/ambient.d.ts index 42c4f9f12309..537f7cc041d1 100644 --- a/packages/adapter-vercel/files/ambient.d.ts +++ b/packages/adapter-vercel/files/ambient.d.ts @@ -1,5 +1,5 @@ -declare module 'APP' { - export { App } from '@sveltejs/kit'; +declare module 'SERVER' { + export { Server } from '@sveltejs/kit'; } declare module 'MANIFEST' { diff --git a/packages/adapter-vercel/files/entry.js b/packages/adapter-vercel/files/entry.js index 9b42dbc79320..56f409c1a6fa 100644 --- a/packages/adapter-vercel/files/entry.js +++ b/packages/adapter-vercel/files/entry.js @@ -1,9 +1,9 @@ import './shims'; import { getRequest, setResponse } from '@sveltejs/kit/node'; -import { App } from 'APP'; +import { Server } from 'SERVER'; import { manifest } from 'MANIFEST'; -const app = new App(manifest); +const server = new Server(manifest); /** * @param {import('http').IncomingMessage} req @@ -19,5 +19,5 @@ export default async (req, res) => { return res.end(err.reason || 'Invalid request body'); } - setResponse(res, await app.render(request)); + setResponse(res, await server.respond(request)); }; diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 7ed2a5c72186..d9c69de89d2e 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -112,7 +112,7 @@ export default function ({ external = [] } = {}) { builder.copy(files, tmp, { replace: { - APP: `${relativePath}/app.js`, + SERVER: `${relativePath}/index.js`, MANIFEST: './manifest.js' } }); diff --git a/packages/kit/src/core/adapt/fixtures/basic/.svelte-kit/output/server/app.js b/packages/kit/src/core/adapt/fixtures/basic/.svelte-kit/output/server/index.js similarity index 100% rename from packages/kit/src/core/adapt/fixtures/basic/.svelte-kit/output/server/app.js rename to packages/kit/src/core/adapt/fixtures/basic/.svelte-kit/output/server/index.js diff --git a/packages/kit/src/core/adapt/prerender/prerender.js b/packages/kit/src/core/adapt/prerender/prerender.js index e65e6fd6f95a..b950afeda76b 100644 --- a/packages/kit/src/core/adapt/prerender/prerender.js +++ b/packages/kit/src/core/adapt/prerender/prerender.js @@ -67,8 +67,8 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a const server_root = resolve_path(cwd, `${SVELTE_KIT}/output`); - /** @type {import('types').AppModule} */ - const { App, override } = await import(pathToFileURL(`${server_root}/server/app.js`).href); + /** @type {import('types').ServerModule} */ + const { Server, override } = await import(pathToFileURL(`${server_root}/server/index.js`).href); const { manifest } = await import(pathToFileURL(`${server_root}/server/manifest.js`).href); override({ @@ -77,7 +77,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a read: (file) => readFileSync(join(config.kit.files.assets, file)) }); - const app = new App(manifest); + const server = new Server(manifest); const error = normalise_error_handler(log, config.kit.prerender.onError); @@ -145,7 +145,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a /** @type {Map} */ const dependencies = new Map(); - const response = await app.render(new Request(`http://sveltekit-prerender${encoded}`), { + const response = await server.respond(new Request(`http://sveltekit-prerender${encoded}`), { prerender: { all, dependencies @@ -282,7 +282,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a } if (fallback) { - const rendered = await app.render(new Request('http://sveltekit-prerender/[fallback]'), { + const rendered = await server.respond(new Request('http://sveltekit-prerender/[fallback]'), { prerender: { fallback, all: false, diff --git a/packages/kit/src/core/build/build_server.js b/packages/kit/src/core/build/build_server.js index c9e3dc1cd085..dfc2cce9acf1 100644 --- a/packages/kit/src/core/build/build_server.js +++ b/packages/kit/src/core/build/build_server.js @@ -18,7 +18,7 @@ import { s } from '../../utils/misc.js'; * }} opts * @returns */ -const app_template = ({ config, hooks, has_service_worker, template }) => ` +const server_template = ({ config, hooks, has_service_worker, template }) => ` import root from '__GENERATED__/root.svelte'; import { respond } from '${runtime}/server/index.js'; import { set_paths, assets, base } from '${runtime}/paths.js'; @@ -55,7 +55,7 @@ export function override(settings) { read = settings.read; } -export class App { +export class Server { constructor(manifest) { const hooks = get_hooks(user_hooks); @@ -95,7 +95,7 @@ export class App { }; } - render(request, options = {}) { + respond(request, options = {}) { if (!(request instanceof Request)) { throw new Error('The first argument to app.render must be a Request object. See https://github.com/sveltejs/kit/pull/3384 for details'); } @@ -139,7 +139,7 @@ export async function build_server( /** @type {Record} */ const input = { - app: `${build_dir}/app.js` + index: `${build_dir}/index.js` }; // add entry points for every endpoint... @@ -172,8 +172,8 @@ export async function build_server( }; fs.writeFileSync( - input.app, - app_template({ + input.index, + server_template({ config, hooks: app_relative(hooks_file), has_service_worker: service_worker_register && !!service_worker_entry_file, diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 60e1e000b7d4..577775919e8a 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -33,11 +33,11 @@ export async function preview({ }) { __fetch_polyfill(); - const app_file = resolve(cwd, `${SVELTE_KIT}/output/server/app.js`); + const index_file = resolve(cwd, `${SVELTE_KIT}/output/server/index.js`); const manifest_file = resolve(cwd, `${SVELTE_KIT}/output/server/manifest.js`); - /** @type {import('types').AppModule} */ - const { App, override } = await import(pathToFileURL(app_file).href); + /** @type {import('types').ServerModule} */ + const { Server, override } = await import(pathToFileURL(index_file).href); const { manifest } = await import(pathToFileURL(manifest_file).href); @@ -66,12 +66,12 @@ export async function preview({ read: (file) => fs.readFileSync(join(config.kit.files.assets, file)) }); - const app = new App(manifest); + const server = new Server(manifest); /** @type {import('vite').UserConfig} */ const vite_config = (config.kit.vite && (await config.kit.vite())) || {}; - const server = await get_server(use_https, vite_config, (req, res) => { + const http_server = await get_server(use_https, vite_config, (req, res) => { if (req.url == null) { throw new Error('Invalid request url'); } @@ -93,7 +93,7 @@ export async function preview({ return res.end(err.reason || 'Invalid request body'); } - setResponse(res, await app.render(request)); + setResponse(res, await server.respond(request)); } else { res.statusCode = 404; res.end('Not found'); @@ -120,9 +120,9 @@ export async function preview({ } }); - await server.listen(port, host || '0.0.0.0'); + await http_server.listen(port, host || '0.0.0.0'); - return Promise.resolve(server); + return Promise.resolve(http_server); } /** diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index a0031a8da805..51383029a9b4 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -20,11 +20,6 @@ import { } from './internal'; import './ambient'; -export class App { - constructor(manifest: SSRManifest); - render(request: Request, options?: RequestOptions): Promise; -} - export interface Adapter { name: string; adapt(builder: Builder): Promise; @@ -371,6 +366,11 @@ export interface RequestOptions { export type ResolveOptions = Partial; +export class Server { + constructor(manifest: SSRManifest); + respond(request: Request, options?: RequestOptions): Promise; +} + export interface SSRManifest { appDir: string; assets: Set; diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 76cb3a2dccc2..cf409dbb141a 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -9,9 +9,9 @@ import { Handle, HandleError, RequestEvent, - App, RequestOptions, - PrerenderErrorHandler + PrerenderErrorHandler, + Server } from './index'; export interface AdapterEntry { @@ -38,8 +38,8 @@ export interface AdapterEntry { }) => void; } -export interface AppModule { - App: typeof InternalApp; +export interface ServerModule { + Server: typeof InternalServer; override(options: { paths: { @@ -117,8 +117,8 @@ export interface Hooks { export type HttpMethod = 'get' | 'head' | 'post' | 'put' | 'delete' | 'patch'; -export class InternalApp extends App { - render( +export class InternalServer extends Server { + respond( request: Request, options?: RequestOptions & { prerender?: PrerenderOptions;