Skip to content

Commit

Permalink
Merge branch 'main' into feat/openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 21, 2023
2 parents 4071a14 + 1d218eb commit 959f2b1
Show file tree
Hide file tree
Showing 50 changed files with 1,368 additions and 1,022 deletions.
1 change: 1 addition & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineBuildConfig({
name: "nitro",
entries: [
"src/index",
"src/config",
"src/cli/cli",
{ input: "src/runtime/", outDir: "dist/runtime", format: "esm" },
],
Expand Down
1 change: 1 addition & 0 deletions config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/config";
19 changes: 19 additions & 0 deletions docs/content/1.guide/0.getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ You can try it locally with:
npm run preview
```


## Edge Release Channel

Nitro offers an edge release channel that automatically releases for every commit to `main` branch.

You can opt-in to the edge release channel by updating your `package.json`:

```diff
{
"devDependencies": {
-- "nitropack": "^2.0.0"
++ "nitropack": "npm:nitropack-edge@latest"
}
}
```

Remove an lockfile (`package-lock.json`, `yarn.lock`, or `pnpm-lock.yaml`) and reinstall the dependencies.


2 changes: 1 addition & 1 deletion docs/content/1.guide/1.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you are using [Nuxt](https://nuxt.com), use the `nitro` option in your Nuxt c

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
// Nitro options
Expand Down
3 changes: 2 additions & 1 deletion docs/content/1.guide/2.auto-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Nitro is using [unjs/unimport](https://github.com/unjs/unimport) to auto import
- `defineCachedFunction(fn, options)`{lang=ts} / `cachedFunction(fn, options)`{lang=ts}
- `defineCachedEventHandler(handler, options)`{lang=ts} / `cachedEventHandler(handler, options)`{lang=ts}
- `defineRenderHandler(handler)`{lang=ts}
- `useRuntimeConfig()`{lang=ts}
- `useRuntimeConfig(event?)`{lang=ts}
- `useAppConfig(event?)`{lang=ts}
- `useStorage(base?)`{lang=ts}
- `useNitroApp()`{lang=ts}
- `defineNitroPlugin(plugin)`{lang=ts}
Expand Down
61 changes: 57 additions & 4 deletions docs/content/1.guide/3.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ title: Routing
icon: ri:direction-line
---

# Filesystem Routing
# Routing

Nitro support filesystem routing as well as defining route rules for maximum flexibility and performance.

## Filesystem Routing

Nitro supports file-based routing for your API routes.

Expand All @@ -13,8 +17,6 @@ Handler files inside `api/` and `routes/` directory will be automatically mapped
Due to some providers like Vercel using top-level `api/` directory as a feature, Nitro also supports `routes/api/` to create API routes.
::

## Usage

```md
api/
test.ts <-- /api/test
Expand All @@ -23,8 +25,9 @@ routes/
nitro.config.ts
```

::alert
If you are using [Nuxt](https://nuxt.com), move the `api/` and `routes/` inside the `server/` directory.

::

### Simple route

Expand Down Expand Up @@ -96,3 +99,53 @@ Check out [h3 JSDocs](https://www.jsdocs.io/package/h3#package-index-functions)
export default eventHandler(event => `Default page`)
```

## Route Rules

Nitro allows you to add logic at the top-level of your configuration, useful for redirecting, proxying, caching and adding headers to routes.

It is a map from route pattern (following [unjs/radix3](https://github.com/unjs/radix3#route-matcher)) to route options.

When `cache` option is set, handlers matching pattern will be automatically wrapped with `defineCachedEventHandler`.

See the [Cache API](/guide/cache) for all available cache options.

::alert
`swr: true|number` is shortcut for `cache: { swr: true, maxAge: number }`
::

**Example:**

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
routeRules: {
'/blog/**': { swr: true },
'/blog/**': { swr: 600 },
'/blog/**': { static: true },
'/blog/**': { cache: { /* cache options*/ } },
'/assets/**': { headers: { 'cache-control': 's-maxage=0' } },
'/api/v1/**': { cors: true, headers: { 'access-control-allow-methods': 'GET' } },
'/old-page': { redirect: '/new-page' },
'/proxy/example': { proxy: 'https://example.com' },
'/proxy/**': { proxy: '/api/**' },
}
})
```
```ts [nuxt.config.ts]
export default defineNuxtConfig({
routeRules: {
'/blog/**': { swr: true },
'/blog/**': { swr: 600 },
'/blog/**': { static: true },
'/blog/**': { cache: { /* cache options*/ } },
'/assets/**': { headers: { 'cache-control': 's-maxage=0' } },
'/api/v1/**': { cors: true, headers: { 'access-control-allow-methods': 'GET' } },
'/old-page': { redirect: '/new-page' },
'/proxy/example': { proxy: 'https://example.com' },
'/proxy/**': { proxy: '/api/**' },
}
})
```
::
6 changes: 3 additions & 3 deletions docs/content/1.guide/4.storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You can mount storage drivers using the `storage` option:

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
storage: {
Expand Down Expand Up @@ -93,7 +93,7 @@ export default defineNitroConfig({
driver: 'redis',
/* redis connector options */
}
}
},
// Development
devStorage: {
db: {
Expand All @@ -112,7 +112,7 @@ export default defineNuxtConfig({
driver: 'redis',
/* redis connector options */
}
}
},
// Development
devStorage: {
db: {
Expand Down
6 changes: 3 additions & 3 deletions docs/content/1.guide/5.cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To overwrite the production storage, set the `cache` mountpoint using the `stora

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from "nitropack";
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
storage: {
Expand Down Expand Up @@ -123,7 +123,7 @@ Cache all the blog routes for 1 hour with `stale-while-revalidate` behavior:

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from "nitropack";
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
routeRules: {
Expand Down Expand Up @@ -156,7 +156,7 @@ If we want to use a custom storage mountpoint, we can use the `base` option. Let

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from "nitropack";
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
storage: {
Expand Down
10 changes: 6 additions & 4 deletions docs/content/1.guide/6.assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ In order to add assets from a custom directory, path will need to be defined in

::code-group
```js [nitro.config.ts]
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
serverAssets: [{
Expand Down Expand Up @@ -101,12 +101,12 @@ export default defineEventHandler(async () => {

::code-group
```js [nitro.config.ts]
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
serverAssets: [{
baseName: 'templates',
dir: './server/templates'
dir: './templates' // Relative to `srcDir` (`server/` for nuxt)
}]
})
```
Expand All @@ -115,7 +115,7 @@ export default defineNuxtConfig({
nitro: {
serverAssets: [{
baseName: 'templates',
dir: './server/templates'
dir: './templates' // Relative to `srcDir` (`server/` for nuxt)
}]
}
})
Expand All @@ -126,5 +126,7 @@ export default defineNuxtConfig({
// routes/success.ts
export default defineEventHandler(async (event) => {
return await useStorage().getItem(`assets/templates/success.html`)
// or
return await useStorage('assets:templates').getItem(`success.html`)
})
```
2 changes: 1 addition & 1 deletion docs/content/1.guide/7.plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ If you have plugins in another directory, you can use the `plugins` option:

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from 'nitropack'
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
plugins: ['my-plugins/hello.ts']
Expand Down
2 changes: 1 addition & 1 deletion docs/content/2.deploy/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ NITRO_PRESET=aws-lambda nitro build
**Example:** Using [nitro.config.ts](/config/)

```ts
import { defineNitroConfig } from "nitropack";
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
preset: 'node-server'
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ In order to customize Nitro's behavior, we create a file named `nitro.config.ts`

::code-group
```ts [nitro.config.ts]
import { defineNitroConfig } from "nitropack";
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
// options
Expand Down Expand Up @@ -251,7 +251,7 @@ Path to a custom runtime error handler. Replacing nitro's built-in error page.
**Example:**

```js [nitro.config]
import { defineNitroConfig } from "nitropack";
import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
errorHandler: "~/error",
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"@nuxt-themes/docus": "^1.10.1",
"@nuxthq/studio": "^0.10.1",
"@nuxtjs/plausible": "^0.2.0",
"nuxt": "3.3.3"
"nuxt": "3.4.2"
}
}
Loading

0 comments on commit 959f2b1

Please sign in to comment.