nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Generated .js files 404 and cause outages every deployment

Open georgesaliba opened this issue 9 months ago • 5 comments

Environment

nitro: { preset: 'firebase', firebase: { gen: 2 } }

Reproduction

Unfortunately I believe this issue likely only repros in production environments that leverage caching. I've tried to outline the high-level repro setup in the bug description.

Describe the bug

(Apologies in advance if this isn't the right place to request this, but I believe this is a Nitro issue and not a Nuxt issue.)

I'm running Nuxt 3 on Firebase, using the Nitro firebase preset. Whenever I deploy my site to production, it causes an outage. After digging into the issue, I've found the that .js files are returning 404 immediately after the deployment, and that 404 response is being cached for long periods (in both Cloudflare and user browsers). I have a hypothesis on what is happening:

  1. When I deploy the site, the new pages are referencing .js files with new build hashes.
  2. I believe that immediately after this deployment completes, the new Firebase cloud functions likely aren't fully spun up yet and/or the old functions are still taking some of the traffic. So it's the previously deployed Firebase cloud functions that receive & handle the new requests.
  3. Because those old functions don't understand the new build hash, they're returning a 404 response when the new page is requesting the new build hash .js files. (I can see the 404 in the response body when trying to load the .js files with the new build hash directly in the browser; screenshot in comments).
  4. That .js file with the 404 response is then being cached on the client browser.

Now, I think the workaround to this would be to never cache any of the .js files. But that's a useful performance optimization (and potentially cost prohibitive to disable). I think the right solution is for Nitro to remove the cache control header for any 4XX (and possibly 5XX) response, and to add a no-cache header in this scenario.

Additional context

No response

Logs

No response

georgesaliba avatar Sep 19 '23 17:09 georgesaliba