Skip to content

Commit

Permalink
only serve _app/build with immutable cache header, not _app/version.j…
Browse files Browse the repository at this point in the history
…son - fixes #4837
  • Loading branch information
Rich-Harris committed May 24, 2022
1 parent 6a30552 commit f28d823
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .changeset/wise-berries-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@sveltejs/adapter-cloudflare': patch
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

only serve \_app/build with immutable cache header, not \_app/version.json
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const static_asset_manifest = JSON.parse(static_asset_manifest_json);

const server = new Server(manifest);

const prefix = `/${manifest.appDir}/`;
const prefix = `/${manifest.appDir}/build/`;

export default {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Cache from 'worktop/cfw.cache';

const server = new Server(manifest);

const prefix = `/${manifest.appDir}/`;
const prefix = `/${manifest.appDir}/build/`;

/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-netlify/src/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Server } from '0SERVER';
import { manifest, prerendered } from 'MANIFEST';

const server = new Server(manifest);
const prefix = `/${manifest.appDir}/`;
const prefix = `/${manifest.appDir}/build/`;

/**
* @param { Request } request
Expand All @@ -12,6 +12,7 @@ const prefix = `/${manifest.appDir}/`;
export default function handler(request, context) {
if (is_static_file(request)) {
// Static files can skip the handler
// TODO can we serve _app/build files with an immutable cache header?
return;
}

Expand Down
23 changes: 14 additions & 9 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

/**
* @param {string} path
* @param {number} max_age
* @param {boolean} immutable
* @param {boolean} client
*/
function serve(path, max_age, immutable = false) {
function serve(path, client = false) {
return (
fs.existsSync(path) &&
sirv(path, {
etag: true,
maxAge: max_age,
immutable,
gzip: true,
brotli: true
brotli: true,
setHeaders:
client &&
((res, pathname) => {
// only apply to build directory, not e.g. version.json
if (pathname.startsWith(`/${manifest.appDir}/build/`)) {
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
}
})
})
);
}
Expand Down Expand Up @@ -126,9 +131,9 @@ function get_origin(headers) {

export const handler = sequence(
[
serve(path.join(__dirname, '/client'), 31536000, true),
serve(path.join(__dirname, '/static'), 0),
serve(path.join(__dirname, '/prerendered'), 0),
serve(path.join(__dirname, '/client'), true),
serve(path.join(__dirname, '/static')),
serve(path.join(__dirname, '/prerendered')),
ssr
].filter(Boolean)
);
2 changes: 1 addition & 1 deletion packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async function v1(builder, external) {
...prerendered_pages,
...prerendered_redirects,
{
src: `/${builder.config.kit.appDir}/.+`,
src: `/${builder.config.kit.appDir}/build/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
Expand Down

0 comments on commit f28d823

Please sign in to comment.