docs: update fs route (#3125)
This commit is contained in:
@@ -3,20 +3,31 @@ description: |
|
|||||||
Routes are the basic building block of Fresh applications. They are used to define the behaviour the application when a given path is requested.
|
Routes are the basic building block of Fresh applications. They are used to define the behaviour the application when a given path is requested.
|
||||||
---
|
---
|
||||||
|
|
||||||
Fresh ships with the `fsRoutes` helper which automatically adds routes based on
|
Use the `.fsRoutes()` helper on the [`App`](/docs/canary/concepts/app) instance
|
||||||
the structure in the `routes/` folder in your project. When you add a new file
|
to specify where file based routes should be inserted. It adds routes based on
|
||||||
there, it will register a new route automatically.
|
the structure in the `routes/` folder in your project (or any other folder you
|
||||||
|
have set in `dev.ts`). When you add a new file there, it will register a new
|
||||||
|
route automatically.
|
||||||
|
|
||||||
```ts
|
```ts main.ts
|
||||||
import { App, fsRoutes, staticFiles } from "fresh";
|
import { Builder } from "fresh/dev";
|
||||||
|
|
||||||
|
// Optionally set a custom route dir (will be `<root>/routes` by default)
|
||||||
|
const builder = new Builder({ routeDir: "path/to/routes" });
|
||||||
|
|
||||||
|
if (Deno.args.includes("build")) {
|
||||||
|
await builder.build();
|
||||||
|
} else {
|
||||||
|
await builder.listen(() => import("./main.ts"));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts main.ts
|
||||||
|
import { App, staticFiles } from "fresh";
|
||||||
|
|
||||||
const app = new App({ root: import.meta.url })
|
const app = new App({ root: import.meta.url })
|
||||||
.use(staticFiles());
|
.use(staticFiles())
|
||||||
|
.fsRoutes(); // This inserts all file based routes here
|
||||||
await fsRoutes(app, {
|
|
||||||
loadIsland: (path) => import(`./islands/${path}`),
|
|
||||||
loadRoute: (path) => import(`./routes/${path}`),
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> [info]: The `staticFiles()` middleware is required when using file based
|
> [info]: The `staticFiles()` middleware is required when using file based
|
||||||
@@ -26,6 +37,9 @@ await fsRoutes(app, {
|
|||||||
Example project structure:
|
Example project structure:
|
||||||
|
|
||||||
```sh Project structure
|
```sh Project structure
|
||||||
|
├── deno.json
|
||||||
|
├── main.ts
|
||||||
|
├── dev.ts
|
||||||
└── routes
|
└── routes
|
||||||
├── (marketing) # Route group, used to group related routes
|
├── (marketing) # Route group, used to group related routes
|
||||||
│ ├── _layout.tsx # Apply layout to all routes in this directory
|
│ ├── _layout.tsx # Apply layout to all routes in this directory
|
||||||
|
|||||||
@@ -290,6 +290,11 @@ export class App<State> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert file routes collected in {@linkcode Builder} at this point.
|
||||||
|
* @param pattern Append file routes at this pattern instead of the root
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
fsRoutes(pattern = "*"): this {
|
fsRoutes(pattern = "*"): this {
|
||||||
this.#commands.push({
|
this.#commands.push({
|
||||||
type: CommandType.FsRoute,
|
type: CommandType.FsRoute,
|
||||||
|
|||||||
Reference in New Issue
Block a user