1

How should we setup the nginx-expires setting only for files that include a hash in the name?

e.g. the angular build process will output:

enter image description here

where:

  • ngs-worker.js (non-hashed filename) must not be cached
  • polyfills-es5.3eb4a9891f4a514281f1.js etc. (hashed file-name) can be cached forever

Now, I know I could setup max cache expiry for all js files and exclude some (e.g. ngs-worker.js)
But this would be disastrous when I forget a non-hashed file or if a future version will produce additional non-hashed files.

How should we handle this?
Maybe use a regex to find all hashed-files?

1
  • 1
    Construct a regular expression that only matches filenames with a hash, and use a map to set the expires value. See this answer. Commented May 14, 2020 at 8:58

1 Answer 1

0

We use a regex to find all files that include a 20-char hash in the name (surrounded by dots)

# e.g. 'polyfills-es5.3eb4a9891f4a514281f1.js'. 'roboto-v18-latin-300.815babffe97b00a1f30b.woff2', .. location ~* "\.[a-f0-9]{20}\." { access_log off; expires max; } 

note, that the location regex must be quoted - see this SO answer

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.