Visual Studio Development Server not updating css and javascript?

后端 未结 4 1167
悲哀的现实
悲哀的现实 2020-12-09 08:46

I\'ve recently returned to a web site project that has been on the backburner. Since recommencing work I\'ve noticed css and javascript changes aren\'t being recognised by

相关标签:
4条回答
  • 2020-12-09 09:23

    The server may be sending headers to the browser that cause it to keep using cached copies. The simple way to test this is to empty your browser cache.

    If that fixes it, you need to study the HTTP headers you get from the server. The developer tools (a.k.a. F12 tools) in your browser of choice will expose the headers returned by the server. Then decide if you want to keep using these caching settings (good for speed) or change them (good for development).

    And how do you adjust these headers, you ask? It depends on the server. Here is a link to the instructions for common servers:

    • Apache
    • nginx
    • IIS
    0 讨论(0)
  • 2020-12-09 09:25

    A quick way is to add random parameters after the script or css file's src attribute. for example

    <script type="javascript" src="@Url.Content("~/scripts/myScripts.js?" + DateTime.Now.ToString("ddMMHHmmss")"></script>
    

    so browser will always assume its a new file and will not cache.

    Be sure to remove this when deploying on live server.

    0 讨论(0)
  • 2020-12-09 09:34

    If you always want to get the last version of js and CSS files, you could modify your StaticFile middleware like

    app.UseStaticFiles(new StaticFileOptions()
    {
        OnPrepareResponse = context =>
        {
            context.Context.Response.Headers.Add("Cache-Control", "no-cache, no-store");
            context.Context.Response.Headers.Add("Expires", "-1");
        }
    });
    

    or you could add asp-append-version="true" to your file references like:

    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>
    
    0 讨论(0)
  • 2020-12-09 09:38

    Try to use

    <link href"~/Content/Style.css" rel="stylesheet"/>

    together with bundles. This worked for me

    0 讨论(0)
提交回复
热议问题