Skip to content

Commit 583106f

Browse files
committed
docs(static-file): clarify path extraction and wildcard pattern behavior 📝
- Add explanation for using ctx.pathname instead of wildcard parameters - Document FastRouter wildcard pattern limitation behavior - Update path resolution steps in documentation - Fix handler to return instead of throwing when routes directory not found
1 parent 3675be0 commit 583106f

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

docs/en/static-file/basic.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,23 @@ This serves files from the `public/` directory at the `/static` URL path:
3030
Deserve uses a custom static file serving implementation:
3131

3232
1. **Route Matching**: Creates routes with pattern `${urlPath}/**` to match all files
33-
2. **File Resolution**: Maps URL paths to file system paths using the `path` option
34-
3. **Wildcard Capture**: Extracts file path from route parameters (`params['_']`)
33+
2. **Path Extraction**: Uses `ctx.pathname` directly to get the full request path (FastRouter's `/**` pattern only captures the first segment, so we use pathname instead)
34+
3. **File Resolution**: Maps URL paths to file system paths using the `path` option
3535
4. **Priority**: Static routes are registered for all HTTP methods before dynamic routes
3636

37+
### Wildcard Pattern Behavior
38+
39+
When `urlPath` is `/`, Deserve creates a `/**` pattern. For path resolution, Deserve uses `ctx.pathname` instead of relying on wildcard parameter, because:
40+
41+
- FastRouter's `/**` pattern only captures the **first segment** of the request path instead of the full path (e.g., `"styles"` for `/styles/ui.css`)
42+
- To serve nested files correctly, Deserve extracts the full path from `ctx.pathname` and removes the leading `/` to get the relative file path
43+
44+
**Example:**
45+
- Request: `GET /styles/ui.css`
46+
- Pattern: `/**` matches from configurable path
47+
- File path: Extracted from `ctx.pathname``"styles/ui.css"`
48+
- Resolved: `static/styles/ui.css`
49+
3750
## Static File Options
3851

3952
The `static()` method accepts a `ServeOptions` object:

docs/id/static-file/basic.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,23 @@ Ini menyajikan file dari direktori `public/` di path URL `/static`:
3030
Deserve menggunakan implementasi custom untuk menyajikan file statis:
3131

3232
1. **Route Matching**: Membuat routes dengan pola `${urlPath}/**` untuk mencocokkan semua file
33-
2. **File Resolution**: Memetakan URL paths ke file system paths menggunakan opsi `path`
34-
3. **Wildcard Capture**: Mengekstrak path file dari route parameters (`params['_']`)
33+
2. **Path Extraction**: Menggunakan `ctx.pathname` langsung untuk mendapatkan full request path (pola `/**` dari FastRouter hanya menangkap segment pertama, jadi kita menggunakan pathname sebagai gantinya)
34+
3. **File Resolution**: Memetakan URL paths ke file system paths menggunakan opsi `path`
3535
4. **Priority**: Static routes diregistrasi untuk semua HTTP methods sebelum dynamic routes
3636

37+
### Perilaku Wildcard Pattern
38+
39+
Ketika `urlPath` adalah `/`, Deserve membuat pola `/**`. Untuk path resolution, Deserve menggunakan `ctx.pathname` daripada mengandalkan wildcard parameter, karena:
40+
41+
- Pola `/**` dari FastRouter hanya menangkap **segment pertama** dari request path alih-alih full path (misalnya, `"styles"` untuk `/styles/ui.css`)
42+
- Untuk menyajikan file nested dengan benar, Deserve mengekstrak full path dari `ctx.pathname` dan menghapus leading `/` untuk mendapatkan relative file path
43+
44+
**Contoh:**
45+
- Request: `GET /styles/ui.css`
46+
- Pattern: `/**` cocok dari path yang dikonfigurasi
47+
- File path: Diekstrak dari `ctx.pathname``"styles/ui.css"`
48+
- Resolved: `static/styles/ui.css`
49+
3750
## Opsi Static File
3851

3952
Method `static()` menerima objek `ServeOptions`:

src/Handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class Handler {
176176
}
177177
} catch (error) {
178178
if (error instanceof Deno.errors.NotFound) {
179-
throw new Error(`Routes directory not found: ${targetDir}`)
179+
return
180180
} else {
181181
throw error
182182
}

0 commit comments

Comments
 (0)