Svelte cache 404 bug

I’m facing an issue with Vercel and my sveltekit project. I do not seem to be alone in this issue, there is a long running github issue Not found /_app/immutable/assets/xxx · Issue #9089 · sveltejs/kit · GitHub

It seems that Vercel is caching 404 responses for static assets. This is not what sveltekit intends, at least not in adapter-node, it is only supposed to cache when the response is 200. You can see this on line 68 of the node adapter. kit/packages/adapter-node/src/handler.js at main · sveltejs/kit · GitHub

You can reproduce this by attempting to fetch any url under _app/immutable that doesn’t exist. You can also hit the same path when running locally with npm preview and see the Cache-Control headers are not set.

As to why this is an issue for me, we’re facing issues where these cached 404s are occasionally being served after new deploys, there may be a race condition where the file isn’t available yet but some JS is requesting it causing the 404 to cache.

Also, I’ve filed this as a support ticket with Vercel 3 days ago, I am on a Pro plan, and pretty shocked that it takes this long for a first reply.

1 Like

Thanks for reporting this, @steve-staysupplec! I need to get help from our cache team and the Svelte experts. Will keep you updated about what I learn

Do you have a minimal reproducible example we can use to investigate the errors?

Hi an asset that doesn’t exist for any sveltekit project on vercel that uses adapter-vercel

https://www.bandwith.us/_app/immutable/anything.js

This has cloudflare in front of it, but that doesn’t matter, adjust the url if you want another example to anything2

I’ve been digging into adapter-vercel to see the source of the issue. Have them look at how static assets are handled differently in adapter-vercel than adapter-node, you can see on line 482, there is no check for the status of the response, just to set the headers kit/packages/adapter-vercel/index.js at main · sveltejs/kit · GitHub

1 Like

Any update? Need anything else from me?

We’re still investigating. If you can share a minimal reproducible example repo, that would let us see the problem more clearly. We’re having trouble replicating the issue with other Svelte sites and examples.

Again, not sure why you would need that, just hit the URL and look at the headers. I’ve sumitted a pull request to adapter-vercel, have someone on the team review, they should be able to understand and either merge or fix their way. Disable caching of 404s for immutable assets in vercel-adapter by smart · Pull Request #12526 · sveltejs/kit · GitHub

We need the MRE so we can pinpoint exactly why it is happening for some sites and not others. We haven’t been able to find an example repo or our own that demonstrates the error. A minimal version of your site would let us see what combination of features and configuration trigger the error so we can fix it. :pray:

Additionally, we’ll need to test any possible solutions before merging them, both to ensure the attempted solution actually works and to avoid breaking anything else. We need an example repo that demonstrates the error to facilitate all of that.

Literally any sveltekit project that uses adapter-vercel, here is a url to hit from Skeleton https://www.skeleton.dev/_app/immutable/assets/0.DXYGQadfaHuM.css

you can see the cache headers.

Here an MRE, it is just a totally blank sveltekit template with adapter-vercel installed.
Here is the github and vercel url to random asset that 404s and has the year long cache header.

code: GitHub - smart/mre

404 asset: https://mre-git-main-stevestaysupplecs-projects.vercel.app/_app/immutable/adfadfasfd.js

1 Like

I’ve submitted a pull request to adapter-vercel which I’m using in production successfully. This or a similar fix should solve the issue for us, but I will point out the race condition for assets becoming available on the filesystem will continue to exist. I did not create a MRE for that.

Thanks for proposing your change and for sharing your example code. That was very helpful!

The Svelte team decided to make a slightly different change to accomplish the same goal in a more widely applicable way. It will make sure static resource 404s cache headers behave correctly in general instead of targeting the Vercel adapter specifically.

I see in the PR comments that you were already able to try new solution, but wanted to make sure the information made it into this post in case it helps anyone else following this topic. Thanks again for your help fixing this bug!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.