8000 docs: improvements · nitrojs/nitro@f78619f · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit f78619f

Browse files
authored
docs: improvements
1 parent 1d2f57e commit f78619f

File tree

4 files changed

+116
-23
lines changed

4 files changed

+116
-23
lines changed

docs/content/1.guide/1.introduction/2.auto-imports.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ Nitro is using [unjs/unimport](https://github.com/unjs/unimport) to auto import
55
## Available Auto Imports
66

77
- `defineCachedFunction(fn, options)`{lang=ts} / `cachedFunction(fn, options)`{lang=ts}
8-
- `defineCachedEventHandler(handler, options)`{lang=ts}
9-
- `cachedEventHandler(handler, options)`{lang=ts}
8+
- `defineCachedEventHandler(handler, options)`{lang=ts} / `cachedEventHandler(handler, options)`{lang=ts}
9+
- `defineRenderHandler(handler)`{lang=ts}
1010
- `useRuntimeConfig()`{lang=ts}
1111
- `useStorage(base?)`{lang=ts}
1212
- `useNitroApp()`{lang=ts}
1313
- `defineNitroPlugin(plugin)`{lang=ts}
1414
- `nitroPlugin(plugin)`{lang=ts}
15-
- `defineRenderHandler`
16-
"getRouteRules",
15+
- `getRouteRules(event)`{lang=ts}
1716

1817
Check [the source code](https://github.com/unjs/nitro/blob/main/src/imports.ts) for list of available auto imports.
1918

docs/content/1.guide/1.introduction/3.routing.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@ title: Routing
66

77
Nitro supports file-based routing for your API routes.
88

9-
Handler files inside `routes/` directory will be automatically mapped to [unjs/h3](https://github.com/unjs/h3) routes.
9+
Handler files inside `api/` and `routes/` directory will be automatically mapped to [unjs/h3](https://github.com/unjs/h3) routes.
10+
11+
::alert{type=primary}
12+
Due to some providers like Vercel using top-level `api/` directory as a feature, Nitro also supports `routes/api/` to create API routes.
13+
::
1014

1115
## Usage
1216

1317
```md
18+
api/
19+
test.ts <-- /api/test
1420
routes/
15-
api/
16-
test.ts <-- /api/test
1721
hello.ts <-- /hello
22+
nitro.config.ts
1823
```
1924

25+
If you are using [Nuxt](https://nuxt.com), move the `api/` and `routes/` inside the `server/` directory.
26+
27+
2028
### Simple route
2129

2230
```ts
23-
// routes/test.ts
24-
export default eventHandler(() => 'Hello World!')
31+
// api/hello.ts
32+
export default eventHandler(() => {
33+
return { hello: 'world' }
34+
})
2535
```
2636

2737
### Route with params
@@ -31,6 +41,25 @@ export default eventHandler(() => 'Hello World!')
3141
export default eventHandler(event => `Hello ${event.context.params.name}!`)
3242
```
3343

44+
::code-group
45+
```md [/hello/nitro]
46+
Hello nitro!
47+
```
48+
::
49+
50+
To include the `/`, use `[...name].ts`:
51+
52+
```js
53+
// routes/hello/[...name].ts
54+
export default eventHandler(event => `Hello ${event.context.params.name}!`)
55+
```
56+
57+
::code-group
58+
```md [/hello/nitro/is/hot]
59+
Hello nitro/is/hot!
60+
```
61+
::
62+
3463
### Specific request method
3564

3665
API route with a specific HTTP request method (get, post, put, delete, options and so on).

docs/content/1.guide/1.introduction/6.assets.md

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,63 @@
11
# Assets Handling
22

3-
Nitro handles assets via the public directory.
3+
Nitro handles assets via the `public/` directory.
44

55
## Public Assets
66

77
All assets in `public/` directory will be automatically served.
88

9+
```md
10+
public/
11+
image.png <-- /image.png
12+
video.mp4 <-- /video.pm4
13+
robots.txt <-- /robots.txt
14+
package.json
15+
nitro.config.ts
16+
```
17+
18+
When building with Nitro, it will copy the `public/` directory to `.output/public/` and create a manifest with metadata:
19+
20+
```json
21+
{
22+
"/image.png": {
23+
"type": "image/png",
24+
"etag": "\"4a0c-6utWq0Kbk5OqDmksYCa9XV8irnM\"",
25+
"mtime": "2023-03-04T21:39:45.086Z",
26+
"size": 18956
27+
},
28+
"/robots.txt": {
29+
"type": "text/plain; charset=utf-8",
30+
"etag": "\"8-hMqyDrA8fJ0R904zgEPs3L55Jls\"",
31+
"mtime": "2023-03-04T21:39:45.086Z",
32+
"size": 8
33+
},
34+
"/video.mp4": {
35+
"type": "video/mp4",
36+
"etag": "\"9b943-4UwfQXKUjPCesGPr6J5j7GzNYGU\"",
37+
"mtime": "2023-03-04T21:39:45.085Z",
38+
"size": 637251
39+
}
40+
}
41+
```
42+
43+
This allows Nitro to know the public assets without scanning the directory, giving high performance with caching headers.
44+
945
## Server Assets
1046

11-
All assets in `assets/` directory will be added automatically to the server bundle.
47+
All assets in `assets/` directory will be added to the server bundle.
1248

13-
They can be addressed by the key `assets:server:path:to:asset` using [storage layer](/guide/introduction/storage) and `useStorage`.
49+
They can be addressed by the `assets:server` mountpoing using [`useStorage('assets:server')`](/guide/introduction/storage)
1450

1551
::alert
16-
**Tip !**
17-
:br
18-
Assets keys can be written `assets/server/my_file_path` or `assets:server:my_file_path`.
52+
Keys can be written `assets/server/my_file_path` or `assets:server:my_file_path`.
1953
::
2054

2155
### Custom Server Assets
2256

2357
In order to add assets from a custom directory, path will need to be defined in the nitro config:
2458

25-
```js
59+
::code-group
60+
```js [nitro.config.ts]
2661
import { defineNitroConfig } from 'nitropack'
2762

2863
export default defineNitroConfig({
@@ -32,6 +67,17 @@ export default defineNitroConfig({
3267
}]
3368
})
3469
```
70+
```ts [nuxt.config.ts]
71+
export default defineNuxtConfig({
72+
nitro: {
73+
serverAssets: [{
74+
baseName: 'my_directory',
75+
dir: './server/my_directory'
76+
}]
77+
}
78+
})
79+
```
80+
::
3581

3682
They can be addressed by the key `assets/my_directory/`.
3783

@@ -48,8 +94,8 @@ export default defineEventHandler(async () => {
4894

4995
**Example:** Retrieving a html file from a custom `assets` directory:
5096

51-
```js
52-
// nitro.config.ts
97+
::code-group
98+
```js [nitro.config.ts]
5399
import { defineNitroConfig } from 'nitropack'
54100

55101
export default defineNitroConfig({
@@ -59,10 +105,21 @@ export default defineNitroConfig({
59105
}]
60106
})
61107
```
108+
```ts [nuxt.config.ts]
109+
export default defineNuxtConfig({
110+
nitro: {
111+
serverAssets: [{
112+
baseName: 'templates',
113+
dir: './server/templates'
114+
}]
115+
}
116+
})
117+
```
118+
::
62119

63120
```js
64-
export default defineEventHandler(async () => {
65-
const html = await useStorage().getItem(`assets/templates/success.html`)
66-
return html
121+
// routes/success.ts
122+
export default defineEventHandler(async (event) => {
123+
return await useStorage().getItem(`assets/templates/success.html`)
67124
})
68125
```
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# TypeScript Support
22

3-
Nitro supports TypeScript by default.
3+
Nitro supports TypeScript by default. If you are using the starter template, you have nothing to do :sparkles:.
44

5-
To add type hints within your project, you should add the following to your `tsconfig.json` file:
5+
To add type hints within your project, create a `tsconfig.json` file:
66

77
```json [tsconfig.json]
88
{
@@ -11,3 +11,11 @@ To add type hints within your project, you should add the following to your `tsc
1111
```
1212

1313
Run `npx nitropack prepare` to generate the types for global auto-imports. This can be useful in a CI environment or as a `postinstall` command in your `package.json`.
14+
15+
If you are using [Nuxt](https://nuxt.com), your `tsconfig.json` can stay:
16+
17+
```json [tsconfig.json]
18+
{
19+
"extends": "./.nuxt/tsconfig.json"
20+
}
21+
```

0 commit comments

Comments
 (0)
0