From 41b11de4700f941263a533e7307b87cf4fa394cc Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 24 Jul 2025 15:34:51 +0200 Subject: [PATCH] docs: update fs route (#3125) --- docs/canary/concepts/file-routing.md | 36 +++++++++++++++++++--------- src/app.ts | 5 ++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/docs/canary/concepts/file-routing.md b/docs/canary/concepts/file-routing.md index 9a7f4d78..e08267ff 100644 --- a/docs/canary/concepts/file-routing.md +++ b/docs/canary/concepts/file-routing.md @@ -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. --- -Fresh ships with the `fsRoutes` helper which automatically adds routes based on -the structure in the `routes/` folder in your project. When you add a new file -there, it will register a new route automatically. +Use the `.fsRoutes()` helper on the [`App`](/docs/canary/concepts/app) instance +to specify where file based routes should be inserted. It adds routes based on +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 -import { App, fsRoutes, staticFiles } from "fresh"; +```ts main.ts +import { Builder } from "fresh/dev"; + +// Optionally set a custom route dir (will be `/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 }) - .use(staticFiles()); - -await fsRoutes(app, { - loadIsland: (path) => import(`./islands/${path}`), - loadRoute: (path) => import(`./routes/${path}`), -}); + .use(staticFiles()) + .fsRoutes(); // This inserts all file based routes here ``` > [info]: The `staticFiles()` middleware is required when using file based @@ -26,6 +37,9 @@ await fsRoutes(app, { Example project structure: ```sh Project structure +├── deno.json +├── main.ts +├── dev.ts └── routes    ├── (marketing) # Route group, used to group related routes    │ ├── _layout.tsx # Apply layout to all routes in this directory diff --git a/src/app.ts b/src/app.ts index 93dda2ab..803ee404 100644 --- a/src/app.ts +++ b/src/app.ts @@ -290,6 +290,11 @@ export class App { 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 { this.#commands.push({ type: CommandType.FsRoute,