<p>My frontend runs on nginx, and I'm serving a bunch of <code>.chunk.js</code> files that are built from react.</p>
<p>Every time I update my frontend, I rebuild the docker image and update the kubernetes deployment. However, <strong>some users might still be trying to fetch the old js files.</strong></p>
<p>I would <em>like</em> Google Cloud CDN to serve the stale, cached version of the old files, however it seems that <a href="https://cloud.google.com/cdn/docs/serving-stale-content" rel="nofollow noreferrer">it will only serve stale content in the event of errors or the server being unreachable, not a 404.</a></p>
<p>Cloud CDN also has something called &quot;negative caching&quot;, <a href="https://cloud.google.com/cdn/docs/using-negative-caching" rel="nofollow noreferrer">however that seems to be for deciding how long a 404 is cached.</a></p>
<h1></h1>
<p><strong>--&gt; What's the best way to temporarily serve old files on Google Cloud? Can this be done with Cloud CDN?</strong></p>
<p>(Ideally without some funky build process that requires deploying the old files as well)</p>

DemiPixel
<p>I have this issue as well, if you find a way to set it up on Google CDN please let me know.</p>
<p>Here is my Workaround:</p>
<ol>
<li><p>Choose <code>Use origin settings based on Cache-Control headers</code></p>
</li>
<li><p>Since most browsers cache static JS assets, I reduce my <code>Cache-Control</code> header for <code>.html</code> to be reasonably low or normal, like 5-60 minutes, whereas the javascript files have a much longer cache time, like a week.</p>
</li>
</ol>
<p>Some Context: After deployment, if google serves the old index.html from its CDN cache, the user's browser will request old JS files. If it's time for those JS files to be re-validated, google will see they are now 404s, and send a 404 response instead of the JS file. The workaround above makes sure that the JS files a highly likely to be available in the cache, while the index.html is updated more frequently.</p>
<p><strong>Update:</strong> This works... but there appears to be a caveat, if the page isn't a frequently trafficked page, google will eventually return a 404 on the javascript file before the specified time. Even though <a href="https://cloud.google.com/cdn/docs/caching#:%7E:text=Cloud%20CDN%20revalidates%20cached%20objects,if%20it%20were%202%2C592%2C000%20seconds" rel="nofollow noreferrer">google docs</a> state it won't get revalidated for 30 days, this appears to be false.</p>
<p><strong>Update 2</strong>: Google's Response:</p>
<blockquote>
<p>The expiration doc says &quot;Cloud CDN revalidates cached objects that are older than 30 days.&quot; It doesn't say that Google won't revalidate prior to 30 days. Things fall out of cache arbitrarily quickly and max-age is just an upper bound.</p>
</blockquote>
