Skip to content

Commit cbad465

Browse files
authored
docs(routes): add route conflicts and rogue routes section (#250)
1 parent 76a9563 commit cbad465

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ sidebar_position: 1
44

55
# Routes 🚏
66

7+
## Overview ✨
8+
79
In Dart Frog, a route consists of an `onRequest` function (called a route handler) exported from a `.dart` file in the `routes` directory. Each endpoint is associated with a routes file based on its file name. Files named, `index.dart` will correspond to a `/` endpoint.
810

911
For example, if you create `routes/hello.dart` that exports an `onRequest` method like below, it will be accessible at `/hello`.
@@ -16,7 +18,9 @@ Response onRequest(RequestContext context) {
1618
}
1719
```
1820

19-
All route handlers have access to a `RequestContext` which can be used to access the incoming request as well as dependencies provided to the request context (see middleware).
21+
## Request Context πŸ”—
22+
23+
All route handlers have access to a `RequestContext` which can be used to access the incoming request as well as dependencies provided to the request context ([see middleware](/docs/basics/middleware)).
2024

2125
```dart
2226
import 'package:dart_frog/dart_frog.dart';
@@ -30,6 +34,8 @@ Response onRequest(RequestContext context) {
3034
}
3135
```
3236

37+
## Custom Status Code πŸ†—
38+
3339
We can customize the status code of the response via the `statusCode` parameter on the `Response` object:
3440

3541
```dart
@@ -40,6 +46,8 @@ Response onRequest(RequestContext context) {
4046
}
4147
```
4248

49+
## Returning JSON `{}`
50+
4351
In addition, we can return JSON via the `Response.json` constructor:
4452

4553
```dart
@@ -108,3 +116,66 @@ Response onRequest(RequestContext context, String id) {
108116
return Response(body: 'post id: $id');
109117
}
110118
```
119+
120+
## Route Conflicts πŸ’₯
121+
122+
When defining routes, it's possible to encounter route conflicts.
123+
124+
A route conflict occurs when more than one route handler resolves to the same endpoint.
125+
126+
For example, given the following file structure:
127+
128+
```
129+
β”œβ”€β”€ routes
130+
β”‚Β Β  β”œβ”€β”€ api
131+
β”‚Β Β  β”‚Β Β  └── index.dart
132+
β”‚Β Β  └── api.dart
133+
```
134+
135+
Both `routes/api/index.dart` and `routes/api.dart` resolve the the `/api` endpoint.
136+
137+
When running the development server via `dart_frog dev`, Dart Frog will report route conflicts while the development server is running. You can resolve the conflicts and hot reload will allow you to continue development without having to restart the server.
138+
139+
```bash
140+
[hotreload] - Application reloaded.
141+
142+
Route conflict detected. `routes/api.dart` and `routes/api/index.dart` both resolve to `/api`.
143+
```
144+
145+
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.
146+
147+
## Rogue Routes πŸ₯·
148+
149+
Similar to route conflicts, it's also possible to run into rogue routes when working with Dart Frog.
150+
151+
A route is considered rogue when it is defined outside of an existing subdirectory with the same name.
152+
153+
For example:
154+
155+
```
156+
β”œβ”€β”€ routes
157+
β”‚ β”œβ”€β”€ api
158+
β”‚ β”‚ └── example.dart
159+
β”‚ β”œβ”€β”€ api.dart
160+
```
161+
162+
In the above scenario, `routes/api.dart` is rogue because it is defined outside of the existing `api` directory.
163+
164+
To correct this, `api.dart` should be renamed to `index.dart` and placed within the `api` directory like:
165+
166+
```
167+
β”œβ”€β”€ routes
168+
β”‚ β”œβ”€β”€ api
169+
β”‚ β”‚ β”œβ”€β”€ example.dart
170+
β”‚ β”‚ └── index.dart
171+
```
172+
173+
When running the development server via `dart_frog dev`, Dart Frog will report rogue routes while the development server is running. You can resolve the issues and hot reload will allow you to continue development without having to restart the server.
174+
175+
```bash
176+
[hotreload] - Application reloaded.
177+
178+
Rogue route detected. `routes/api.dart` should be renamed to `routes/api/index.dart`.
179+
```
180+
181+
When generating a production build via `dart_frog build`, Dart Frog will report all detected rogue routes and fail the build if one or more rogue routes are detected.

0 commit comments

Comments
Β (0)