Skip to content

Commit

Permalink
Rename handle's render parameter to resolve (#1566)
Browse files Browse the repository at this point in the history
* Rename handle's render parameter to respond

* Rename to resolve

* Update 04-hooks.md

remove reference to 'renderer', which is confusing now that we no longer use `render`

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
  • Loading branch information
benmccann and Rich-Harris authored May 29, 2021
1 parent 6aa4988 commit eae1b1d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-buckets-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Rename handle's render parameter to resolve
10 changes: 5 additions & 5 deletions documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ An optional `src/hooks.js` (or `src/hooks.ts`, or `src/hooks/index.js`) file exp
### handle

This function runs on every request, and determines the response. It receives the `request` object and `render` method, which calls SvelteKit's default renderer. This allows you to modify response headers or bodies, or bypass SvelteKit entirely (for implementing endpoints programmatically, for example).
This function runs on every request, for both pages and endpoints, and determines the response. It receives the `request` object and a function called `resolve`, which invokes SvelteKit's router and generates a response accordingly. This allows you to modify response headers or bodies, or bypass SvelteKit entirely (for implementing endpoints programmatically, for example).

If unimplemented, defaults to `({ request, render }) => render(request)`.
If unimplemented, defaults to `({ request, resolve }) => resolve(request)`.

To add custom data to the request, which is passed to endpoints, populate the `request.locals` object, as shown below.

Expand All @@ -37,16 +37,16 @@ type Response = {

type Handle<Locals = Record<string, any>> = (input: {
request: Request<Locals>;
render: (request: Request<Locals>) => Response | Promise<Response>;
resolve: (request: Request<Locals>) => Response | Promise<Response>;
}) => Response | Promise<Response>;
```

```js
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, render }) {
export async function handle({ request, resolve }) {
request.locals.user = await getUserInformation(request.headers.cookie);

const response = await render(request);
const response = await resolve(request);

return {
...response,
Expand Down
4 changes: 2 additions & 2 deletions packages/create-svelte/templates/default/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cookie from 'cookie';
import { v4 as uuid } from '@lukeed/uuid';
import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ request, render }) => {
export const handle: Handle = async ({ request, resolve }) => {
const cookies = cookie.parse(request.headers.cookie || '');
request.locals.userid = cookies.userid || uuid();

Expand All @@ -11,7 +11,7 @@ export const handle: Handle = async ({ request, render }) => {
request.method = request.query.get('_method').toUpperCase();
}

const response = await render(request);
const response = await resolve(request);

if (!cookies.userid) {
// if this is the first time the user has visited this app,
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ async function build_server(
// named imports without triggering Rollup's missing import detection
const get_hooks = hooks => ({
getSession: hooks.getSession || (() => ({})),
handle: hooks.handle || (({ request, render }) => render(request))
handle: hooks.handle || (({ request, resolve }) => resolve(request))
});
const module_lookup = {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Watcher extends EventEmitter {
},
hooks: {
getSession: hooks.getSession || (() => ({})),
handle: hooks.handle || (({ request, render }) => render(request))
handle: hooks.handle || (({ request, resolve }) => resolve(request))
},
hydrate: this.config.kit.hydrate,
paths: this.config.kit.paths,
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function respond(incoming, options, state = {}) {
params: null,
locals: {}
},
render: async (request) => {
resolve: async (request) => {
if (state.prerender && state.prerender.fallback) {
return await render_response({
options,
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/test/apps/basics/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export function getSession(request) {
}

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, render }) {
export async function handle({ request, resolve }) {
const cookies = cookie.parse(request.headers.cookie || '');

request.locals.answer = 42;
request.locals.name = cookies.name;

const response = await render(request);
const response = await resolve(request);

if (response) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export type GetSession<Locals = Record<string, any>, Session = any> = {

export type Handle<Locals = Record<string, any>> = (input: {
request: ServerRequest<Locals>;
render: (request: ServerRequest<Locals>) => ServerResponse | Promise<ServerResponse>;
resolve: (request: ServerRequest<Locals>) => ServerResponse | Promise<ServerResponse>;
}) => ServerResponse | Promise<ServerResponse>;

0 comments on commit eae1b1d

Please sign in to comment.