Serve a static frontend build as low-priority routes. Use this for frontend tools that build static files into a directory, such as `dist`. **FastAPI** path operations are checked first, and the frontend files are checked only if no normal route matched. A
(
self,
path: Annotated[
str,
Doc(
"""
The URL path prefix where the frontend build should be served.
"""
),
],
*,
directory: Annotated[
str | os.PathLike[str],
Doc(
"""
The directory containing the static frontend build output.
"""
),
],
fallback: Annotated[
Literal["auto", "index.html", "404.html"] | None,
Doc(
"""
The fallback file behavior for missing frontend paths.
"""
),
] = "auto",
check_dir: Annotated[
bool,
Doc(
"""
Check that the frontend directory exists when the app is created.
"""
),
] = True,
)
| 1220 | ) |
| 1221 | |
| 1222 | def frontend( |
| 1223 | self, |
| 1224 | path: Annotated[ |
| 1225 | str, |
| 1226 | Doc( |
| 1227 | """ |
| 1228 | The URL path prefix where the frontend build should be served. |
| 1229 | """ |
| 1230 | ), |
| 1231 | ], |
| 1232 | *, |
| 1233 | directory: Annotated[ |
| 1234 | str | os.PathLike[str], |
| 1235 | Doc( |
| 1236 | """ |
| 1237 | The directory containing the static frontend build output. |
| 1238 | """ |
| 1239 | ), |
| 1240 | ], |
| 1241 | fallback: Annotated[ |
| 1242 | Literal["auto", "index.html", "404.html"] | None, |
| 1243 | Doc( |
| 1244 | """ |
| 1245 | The fallback file behavior for missing frontend paths. |
| 1246 | """ |
| 1247 | ), |
| 1248 | ] = "auto", |
| 1249 | check_dir: Annotated[ |
| 1250 | bool, |
| 1251 | Doc( |
| 1252 | """ |
| 1253 | Check that the frontend directory exists when the app is created. |
| 1254 | """ |
| 1255 | ), |
| 1256 | ] = True, |
| 1257 | ) -> None: |
| 1258 | """ |
| 1259 | Serve a static frontend build as low-priority routes. |
| 1260 | |
| 1261 | Use this for frontend tools that build static files into a directory, |
| 1262 | such as `dist`. **FastAPI** path operations are checked first, and |
| 1263 | the frontend files are checked only if no normal route matched. |
| 1264 | |
| 1265 | A typical project could look like this: |
| 1266 | |
| 1267 | ```text |
| 1268 | . |
| 1269 | ├── pyproject.toml |
| 1270 | ├── app |
| 1271 | │ ├── __init__.py |
| 1272 | │ └── main.py |
| 1273 | └── dist |
| 1274 | ├── index.html |
| 1275 | └── assets |
| 1276 | └── app.js |
| 1277 | ``` |
| 1278 | |
| 1279 | Then in `app/main.py`: |
no outgoing calls