Skip to content

Commit

Permalink
switch handle hook from positional arguments to named arguments (#959)
Browse files Browse the repository at this point in the history
* update handle to ({ request, render }) => response

* update hooks test

* update documentation

* add changeset

* Update documentation/docs/04-hooks.md

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* Update documentation/docs/04-hooks.md

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Rich Harris <rich.harris@hey.com>
  • Loading branch information
4 people authored Apr 12, 2021
1 parent b202c1e commit ca108a6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-countries-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Change `handle` hook from positional arguments to named arguments
6 changes: 3 additions & 3 deletions documentation/docs/04-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export function getSession({ context }) {
### handle
This function runs on every request, and determines the response. The second argument, `render`, 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, 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).
If unimplemented, defaults to `(request, render) => render(request)`.
If unimplemented, defaults to `({ request, render }) => render(request)`.
```ts
type Request<Context = any> = {
Expand Down Expand Up @@ -103,7 +103,7 @@ type Handle<Context = any> = (
```js
/** @type {import('@sveltejs/kit').Handle} */
export async function handle(request, render) {
export async function handle({ request, render }) {
const response = await render(request);

return {
Expand Down
12 changes: 5 additions & 7 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,11 @@ async function build_server(
]
};
const get_hooks = hooks => ({
getContext: hooks.getContext || (() => ({})),
getSession: hooks.getSession || (() => ({})),
handle: hooks.handle || ((request, render) => render(request))
});
const hooks = get_hooks(user_hooks);
const hooks = {
getContext: user_hooks.getContext || (() => ({})),
getSession: user_hooks.getSession || (() => ({})),
handle: user_hooks.handle || (({ request, render }) => render(request))
};
const module_lookup = {
${manifest.components.map(file => `${s(file)}: () => import(${s(app_relative(file))})`)}
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 @@ -260,7 +260,7 @@ class Watcher extends EventEmitter {
hooks: {
getContext: hooks.getContext || (() => ({})),
getSession: hooks.getSession || (() => ({})),
handle: hooks.handle || ((request, render) => render(request))
handle: hooks.handle || (({ request, render }) => render(request))
},
only_render_prerenderable_pages: false,
// get_component_path: (id) => `/${id}?import`,
Expand Down
8 changes: 4 additions & 4 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export async function ssr(incoming, options) {
const context = (await options.hooks.getContext(incoming)) || {};

try {
return await options.hooks.handle(
{
return await options.hooks.handle({
request: {
...incoming,
params: null,
context
},
async (request) => {
render: async (request) => {
for (const route of options.manifest.routes) {
if (!route.pattern.test(request.path)) continue;

Expand Down Expand Up @@ -65,7 +65,7 @@ export async function ssr(incoming, options) {

return await render_page(request, null, options);
}
);
});
} catch (e) {
if (e && e.stack) {
e.stack = await options.get_stack(e);
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getSession({ context }) {
}

/** @type {import('../../../../types').Handle} */
export async function handle(request, render) {
export async function handle({ request, render }) {
const response = await render(request);

if (response) {
Expand Down
11 changes: 7 additions & 4 deletions packages/kit/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ export type GetSession<Context = any, Session = any> = {
({ context }: { context: Context }): Session | Promise<Session>;
};

export type Handle<Context = any> = (
request: Request<Context>,
render: (request: Request<Context>) => Response | Promise<Response>
) => Response | Promise<Response>;
export type Handle<Context = any> = ({
request,
render
}: {
request: Request<Context>;
render: (request: Request<Context>) => Response | Promise<Response>;
}) => Response | Promise<Response>;

export type Page = {
host: string;
Expand Down

0 comments on commit ca108a6

Please sign in to comment.