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 handle's render parameter to resolve #1566

Merged
merged 3 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -187,7 +187,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>;