Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended cache control method?

I'm looking to force refreshes of JS/CSS dependencies.

Will <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> work for that, or will that only force a refresh of the content within the page itself?

like image 280
TimFoolery Avatar asked Dec 28 '22 01:12

TimFoolery


2 Answers

You could use a server-side language to append a timestamp to each file being pulled in:

<?php $timestamp = time(); ?>
<link href="shell.css?timestamp=<?=$timestamp?>" rel="stylesheet" type="text/css" />

I've found that meta cache tags don't work consistently cross-browser, so this is my go-to if I need to force-reload something on page refresh.

like image 96
jpea Avatar answered Jan 12 '23 12:01

jpea


No, it controls only current document. If you dont want ugly URIs with random query-strings, its the time to configure your server. Assuming Apache:

# mod_expires directives: 
# enable expires/max-age headers and set default to 0 seconds from last access time
ExpiresActive On
ExpiresDefault A0
# configure ExpiresByType on your specific types, eg ExpiresByType text/css A0



# mod_headers directives:
# send variety of no-cache directives, should cover any quirky clients and gateways
Header set Cache-Control "max-age=0, private, no-cache, no-store, must-revalidate, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
# enclose this in <Files> directive for specific file eg <Files *.js>

These directive groups will work in per-directory configs (.htaccess files) too (in case of shared environment hosting), given following requirements met:

  1. AllowOverride FileInfo is in effect
  2. Either mod_expires or mod_headers is enabled

If both are enabled - note that groups are overlapping on max-age, you will want to remove it from Header and use finer control via ExpiresXXXX. Described setup is rather common for the shared hosting environment, so ask server admin or just try yourself (will return 500 Internal Server Error if corresponding module is not enabled or have no effect if .htaccess processing is not enabled)

like image 31
Free Consulting Avatar answered Jan 12 '23 11:01

Free Consulting