Skip to content

Commit 41b11de

Browse files
docs: update fs route (#3125)
1 parent 118242e commit 41b11de

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

docs/canary/concepts/file-routing.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,31 @@ description: |
33
Routes are the basic building block of Fresh applications. They are used to define the behaviour the application when a given path is requested.
44
---
55

6-
Fresh ships with the `fsRoutes` helper which automatically adds routes based on
7-
the structure in the `routes/` folder in your project. When you add a new file
8-
there, it will register a new route automatically.
6+
Use the `.fsRoutes()` helper on the [`App`](/docs/canary/concepts/app) instance
7+
to specify where file based routes should be inserted. It adds routes based on
8+
the structure in the `routes/` folder in your project (or any other folder you
9+
have set in `dev.ts`). When you add a new file there, it will register a new
10+
route automatically.
11+
12+
```ts main.ts
13+
import { Builder } from "fresh/dev";
14+
15+
// Optionally set a custom route dir (will be `<root>/routes` by default)
16+
const builder = new Builder({ routeDir: "path/to/routes" });
17+
18+
if (Deno.args.includes("build")) {
19+
await builder.build();
20+
} else {
21+
await builder.listen(() => import("./main.ts"));
22+
}
23+
```
924

10-
```ts
11-
import { App, fsRoutes, staticFiles } from "fresh";
25+
```ts main.ts
26+
import { App, staticFiles } from "fresh";
1227

1328
const app = new App({ root: import.meta.url })
14-
.use(staticFiles());
15-
16-
await fsRoutes(app, {
17-
loadIsland: (path) => import(`./islands/${path}`),
18-
loadRoute: (path) => import(`./routes/${path}`),
19-
});
29+
.use(staticFiles())
30+
.fsRoutes(); // This inserts all file based routes here
2031
```
2132

2233
> [info]: The `staticFiles()` middleware is required when using file based
@@ -26,6 +37,9 @@ await fsRoutes(app, {
2637
Example project structure:
2738

2839
```sh Project structure
40+
├── deno.json
41+
├── main.ts
42+
├── dev.ts
2943
└── routes
3044
   ├── (marketing) # Route group, used to group related routes
3145
   │ ├── _layout.tsx # Apply layout to all routes in this directory

src/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ export class App<State> {
290290
return this;
291291
}
292292

293+
/**
294+
* Insert file routes collected in {@linkcode Builder} at this point.
295+
* @param pattern Append file routes at this pattern instead of the root
296+
* @returns
297+
*/
293298
fsRoutes(pattern = "*"): this {
294299
this.#commands.push({
295300
type: CommandType.FsRoute,

0 commit comments

Comments
 (0)