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

switch handle hook from positional arguments to named arguments #959

Merged
merged 7 commits into from
Apr 12, 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/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 = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_hooks seemed to be an unnecessary indirection, and I've removed it while updating this.

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