You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/10-getting-started/10-introduction.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
title: Introduction
3
3
---
4
4
5
-
###Before we begin
5
+
## Before we begin
6
6
7
7
> SvelteKit is in release candidate phase for 1.0 while we address reported issues and add polish. If you get stuck, reach out for help in the [Discord chatroom](https://svelte.dev/chat).
8
8
>
9
9
> See the [migration guides](/docs/migrating) for help upgrading from Sapper.
10
10
11
-
###What is SvelteKit?
11
+
## What is SvelteKit?
12
12
13
13
SvelteKit is a framework for building extremely high-performance web apps.
Copy file name to clipboardExpand all lines: documentation/docs/10-getting-started/20-creating-a-project.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,6 @@ There are two basic concepts:
20
20
21
21
Try editing the files to get a feel for how everything works – you may not need to bother reading the rest of this guide!
22
22
23
-
###Editor setup
23
+
## Editor setup
24
24
25
25
We recommend using [Visual Studio Code (aka VS Code)](https://code.visualstudio.com/download) with [the Svelte extension](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode), but [support also exists for numerous other editors](https://sveltesociety.dev/tools#editor-support).
Copy file name to clipboardExpand all lines: documentation/docs/10-getting-started/30-project-structure.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,9 +30,9 @@ my-project/
30
30
31
31
You'll also find common files like `.gitignore` and `.npmrc` (and `.prettierrc` and `.eslintrc.cjs` and so on, if you chose those options when running `npm create svelte@latest`).
32
32
33
-
###Project files
33
+
## Project files
34
34
35
-
####src
35
+
### src
36
36
37
37
The `src` directory contains the meat of your project.
38
38
@@ -53,34 +53,34 @@ The `src` directory contains the meat of your project.
53
53
54
54
You can use `.ts` files instead of `.js` files, if using TypeScript.
55
55
56
-
####static
56
+
### static
57
57
58
58
Any static assets that should be served as-is, like `robots.txt` or `favicon.png`, go in here.
59
59
60
-
####tests
60
+
### tests
61
61
62
62
If you chose to add tests to your project during `npm create svelte@latest`, they will live in this directory.
63
63
64
-
####package.json
64
+
### package.json
65
65
66
66
Your `package.json` file must include `@sveltejs/kit`, `svelte` and `vite` as `devDependencies`.
67
67
68
68
When you create a project with `npm create svelte@latest`, you'll also notice that `package.json` includes `"type": "module"`. This means that `.js` files are interpreted as native JavaScript modules with `import` and `export` keywords. Legacy CommonJS files need a `.cjs` file extension.
69
69
70
-
####svelte.config.js
70
+
### svelte.config.js
71
71
72
72
This file contains your Svelte and SvelteKit [configuration](/docs/configuration).
73
73
74
-
####tsconfig.json
74
+
### tsconfig.json
75
75
76
76
This file (or `jsconfig.json`, if you prefer type-checked `.js` files over `.ts` files) configures TypeScript, if you added typechecking during `npm create svelte@latest`. Since SvelteKit relies on certain configuration being set a specific way, it generates its own `.svelte-kit/tsconfig.json` file which your own config `extends`.
77
77
78
-
####vite.config.js
78
+
### vite.config.js
79
79
80
80
A SvelteKit project is really just a [Vite](https://vitejs.dev) project that uses the [`@sveltejs/kit/vite`](/docs/modules#sveltejs-kit-vite) plugin, along with any other [Vite configuration](https://vitejs.dev/config/).
81
81
82
-
###Other files
82
+
## Other files
83
83
84
-
####.svelte-kit
84
+
### .svelte-kit
85
85
86
86
As you develop and build your project, SvelteKit will generate files in a `.svelte-kit` directory (configurable as [`outDir`](/docs/configuration#outdir)). You can ignore its contents, and delete them at any time (they will be regenerated when you next `dev` or `build`).
Copy file name to clipboardExpand all lines: documentation/docs/10-getting-started/40-web-standards.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,23 +8,23 @@ These APIs are available in all modern browsers and in many non-browser environm
8
8
9
9
In particular, you'll get comfortable with the following:
10
10
11
-
###Fetch APIs
11
+
## Fetch APIs
12
12
13
13
SvelteKit uses [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch) for getting data from the network. It's available in [hooks](/docs/hooks) and [server routes](/docs/routing#server) as well as in the browser.
14
14
15
15
> A special version of `fetch` is available in [`load`](/docs/load) functions for invoking endpoints directly during server-side rendering, without making an HTTP call, while preserving credentials. (To make credentialled fetches in server-side code outside `load`, you must explicitly pass `cookie` and/or `authorization` headers.) It also allows you to make relative requests, whereas server-side `fetch` normally requires a fully qualified URL.
16
16
17
17
Besides `fetch` itself, the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) includes the following interfaces:
18
18
19
-
####Request
19
+
### Request
20
20
21
21
An instance of [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) is accessible in [hooks](/docs/hooks) and [server routes](/docs/routing#server) as `event.request`. It contains useful methods like `request.json()` and `request.formData()` for getting data that was posted to an endpoint.
22
22
23
-
####Response
23
+
### Response
24
24
25
25
An instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) is returned from `await fetch(...)` and handlers in `+server.js` files. Fundamentally, a SvelteKit app is a machine for turning a `Request` into a `Response`.
26
26
27
-
####Headers
27
+
### Headers
28
28
29
29
The [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) interface allows you to read incoming `request.headers` and set outgoing `response.headers`:
30
30
@@ -45,7 +45,7 @@ export function GET(event) {
45
45
}
46
46
```
47
47
48
-
###FormData
48
+
## FormData
49
49
50
50
When dealing with HTML native form submissions you'll be working with [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) objects.
51
51
@@ -68,15 +68,15 @@ export async function POST(event) {
68
68
}
69
69
```
70
70
71
-
### Stream APIs
71
+
## Stream APIs
72
72
73
73
Most of the time, your endpoints will return complete data, as in the `userAgent` example above. Sometimes, you may need to return a response that's too large to fit in memory in one go, or is delivered in chunks, and for this the platform provides [streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) — [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream), [WritableStream](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream) and [TransformStream](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream).
74
74
75
-
### URL APIs
75
+
## URL APIs
76
76
77
77
URLs are represented by the [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) interface, which includes useful properties like `origin` and `pathname` (and, in the browser, `hash`). This interface shows up in various places — `event.url` in [hooks](/docs/hooks) and [server routes](/docs/routing#server), [`$page.url`](/docs/modules#$app-stores) in [pages](/docs/routing#page), `from` and `to` in [`beforeNavigate` and `afterNavigate`](/docs/modules#$app-navigation) and so on.
78
78
79
-
#### URLSearchParams
79
+
### URLSearchParams
80
80
81
81
Wherever you encounter a URL, you can access query parameters via `url.searchParams`, which is an instance of [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams):
82
82
@@ -93,7 +93,7 @@ export {};
93
93
constfoo=url.searchParams.get('foo');
94
94
```
95
95
96
-
### Web Crypto
96
+
## Web Crypto
97
97
98
98
The [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) is made available via the `crypto` global. It's used internally for [Content Security Policy](/docs/configuration#csp) headers, but you can also use it for things like generating UUIDs:
Copy file name to clipboardExpand all lines: documentation/docs/20-core-concepts/10-routing.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,9 @@ At the heart of SvelteKit is a _filesystem-based router_. The routes of your app
12
12
13
13
Each route directory contains one or more _route files_, which can be identified by their `+` prefix.
14
14
15
-
###+page
15
+
## +page
16
16
17
-
####+page.svelte
17
+
### +page.svelte
18
18
19
19
A `+page.svelte` component defines a page of your app. By default, pages are rendered both on the server ([SSR](/docs/glossary#ssr)) for the initial request and in the browser ([CSR](/docs/glossary#csr-and-spa)) for subsequent navigation.
20
20
@@ -44,7 +44,7 @@ A `+page.svelte` component defines a page of your app. By default, pages are ren
44
44
45
45
> Note that SvelteKit uses `<a>` elements to navigate between routes, rather than a framework-specific `<Link>` component.
46
46
47
-
####+page.js
47
+
### +page.js
48
48
49
49
Often, a page will need to load some data before it can be rendered. For this, we add a `+page.js` (or `+page.ts`, if you're TypeScript-inclined) module that exports a `load` function:
50
50
@@ -75,7 +75,7 @@ As well as `load`, `+page.js` can export values that configure the page's behavi
75
75
76
76
You can find more information about these in [page options](/docs/page-options).
77
77
78
-
####+page.server.js
78
+
### +page.server.js
79
79
80
80
If your `load` function can only run on the server — for example, if it needs to fetch data from a database or you need to access private [environment variables](/docs/modules#$env-static-private) like API keys — then you can rename `+page.js` to `+page.server.js` and change the `PageLoad` type to `PageServerLoad`.
81
81
@@ -114,7 +114,7 @@ Like `+page.js`, `+page.server.js` can export [page options](/docs/page-options)
114
114
115
115
A `+page.server.js` file can also export _actions_. If `load` lets you read data from the server, `actions` let you write data _to_ the server using the `<form>` element. To learn how to use them, see the [form actions](/docs/form-actions) section.
116
116
117
-
###+error
117
+
## +error
118
118
119
119
If an error occurs during `load`, SvelteKit will render a default error page. You can customise this error page on a per-route basis by adding an `+error.svelte` file:
120
120
@@ -133,13 +133,13 @@ SvelteKit will 'walk up the tree' looking for the closest error boundary — if
133
133
134
134
You can read more about error handling [here](/docs/errors).
135
135
136
-
###+layout
136
+
## +layout
137
137
138
138
So far, we've treated pages as entirely standalone components — upon navigation, the existing `+page.svelte` component will be destroyed, and a new one will take its place.
139
139
140
140
But in many apps, there are elements that should be visible on _every_ page, such as top-level navigation or a footer. Instead of repeating them in every `+page.svelte`, we can put them in _layouts_.
141
141
142
-
####+layout.svelte
142
+
### +layout.svelte
143
143
144
144
To create a layout that applies to every page, make a file called `src/routes/+layout.svelte`. The default layout (the one that SvelteKit uses if you don't bring your own) looks like this...
145
145
@@ -203,7 +203,7 @@ We can create a layout that only applies to pages below `/settings` (while inher
203
203
204
204
By default, each layout inherits the next layout above it. Sometimes that isn't what you want - in this case, [advanced layouts](/docs/advanced-routing#advanced-layouts) can help you.
205
205
206
-
####+layout.js
206
+
### +layout.js
207
207
208
208
Just like `+page.svelte` loading data from `+page.js`, your `+layout.svelte` component can get data from a [`load`](/docs/load) function in `+layout.js`.
209
209
@@ -236,13 +236,13 @@ Data returned from a layout's `load` function is also available to all its child
236
236
237
237
> Often, layout data is unchanged when navigating between pages. SvelteKit will intelligently re-run [`load`](/docs/load) functions when necessary.
238
238
239
-
####+layout.server.js
239
+
### +layout.server.js
240
240
241
241
To run your layout's `load` function on the server, move it to `+layout.server.js`, and change the `LayoutLoad` type to `LayoutServerLoad`.
242
242
243
243
Like `+layout.js`, `+layout.server.js` can export [page options](/docs/page-options) — `prerender`, `ssr` and `csr`.
244
244
245
-
###+server
245
+
## +server
246
246
247
247
As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file (or `+server.ts`) exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT` and `DELETE` that take a `RequestEvent` argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
248
248
@@ -275,7 +275,7 @@ You can use the [`error`](/docs/modules#sveltejs-kit-error), [`redirect`](/docs/
275
275
276
276
If an error is thrown (either `throwerror(...)` or an unexpected error), the response will be a JSON representation of the error or a fallback error page — which can be customised via `src/error.html` — depending on the `Accept` header. The [`+error.svelte`](#error) component will _not_ be rendered in this case. You can read more about error handling [here](/docs/errors).
277
277
278
-
#### Receiving data
278
+
### Receiving data
279
279
280
280
By exporting `POST`/`PUT`/`PATCH`/`DELETE` handlers, `+server.js` files can be used to create a complete API:
> In general, [form actions](/docs/form-actions) are a better way to submit data from the browser to the server.
321
321
322
-
#### Content negotiation
322
+
### Content negotiation
323
323
324
324
`+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:
325
325
326
326
- `PUT`/`PATCH`/`DELETE` requests are always handled by `+server.js` since they do not apply to pages
327
327
- `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`
328
328
329
-
### $types
329
+
## $types
330
330
331
331
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.
332
332
@@ -342,7 +342,7 @@ For example, annotating `export let data` with `PageData` (or `LayoutData`, for
342
342
343
343
In turn, annotating the `load` function with `PageLoad`, `PageServerLoad`, `LayoutLoad` or `LayoutServerLoad` (for `+page.js`, `+page.server.js`, `+layout.js` and `+layout.server.js` respectively) ensures that `params` and the return value are correctly typed.
344
344
345
-
### Other files
345
+
## Other files
346
346
347
347
Any other files inside a route directory are ignored by SvelteKit. This means you can colocate components and utility modules with the routes that need them.
0 commit comments