Starlette: Support serving `index.html` for StaticFiles

Created on 26 Oct 2018  路  8Comments  路  Source: encode/starlette

We should add an option for serving index.html files from StaticFiles for directory URLs so that you can use it for serving websites. We could also support serving any 404.html file in the root of the directory for 404 responses. (Ie. Mirror the behaviour of GitHub pages)

Most helpful comment

Commenting here for future reference. Here's how you do it.

StaticFiles(directory=..., html=True)

All 8 comments

With the addition of #198 and the ability to provide a template_directory to the application, do we still want this feature? Seems like duplicated functionality to me but I'm sure there are use cases I haven't considered.

Yeah we鈥檇 still like it. Being able to serve a directory of content with a one liner would be super useful.

Almost done with this one. I'm assuming we want a feature switch in the StaticFiles constructor for this? Something to the effect of:

def __init__(
    self,
    *,
    directory: str,
    check_dir: bool = True,
    serve_html: bool = False
) -> None

(I'm terrible at naming things ;) )
Just wanted to double check before making API changes all willy-nilly.

Exactly yes.
I guess a long-winded serve_index_html would be okay here.

Another possibility might be to patch WhiteNoise to support ASGI and use WhiteNoise for this kind of use case.

Fairly sure there鈥檚 an open ticket on WhiteNoise for ASGI support, yup.

Commenting here for future reference. Here's how you do it.

StaticFiles(directory=..., html=True)

Thanks @dgrahn, yup! Just released with 0.12.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Immortalin picture Immortalin  路  3Comments

rlewkowicz picture rlewkowicz  路  3Comments

jordic picture jordic  路  6Comments

kouohhashi picture kouohhashi  路  3Comments

zhammer picture zhammer  路  5Comments