Skip to content

Commit

Permalink
fix: set revalidate cache header on 404'd static assets (#12530)
Browse files Browse the repository at this point in the history
* fix: set revalidate cache header on 404'd static assets

Some adapters (like Vercel) apply a immutable cache header by default to static assets. They respect explicitly set headers though. Therefore apply a "must immediately revalidate" cache header to the 404 response of static assets in the SvelteKit runtime

related to #9089

* Update packages/kit/src/runtime/server/respond.js

Co-authored-by: Conduitry <git@chor.date>

---------

Co-authored-by: Conduitry <git@chor.date>
  • Loading branch information
dummdidumm and Conduitry authored Aug 2, 2024
1 parent 07550a5 commit 008056b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/light-pears-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: set revalidate cache header on 404'd static assets
5 changes: 4 additions & 1 deletion packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export async function respond(request, options, manifest, state) {
}

if (decoded.startsWith(`/${options.app_dir}`)) {
return text('Not found', { status: 404 });
// Ensure that 404'd static assets are not cached - some adapters might apply caching by default
const headers = new Headers();
headers.set('cache-control', 'public, max-age=0, must-revalidate');
return text('Not found', { status: 404, headers });
}

const is_data_request = has_data_suffix(decoded);
Expand Down

0 comments on commit 008056b

Please sign in to comment.