【问题标题】:Azure Devops set max-age on index.html in Azure storage with Azure CDN on static web siteAzure Devops 使用静态网站上的 Azure CDN 在 Azure 存储中的 index.html 上设置 max-age
【发布时间】:2020-06-28 17:35:36
【问题描述】:

所以,我不希望我的静态 vue.js 应用程序上的 index.html 因为更新而始终被缓存。我可以在存储资源管理器中手动设置 max-age。我为此站点设置了 Azure CDN,每次部署后都会清除它,但我认为这不会始终更新当前用户,因为未设置 index.html 上的 max-age。 有没有办法从 CDN 设置最大年龄? 或者是否有一个我应该在 devops deploy 上使用的 azure cli 命令来设置存储中 index.html 文件的 max-age。 我没有看到太多这方面的文档,也许 CDN 只负责处理所有事情?

【问题讨论】:

    标签: azure azure-web-app-service azure-cdn azure-static-website-hosting


    【解决方案1】:

    确保您为 CDN 的“服务器端缓存”和浏览器的“本地缓存”设置了正确的值。您需要在 blob 上设置 Max-Age 以使其始终在 CDN 中处于最新状态,同时在 Blob 上使用 Cache-Control 标头以使浏览器始终读取当前版本。这在 CDN 中也称为“Internal Max Age”与“External Max Age”(缓存控制),但名称因提供商而异。

    另请参阅我最近对此的回答: How to stop index.html being cached by Azure CDN

    对我来说,我发现在 index.html 的 blob 存储中设置 Cache-Control 标头通常就足够了,以便同时控制服务器端和本地缓存。至少 Azure CDN 提供程序在服务器端也尊重这一点,因此您只需付出最少的努力。

    如果您想将其添加到您的管道,另请参阅此代码示例: https://schwabencode.com/blog/2019/05/03/Update-Azure-Storage-Blob-Properties-with-PowerShell

    # Storage Settings
    $storageAccount = "__storage account__";
    $containerName = "__container name__";
    
    # Blob Update Settings
    $contentCacheControl = "public, max-age=2592000"; # 30 days
    $extensions = @(".gif", ".jpg", ".jpeg", ".ico", ".png", ".css", ".js");
    
    # Read all blobs
    $blobs = az storage blob list --account-name $storageAccount --container-name $containerName --num-results * --output json | ConvertFrom-Json
    
    # iterate all blobs
    foreach($blob in $blobs)
    {
        # use name as identifier
        $blobName = $blob.name;
    
        # get extension
        $extension = [System.IO.Path]::GetExtension($blobName).ToLower();
     
        # update blob if extension is affected
        if($extensions.Contains($extension))
        {
            az storage blob update --account-name $storageAccount --container-name $containerName --name $blobName --content-cache-control $contentCacheControl
            Write-Host "Updated $blobName" 
        }
    }
    

    【讨论】:

    • 哦,您使用的是 powershell 而不是 azure cli。目前我只是在 blob 中手动设置,仅用于 index.html 并将其设置为 1 分钟左右。似乎我可以在你有 index.html 的 devops 中使用 azure cli 中的最后一行,在 index.html 中我可以调用构建的每个新构建的 js 文件的版本号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2020-04-06
    • 2020-12-23
    • 2016-10-31
    • 2019-08-24
    • 2021-05-05
    • 2022-08-19
    相关资源
    最近更新 更多