Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/sveltejs/kit into refacto…
Browse files Browse the repository at this point in the history
…r/cutomExtensionTest

* 'master' of https://github.com/sveltejs/kit:
  fix: append trailing slash in manifest (sveltejs#1507)
  Version Packages (next) (sveltejs#1590)
  Rename `handle`'s `render` parameter to `resolve` (sveltejs#1566)
  Replace favicon (sveltejs#1589)
  • Loading branch information
sidharthv96 committed May 30, 2021
2 parents 03ea277 + 1bb44ca commit a69c231
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-lions-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Replace favicon
3 changes: 3 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@
"heavy-lamps-explode",
"heavy-papayas-smile",
"hot-keys-walk",
"hot-kings-confess",
"hungry-lemons-scream",
"itchy-beers-burn",
"itchy-birds-admire",
"itchy-lobsters-tie",
"khaki-ears-repeat",
"khaki-lions-sell",
"khaki-socks-tan",
"khaki-wolves-shout",
"kind-steaks-bake",
Expand Down Expand Up @@ -263,6 +265,7 @@
"tasty-donkeys-wait",
"ten-mice-kneel",
"ten-plants-sleep",
"tender-buckets-turn",
"thick-meals-attend",
"thin-avocados-visit",
"thin-coins-move",
Expand Down
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
7 changes: 7 additions & 0 deletions packages/create-svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# create-svelte

## 2.0.0-next.73

### Patch Changes

- 2d2fab1: Add favicon to skeleton template
- 6aa4988: Replace favicon

## 2.0.0-next.72

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-svelte",
"version": "2.0.0-next.72",
"version": "2.0.0-next.73",
"bin": "./bin.js",
"dependencies": {
"kleur": "^4.1.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/default/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<link rel="icon" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

%svelte.head%
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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/skeleton/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<link rel="icon" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sveltejs/kit

## 1.0.0-next.111

### Patch Changes

- eae1b1d: Rename handle's render parameter to resolve

## 1.0.0-next.110

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.110",
"version": "1.0.0-next.111",
"type": "module",
"dependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.10",
Expand Down
15 changes: 15 additions & 0 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,26 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
}
});

/**
* @param {string} path
*/
function normalize(path) {
if (config.kit.trailingSlash === 'always') {
return path.endsWith('/') ? path : `${path}/`;
} else if (config.kit.trailingSlash === 'never') {
return !path.endsWith('/') || path === '/' ? path : path.slice(0, -1);
}

return path;
}

/**
* @param {string} path
* @param {string} parent
*/
async function visit(path, parent) {
path = normalize(path);

if (seen.has(path)) return;
seen.add(path);

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 a69c231

Please sign in to comment.