Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename app.render to server.respond #4034

Merged
merged 5 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/late-bobcats-brake.md
Original file line number Diff line number Diff line change
@@ -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
---

Rename app.render to server.respond
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare-workers/files/entry.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'APP' {
export { App } from '@sveltejs/kit';
declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -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}/`;

Expand Down Expand Up @@ -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 });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
});
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare/files/worker.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'APP' {
export { App } from '@sveltejs/kit';
declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare/files/worker.js
Original file line number Diff line number Diff line change
@@ -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}/`;

Expand Down Expand Up @@ -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 });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-netlify/src/handler.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare module '0APP' {
export { App } from '@sveltejs/kit';
declare module '0SERVER' {
export { Server } from '@sveltejs/kit';
}
6 changes: 3 additions & 3 deletions packages/adapter-netlify/src/handler.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export default [
format: 'esm'
},
plugins: [nodeResolve(), commonjs(), json()],
external: ['APP', 'MANIFEST', ...require('module').builtinModules]
external: ['SERVER', 'MANIFEST', ...require('module').builtinModules]
}
];
4 changes: 2 additions & 2 deletions packages/adapter-node/src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'APP' {
export { App } from '@sveltejs/kit';
declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-vercel/files/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'APP' {
export { App } from '@sveltejs/kit';
declare module 'SERVER' {
export { Server } from '@sveltejs/kit';
}

declare module 'MANIFEST' {
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-vercel/files/entry.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
};
2 changes: 1 addition & 1 deletion packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default function ({ external = [] } = {}) {

builder.copy(files, tmp, {
replace: {
APP: `${relativePath}/app.js`,
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/src/core/adapt/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);

Expand Down Expand Up @@ -145,7 +145,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
/** @type {Map<string, import('types').PrerenderDependency>} */
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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -139,7 +139,7 @@ export async function build_server(

/** @type {Record<string, string>} */
const input = {
app: `${build_dir}/app.js`
index: `${build_dir}/index.js`
};

// add entry points for every endpoint...
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
Expand All @@ -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);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import {
} from './internal';
import './ambient';

export class App {
constructor(manifest: SSRManifest);
render(request: Request, options?: RequestOptions): Promise<Response>;
}

export interface Adapter {
name: string;
adapt(builder: Builder): Promise<void>;
Expand Down Expand Up @@ -371,6 +366,11 @@ export interface RequestOptions {

export type ResolveOptions = Partial<RequiredResolveOptions>;

export class Server {
constructor(manifest: SSRManifest);
respond(request: Request, options?: RequestOptions): Promise<Response>;
}

export interface SSRManifest {
appDir: string;
assets: Set<string>;
Expand Down
12 changes: 6 additions & 6 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
Handle,
HandleError,
RequestEvent,
App,
RequestOptions,
PrerenderErrorHandler
PrerenderErrorHandler,
Server
} from './index';

export interface AdapterEntry {
Expand All @@ -38,8 +38,8 @@ export interface AdapterEntry {
}) => void;
}

export interface AppModule {
App: typeof InternalApp;
export interface ServerModule {
Server: typeof InternalServer;

override(options: {
paths: {
Expand Down Expand Up @@ -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;
Expand Down