Skip to content

Commit ac8b0b9

Browse files
feat: add wildcard support on routes (#676)
* initial commit * adding missing tests * docs * Update bricks/dart_frog_prod_server/hooks/pre_gen.dart * Apply suggestions from code review Co-authored-by: Scarlett Eliza <[email protected]> * fix: docs format --------- Co-authored-by: Scarlett Eliza <[email protected]>
1 parent 675dc5e commit ac8b0b9

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

β€Žbricks/dart_frog_dev_server/__brick__/server.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žbricks/dart_frog_prod_server/__brick__/build/bin/server.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ždocs/docs/basics/routes.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,23 @@ Response onRequest(RequestContext context, String userId, String postId) {
416416
}
417417
```
418418

419+
## Wildcard Routes β™Ύ
420+
421+
Dart Frog supports wildcard routes. For example, if you create a file called `routes/posts/[...page].dart`, then it will be accessible at any path that starts with `/posts/`, with any number of levels, allowing it to called from `/posts/today`, `/posts/features/stared`, and so forth.
422+
423+
Routing parameters are forwarded to the `onRequest` method as seen below:
424+
425+
```dart
426+
import 'package:dart_frog/dart_frog.dart';
427+
428+
Response onRequest(RequestContext context, String page) {
429+
return Response(body: 'post page: $page');
430+
}
431+
```
432+
433+
```warning
434+
Wildcard routes **must** be unique leaf routes on their route node, meaning that they need to be a file, and they need to be the only route in their folder.
435+
419436
## Route Conflicts πŸ’₯
420437
421438
When defining routes, it's possible to encounter route conflicts.
@@ -425,11 +442,13 @@ A route conflict occurs when more than one route handler resolves to the same en
425442
For example, given the following file structure:
426443
427444
```
445+
428446
β”œβ”€β”€ routes
429447
β”‚Β Β  β”œβ”€β”€ api
430448
β”‚Β Β  β”‚Β Β  └── index.dart
431449
β”‚Β Β  └── api.dart
432-
```
450+
451+
````
433452
434453
Both `routes/api/index.dart` and `routes/api.dart` resolve the the `/api` endpoint.
435454
@@ -439,7 +458,7 @@ When running the development server via `dart_frog dev`, Dart Frog will report r
439458
[hotreload] - Application reloaded.
440459
441460
Route conflict detected. `routes/api.dart` and `routes/api/index.dart` both resolve to `/api`.
442-
```
461+
````
443462

444463
When generating a production build via `dart_frog build`, Dart Frog will report all detected route conflicts and fail the build if one or more route conflicts are detected.
445464

0 commit comments

Comments
Β (0)