Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.86k stars 78 forks source link

More options to handle index.html cache control #297

Open RobertoPrevato opened 1 year ago

RobertoPrevato commented 1 year ago

🚀 Feature Request

    app.UseSpa(spa => {
        spa.Options.SourcePath = "ClientApp";

        spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions() {
            OnPrepareResponse = ctx => {
                // Do not cache default page index.html
                var headers = ctx.Context.Response.GetTypedHeaders();
                headers.CacheControl = new CacheControlHeaderValue {
                    NoStore = true,
                    NoCache = true,
                };
            },
        };

        if (env.IsDevelopment()) {
            //spa.UseAngularCliServer(npmScript: "start");
            spa.UseProxyToSpaDevelopmentServer("http://localhost:4202");
        }
    });

Suggested by netwang @netwang 16:30 on gitter:

Above code is ASP.NET 6.0 code to configure the cache control of the static file index.html. NoStore and NoCache is better for index.html in Angular application. Could you check C# class CacheControlHeaderValue and add the similar function to BlackSheep?

RobertoPrevato commented 1 year ago

@netwang This feature is now available with https://pypi.org/project/blacksheep/2.0a4/

from blacksheep.server.files import DefaultFileOptions
from blacksheep.server.headers.cache import CacheControlHeaderValue

...

    app.serve_files(
        "app/static",
        fallback_document="index.html",
        default_file_options=DefaultFileOptions(
            cache_control=CacheControlHeaderValue(no_cache=True, no_store=True),
        ),
    )