Enable Cache On Azure CDN -


i setting azure cdn, , having trouble setting cache-control header.

i used cloudberry explorer setup sync between server folders , cdn. working well. files copied cdn no problem.

under tools > http headers > edit http header set value cache-control be: public,max-age=604800

however, not appear having effect (according both fiddler , page speed).

any tips on setting cache-control header azure cdn appreciated.

i had issue myself , needed update cache-control header on thousands of files. prevent caching issues in sites, re-deploy these files every release new path.

i able patch different suggestions online , landed on following solution, use deploying 1 of production apps.

you need 2 files, , script assumes they're in same directory on computer:

  • a text file listing of files in container (see example below)
  • the powershell script

the text file (file-list.txt)

the file should in example format below full file path deployed cdn container. note uses forward slashes, , should not include container name since included in script. name of text file included in powershell script below.

v12/app/app.js v12/app/app.min.js v12/app/app.min.js.map v12/app/account/signup.js v12/app/account/signup.min.js ... (and on) 

the script (cdn-cache-control.ps1)

the full script below. you'll need replace constants storage_account_name, storage_key, , may need update path azure sdk dll if have different version. there 2 possible implementations of $blobclient; repurposed of code source online, , un-commented 1 works me.

the key difference between have here , you'll find online inclusion of $blob.fetchattributes(). without explicitly calling method, majority of blob properties content-type, last modified date, , others loaded memory empty/default values, when $blob.setproperties() called these empty values blow away existing ones in cdn, causing files load without content-type among other things.

add-type -path "c:\program files\microsoft sdks\azure\.net sdk\v2.9\bin\microsoft.windowsazure.storageclient.dll"  $accountname = "storage_account_name" $accountkey = "storage_key" $blobcontainername = "storage_container_name"  $storagecredentials = new-object microsoft.windowsazure.storagecredentialsaccountandkey -argumentlist $accountname,$accountkey $storageaccount = new-object microsoft.windowsazure.cloudstorageaccount -argumentlist $storagecredentials,$true #$blobclient = $storageaccount.createcloudblobclient() $blobclient = [microsoft.windowsazure.storageclient.cloudstorageaccountstorageclientextensions]::createcloudblobclient($storageaccount)  $cachecontrolvalue = "max-age=31556926"  echo "setting cache control: $cachecontrolvalue"  get-content "file-list.txt" | foreach {     $blobname = "$blobcontainername/$_".trim()     $blob = $blobclient.getblobreference($blobname)     $blob.fetchattributes()      $blob.properties.cachecontrol = $cachecontrolvalue     $blob.setproperties()     echo $blobname } 

it tricky find information mass-setting cache-control header i've run script multiple production releases great success. i've verified configuration of header well, , routinely run google's pagespeed insights against site verify.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -