Skip to content

Commit 7de8a8a

Browse files
committed
merge
2 parents 98dbe99 + 315b643 commit 7de8a8a

File tree

48 files changed

+2676
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2676
-54
lines changed

.changeset/cool-beans-only.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix `write_types` on windows using posixify()

.changeset/cyan-years-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Prevent caching of `__data.js` files

.changeset/healthy-rivers-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/adapter-cloudflare": patch
3+
---
4+
5+
[fix] return 404 instead of 200 for missing assets

.changeset/lazy-mice-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[feat] allow +server.js files next to +page files

.changeset/pre.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@
445445
"healthy-penguins-fail",
446446
"healthy-pigs-fly",
447447
"healthy-pumpkins-bathe",
448+
"healthy-rivers-share",
448449
"healthy-vans-build",
449450
"healthy-wasps-poke",
450451
"heavy-donuts-grab",
@@ -538,6 +539,7 @@
538539
"lazy-ants-watch",
539540
"lazy-berries-decide",
540541
"lazy-carpets-join",
542+
"lazy-mice-remain",
541543
"lazy-mice-smile",
542544
"lazy-owls-run",
543545
"lazy-spies-wash",
@@ -979,6 +981,7 @@
979981
"silver-hounds-clean",
980982
"silver-lemons-fail",
981983
"silver-needles-judge",
984+
"silver-pugs-worry",
982985
"silver-rice-flash",
983986
"silver-scissors-peel",
984987
"silver-toes-cheer",
@@ -1069,6 +1072,7 @@
10691072
"spotty-phones-love",
10701073
"spotty-ties-love",
10711074
"spotty-timers-fix",
1075+
"spotty-trainers-switch",
10721076
"spotty-vans-tickle",
10731077
"stale-apples-cry",
10741078
"stale-crabs-carry",

.changeset/silver-pugs-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[fix] tighten up handling and documentation around 404
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Decode HTML entities in `href` attributes when crawling
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Make url property getters non-enumerable

documentation/docs/03-routing.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,57 @@ The first argument to `Response` can be a [`ReadableStream`](https://developer.m
267267
268268
You can use the `error`, `redirect` and `json` methods from `@sveltejs/kit` for convenience (but you don't have to). Note that `throw error(..)` only returns a plain text error response.
269269
270+
#### Receiving data
271+
272+
By exporting `POST`/`PUT`/`PATCH`/`DELETE` handlers, `+server.js` files can be used to create a complete API:
273+
274+
```svelte
275+
/// file: src/routes/add/+page.svelte
276+
<script>
277+
let a = 0;
278+
let b = 0;
279+
let total = 0;
280+
281+
async function add() {
282+
const response = await fetch('/api/add', {
283+
method: 'POST',
284+
body: JSON.stringify({ a, b }),
285+
headers: {
286+
'content-type': 'application/json'
287+
}
288+
});
289+
290+
total = await response.json();
291+
}
292+
</script>
293+
294+
<input type="number" bind:value={a}> +
295+
<input type="number" bind:value={b}> =
296+
{total}
297+
298+
<button on:click={add}>Calculate</button>
299+
```
300+
301+
```js
302+
/// file: src/routes/api/add/+server.js
303+
import { json } from '@sveltejs/kit';
304+
305+
/** @type {import('./$types').RequestHandler} */
306+
export async function POST({ request }) {
307+
const { a, b } = await request.json();
308+
return json(a + b);
309+
}
310+
```
311+
312+
> In general, [form actions](/docs/form-actions) are a better way to submit data from the browser to the server.
313+
314+
#### Content negotiation
315+
316+
`+server.js` files can be placed in the same directory as `+page` files, allowing the same route to be either a page or an API endpoint. To determine which, SvelteKit applies the following rules:
317+
318+
- `PUT`/`PATCH`/`DELETE` requests are always handled by `+server.js` since they do not apply to pages
319+
- `GET`/`POST` requests are treated as page requests if the `accept` header prioritises `text/html` (in other words, it's a browser page request), else they are handled by `+server.js`
320+
270321
### $types
271322
272323
Throughout the examples above, we've been importing types from a `$types.d.ts` file. This is a file SvelteKit creates for you in a hidden directory if you're using TypeScript (or JavaScript with JSDoc type annotations) to give you type safety when working with your root files.

documentation/docs/04-advanced-routing.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ If the number of route segments is unknown, you can use rest syntax — for exam
2222
}
2323
```
2424

25-
This also allows you to render custom 404s. Given these routes...
25+
> `src/routes/a/[...rest]/z/+page.svelte` will match `/a/z` (i.e. there's no parameter at all) as well as `/a/b/z` and `/a/b/c/z` and so on. Make sure you check that the value of the rest parameter is valid, for example using a [matcher](#matching).
26+
27+
#### 404 pages
28+
29+
Rest parameters also allow you to render custom 404s. Given these routes...
2630

2731
```
2832
src/routes/
@@ -47,7 +51,17 @@ src/routes/
4751
└ +error.svelte
4852
```
4953

50-
> `src/routes/a/[...rest]/z/+page.svelte` will match `/a/z` (i.e. there's no parameter at all) as well as `/a/b/z` and `/a/b/c/z` and so on. Make sure you check that the value of the rest parameter is valid, for example using a [matcher](#matching).
54+
```js
55+
/// file: src/routes/marx-brothers/[...path]/+page.js
56+
import { error } from '@sveltejs/kit';
57+
58+
/** @type {import('./$types').PageLoad} */
59+
export function load(event) {
60+
throw error(404, 'Not Found');
61+
}
62+
```
63+
64+
> If you don't handle 404 cases, they will appear in [`handleError`](/docs/hooks#shared-hooks-handleerror)
5165
5266
### Matching
5367

0 commit comments

Comments
 (0)