Skip to content

Correct default route usage #677

@kkinder

Description

@kkinder

Default Routes for SPA's -- what's the correct way to do them

Suppose you have an SPA where https://example.com/foo/bar/spam should be routed in JavaScript, not in Python.

As far as I can tell, the "correct" way to do that seems something like this:

async def default_route(scope, receive, send):
    request = Request(scope, receive=receive, send=send)

    response = templates.TemplateResponse("spa.html", {'request': request})
    await response(scope, receive, send)

app.router.default = default_route

That seems a little dirty however, since it's jumping outside both FastAPI and Starlette's documentation and just setting default to a function that then recreates the request object. It isn't very FastAPI-y.

Is there perhaps another, alternative way to establish a default route?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions