1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Cache-Control ASP.NET Core (7) - Weird browser behaviour

Discussão em 'Mobile' iniciado por Stack, Outubro 15, 2024 às 17:23.

  1. Stack

    Stack Membro Participativo

    We have a flutter->asp.net core (7) (just api and serving the flutter build/assets) stack, for an android app and web application.

    We were having client issues on the web with browsers not downloading the latest build of our app, even when the querystring in html link to the build was changed.

    so I added no-cache headers to the response for index.html:

    app.UseStaticFiles(new StaticFileOptions
    {
    // disabled cache for index.html
    OnPrepareResponse = ctx =>
    {
    if (ctx.File.Name == "index.html")
    {
    ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
    ctx.Context.Response.Headers.Append("Pragma", "no-cache");
    ctx.Context.Response.Headers.Append("Expires", "0");
    }
    }
    });


    this worked fine... except now all our api responses were being cached by the browser (or flutter?) (chrome/edge)

    I subsequently added a middleware to disable caching for all api route requests.

    using Microsoft.AspNetCore.Http;
    using System.Threading.Tasks;

    namespace GlobalAttributes.WebServices.Middleware;

    public class NoCacheMiddleware
    {
    private readonly RequestDelegate _next;

    public NoCacheMiddleware(RequestDelegate next)
    {
    _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
    if (context.Request.Path.StartsWithSegments("/api"))
    {
    context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
    context.Response.Headers["Pragma"] = "no-cache";
    context.Response.Headers["Expires"] = "0";
    }

    await _next(context);
    }
    }


    this works, however in the interest of filing a correct incident report...

    the only hypothesis is that explicitly setting the cache headers, made browsers/flutter? "pay attention" to the fact that the cache headers were not explicitly set for the api routes?

    we also can't reproduce this on any test server, even separate iis instances on the same machine.

    Does anyone have ANY idea for the cause? The header responses for the api were the same before and after the "index.html" explicit change was made.

    Continue reading...

Compartilhe esta Página