-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'toakleaf-update-router-docs'
- Loading branch information
Showing
9 changed files
with
468 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,38 @@ | ||
# Router API | ||
|
||
(Coming soon) | ||
Inspired by the [Bun.App](https://bun.sh/blog/bun-bundler#sneak-peek-bun-app) API, the core primitive in Vinxi is the **router**, which is simply a brief specification defining how a group of URLs should be handled. | ||
|
||
Vinxi supports many common router types: | ||
- ['static'](./router/static) - for serving uncompiled, static assets | ||
- ['http'](./router/http) - for creating traditional web servers | ||
- ['spa'](./router/spa) - for building and serving single page application assets | ||
- ['client'](./router/client) - for building and serving of SSR application assets | ||
- [custom](./router/custom) - for adapting Vinxi to your use case | ||
|
||
Creating a new router is as simple as adding a specification object to the `routers` array in the `createApp` call: | ||
|
||
```ts | ||
import { createApp } from 'vinxi'; | ||
|
||
export default createApp({ | ||
routers: [ | ||
// A static router serving files from the `public` directory | ||
{ | ||
name: 'public', | ||
type: 'static', | ||
dir: './public', | ||
base: '/', | ||
}, | ||
// A http router for an api | ||
{ | ||
name: 'api', | ||
type: 'http', | ||
handler: './app/api.ts', | ||
base: '/api', | ||
plugins: () => [ | ||
// Vite plugins applying to exclusively to `http` router | ||
] | ||
} | ||
], | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,80 @@ | ||
# Client Router API | ||
|
||
(Coming soon) | ||
The `client` router is a wrapper for Vite’s build processes and development server, allowing for easy integration with an `http` router for server-side rendering applications. | ||
|
||
## Configuration Options | ||
|
||
### type | ||
|
||
- Value: `'client'` | ||
|
||
### name | ||
|
||
- Type: `string` | ||
- Required: `true` | ||
|
||
A unique identifier for the router. | ||
|
||
### handler | ||
|
||
- Type: `string` | ||
- Required: `true` | ||
|
||
The entry point file for the client application. | ||
|
||
### base | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
- Default value: `'/'` | ||
|
||
The base URL path under which the built assets for client application will be served. | ||
|
||
::: tip | ||
As client router is mostly built assets, its good to use `base` for namspacing these assets in order to avoid conflicts, eg. `"/_build"` | ||
::: | ||
|
||
### plugins | ||
|
||
- Type: `() => Plugin[]` | ||
- Required: `false` | ||
|
||
A function returning an array of Vite plugins to use during the build process. | ||
|
||
[Learn more about configuring Vite plugins](../../guide/vite-plugins.md) | ||
|
||
### routes | ||
|
||
- Type: `(router: RouterSchemaInput, app: AppOptions) => CompiledRouter` | ||
- Required: `false` | ||
|
||
A function defining the routing logic or structure. | ||
|
||
[Learn more about Vinxi's file system routing.](../../guide/file-system-routing.md) | ||
|
||
### outDir | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
The output directory for build artifacts. | ||
|
||
### root | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
The root directory for resolving paths. Defaults to the application's root directory. | ||
|
||
|
||
## Example Configuration | ||
|
||
```ts | ||
{ | ||
name: "client", | ||
type: "client", | ||
handler: "./app/client.tsx", | ||
base: "/_build", | ||
plugins: () => [ reactRefresh() ], | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Custom Router API | ||
|
||
The Custom Router allows for defining routers with custom behavior or configurations that don’t fit into the predefined router types. It’s useful for advanced or specialized use cases. | ||
|
||
## Configuration Options | ||
|
||
### type | ||
|
||
- Type: `{ resolveConfig: (router: Router, app: App) => Router }` | ||
- Required: `true` | ||
|
||
Unlike all other routers, rather than a string literal, custom routers are defined by passing defining the `type` as an object with a `resolveConfig` function. | ||
|
||
### name | ||
|
||
- Type: `string` | ||
- Required: `true` | ||
|
||
A unique identifier for the router. | ||
|
||
### handler | ||
|
||
- Type: `string` | ||
- Required: `true` | ||
|
||
The entry point file for Nitro server handling HTTP requests. | ||
|
||
### target | ||
|
||
- Type: `'server'` | ||
- Required: `true` | ||
|
||
Unlike all other routers where `target` is implied by the `type`, for custom routers the string literal `'server'` must be passed explicitly, as is currently the only option. | ||
|
||
### base | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
- Default value: `'/'` | ||
|
||
The base URL path under which the client application will be served. | ||
|
||
### plugins | ||
|
||
- Type: `() => Plugin[]` | ||
- Required: `false` | ||
|
||
A function returning an array of Vite plugins to use during the build process. | ||
|
||
[Learn more about configuring Vite plugins](../../guide/vite-plugins.md) | ||
|
||
### routes | ||
|
||
- Type: `(router: RouterSchemaInput, app: AppOptions) => CompiledRouter` | ||
- Required: `false` | ||
|
||
A function defining the routing logic or structure. | ||
|
||
[Learn more about Vinxi's file system routing.](../../guide/file-system-routing.md) | ||
|
||
### outDir | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
The output directory for build artifacts. | ||
|
||
### root | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
The root directory for resolving paths. Defaults to the application's root directory. | ||
|
||
|
||
## Example Configuration | ||
|
||
```ts | ||
{ | ||
name: "customRouter", | ||
type: { | ||
resolveConfig: (router, app) => { | ||
// Custom configuration logic | ||
}, | ||
}, | ||
handler: "./app/customHandler.ts", | ||
target: "server", | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,112 @@ | ||
# HTTP Router API | ||
|
||
(Coming soon) | ||
The `http` router is a wrapper of a Nitro web server, with all the flexibility that entails. Great for server side rendering, websockets, custom API endpoints, etc... | ||
|
||
## Configuration Options | ||
|
||
### type | ||
|
||
- Value: `'http'` | ||
|
||
### name | ||
|
||
- Type: `string` | ||
- Required: `true` | ||
|
||
A unique identifier for the router. | ||
|
||
:::tip | ||
The name of the router that the code is currently executing can be imported from `vinxi/manifest`. | ||
```ts | ||
import { routerName } from "vinxi/manifest"; | ||
|
||
export default eventHandler(() => { | ||
const serverManifest = getManifest(routerName); | ||
const clientManifest = getManifest("react-client"); | ||
}); | ||
``` | ||
::: | ||
|
||
### handler | ||
|
||
- Type: `string` | ||
- Required: `true` | ||
|
||
The entry point file for Nitro server handling HTTP requests. | ||
|
||
### base | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
- Default value: `'/'` | ||
|
||
The base URL path under which the client application will be served. | ||
|
||
### plugins | ||
|
||
- Type: `() => Plugin[]` | ||
- Required: `false` | ||
|
||
A function returning an array of Vite plugins to use during the build process. | ||
|
||
[Learn more about configuring Vite plugins](../../guide/vite-plugins.md) | ||
|
||
### routes | ||
|
||
- Type: `(router: RouterSchemaInput, app: AppOptions) => CompiledRouter` | ||
- Required: `false` | ||
|
||
A function defining the routing logic or structure. | ||
|
||
[Learn more about Vinxi's file system routing.](../../guide/file-system-routing.md) | ||
|
||
## middleware | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
Path to server middleware to apply to the router. | ||
|
||
### worker | ||
|
||
- Type: `boolean` | ||
- Required: `false` | ||
- Default value: `false` | ||
|
||
Configures the router to run its request-handling in a separate worker thread. | ||
|
||
### build | ||
|
||
- Type: `boolean` | ||
- Required: `false` | ||
- Default value: `true` | ||
|
||
Include the router in the build process. | ||
|
||
### outDir | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
The output directory for build artifacts. | ||
|
||
### root | ||
|
||
- Type: `string` | ||
- Required: `false` | ||
|
||
The root directory for resolving paths. Defaults to the application's root directory. | ||
|
||
|
||
## Example Configuration | ||
|
||
```ts | ||
{ | ||
name: "server", | ||
type: "http", | ||
handler: "./app/apiHandler.ts", | ||
base: "/api", | ||
worker: true, | ||
plugins: () => [ reactRefresh() ], | ||
} | ||
``` |
Oops, something went wrong.