Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only serve _app/immutable with immutable cache headers #5051

Merged
merged 16 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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)) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
// Static files can skip the handler
// TODO can we serve _app/build files with an immutable cache header?
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 4 additions & 2 deletions packages/kit/src/core/build/build_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function build_client({
build: {
cssCodeSplit: true,
manifest: true,
outDir: client_out_dir,
outDir: `${client_out_dir}/build`,
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
polyfillDynamicImport: false,
rollupOptions: {
input,
Expand Down Expand Up @@ -95,7 +95,9 @@ export async function build_client({
const { chunks, assets } = await create_build(merged_config);

/** @type {import('vite').Manifest} */
const vite_manifest = JSON.parse(fs.readFileSync(`${client_out_dir}/manifest.json`, 'utf-8'));
const vite_manifest = JSON.parse(
fs.readFileSync(`${client_out_dir}/build/manifest.json`, 'utf-8')
);

const entry = posixify(client_entry_file);
const entry_js = new Set();
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Server {
manifest,
method_override: ${s(config.kit.methodOverride)},
paths: { base, assets },
prefix: assets + '/${config.kit.appDir}/',
prefix: assets + '/${config.kit.appDir}/build/',
prerender: {
default: ${config.kit.prerender.default},
enabled: ${config.kit.prerender.enabled}
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function build_service_worker(

export const build = [
${Array.from(build)
.map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
.map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/build/${file}`)}`)
.join(',\n\t\t\t\t')}
];

Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function build(config, { log }) {
// during `svelte-kit preview`, because we use a local asset path. If Vite
// used relative paths, I _think_ this could get fixed. Issue here:
// https://github.com/vitejs/vite/issues/2009
assets_base: `${config.kit.paths.assets || config.kit.paths.base}/${config.kit.appDir}/`,
assets_base: `${config.kit.paths.assets || config.kit.paths.base}/${config.kit.appDir}/build/`,
manifest_data,
output_dir,
client_entry_file: path.relative(cwd, `${get_runtime_path(config)}/client/start.js`),
Expand Down Expand Up @@ -65,8 +65,8 @@ export async function build(config, { log }) {

const files = new Set([
...static_files,
...client.chunks.map((chunk) => `${config.kit.appDir}/${chunk.fileName}`),
...client.assets.map((chunk) => `${config.kit.appDir}/${chunk.fileName}`)
...client.chunks.map((chunk) => `${config.kit.appDir}/build/${chunk.fileName}`),
...client.assets.map((chunk) => `${config.kit.appDir}/build/${chunk.fileName}`)
]);

// TODO is this right?
Expand Down
8 changes: 6 additions & 2 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ export async function preview({ port, host, config, https: use_https = false })
scoped(
assets,
sirv(join(config.kit.outDir, 'output/client'), {
maxAge: 31536000,
immutable: true
setHeaders: (res, pathname) => {
// only apply to build directory, not e.g. version.json
if (pathname.startsWith(`/${config.kit.appDir}/build`)) {
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
}
}
})
),

Expand Down
12 changes: 11 additions & 1 deletion packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ test.describe.parallel('Imports', () => {
]);
} else {
expect(sources[0].startsWith('data:image/png;base64,')).toBeTruthy();
expect(sources[1]).toBe(`${baseURL}/_app/assets/large-3183867c.jpg`);
expect(sources[1]).toBe(`${baseURL}/_app/build/assets/large-3183867c.jpg`);
}
});
});
Expand Down Expand Up @@ -2649,3 +2649,13 @@ test.describe.parallel('XSS', () => {
);
});
});

test.describe.parallel('Version', () => {
test('does not serve version.json with an immutable cache header', async ({ request }) => {
// this isn't actually a great test, because caching behaviour is down to adapters.
// but it's better than nothing
const response = await request.get('/_app/version.json');
const headers = response.headers();
expect(headers['cache-control'] || '').not.toContain('immutable');
});
});