简体   繁体   English

Azure Devops set max-age on index.html in Azure storage with Azure CDN on static web site

[英]Azure Devops set max-age on index.html in Azure storage with Azure CDN on static web site

So, I don't want my index.html on my static vue.js app to always be cached because of updates.所以,我不希望我的 static vue.js 应用程序上的 index.html 因为更新而始终被缓存。 I can set max-age in the storage explorer manually.我可以在存储资源管理器中手动设置 max-age。 I have a Azure CDN set up for this site, which I purge after every deploy, but I don't think this will always update current users because the max-age on the index.html isn't set.我为此站点设置了 Azure CDN,每次部署后我都会清除它,但我认为这不会总是更新当前用户,因为 index.html 上的 max-age 未设置。 Is there a way to set from CDN the max-age?有没有办法从 CDN 设置最大年龄? Or is there a azure cli command that I should use on devops deploy to set the max-age on the index.html file in storage.或者是否有一个 azure cli 命令,我应该在 devops deploy 上使用它来设置存储中 index.html 文件的 max-age。 I don't see much documentation on this, maybe the CDN just takes care of everything?我没有看到太多关于这方面的文档,也许 CDN 只负责处理所有事情?

Make sure you set the correct values for both the "server-side caching" by the CDN and the "local caching" by your browser.确保为 CDN 的“服务器端缓存”和浏览器的“本地缓存”设置正确的值。 You need to set Max-Age on the blob to have it be always current in the CDN, while using the Cache-Control header on the Blob to have the browser always read the current version.您需要在 blob 上设置 Max-Age 以使其始终在 CDN 中保持最新,同时在 Blob 上使用Cache-Control header 以使浏览器始终读取当前版本。 This is also known as "Internal Max Age" vs "External Max Age"(Cache-Control) in CDN, though the naming varies depending on the provider.这在 CDN 中也称为“内部最大年龄”与“外部最大年龄”(缓存控制),尽管命名因提供商而异。

See also my recent answer on this: How to stop index.html being cached by Azure CDN另请参阅我最近对此的回答: 如何停止 index.html 被 Azure CDN 缓存

For me, I found it is usually sufficient to set the Cache-Control header in blob storage on the index.html in order to control both the server-side and the local caching.对我来说,我发现在index.html的 blob 存储中设置Cache-Control header 通常就足够了,以便同时控制服务器端和本地缓存。 At least the Azure CDN Provider respects this for server-side as well, so you have minimal effort.至少 Azure CDN 提供程序在服务器端也尊重这一点,因此您只需付出最少的努力。

See also this code example if you want to add it to your pipeline: https://schwabencode.com/blog/2019/05/03/Update-Azure-Storage-Blob-Properties-with-PowerShell如果要将其添加到管道,另请参阅此代码示例: 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" 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM