From 318cdd70a97468fc13befbfc1a925b1ccf9575c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6n=20Limell?= Date: Wed, 16 Jun 2021 15:27:30 +0200 Subject: [PATCH] Only cache content in /_app (#1416) --- .changeset/strong-files-obey.md | 5 +++++ packages/adapter-node/index.js | 9 +++++--- packages/adapter-node/src/server.js | 34 ++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 .changeset/strong-files-obey.md diff --git a/.changeset/strong-files-obey.md b/.changeset/strong-files-obey.md new file mode 100644 index 000000000000..fd6e6884a8a5 --- /dev/null +++ b/.changeset/strong-files-obey.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': patch +--- + +Only cache files in config.kit.appDir diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 5643b87b3dce..3a0a36309205 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -1,7 +1,7 @@ +import esbuild from 'esbuild'; import { readFileSync } from 'fs'; import { join } from 'path'; import { fileURLToPath } from 'url'; -import esbuild from 'esbuild'; /** * @param {{ @@ -13,7 +13,7 @@ export default function ({ out = 'build' } = {}) { const adapter = { name: '@sveltejs/adapter-node', - async adapt({ utils }) { + async adapt({ utils, config }) { utils.log.minor('Copying assets'); const static_directory = join(out, 'assets'); utils.copy_client_files(static_directory); @@ -29,7 +29,10 @@ export default function ({ out = 'build' } = {}) { external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}), format: 'esm', platform: 'node', - target: 'node12' + target: 'node12', + define: { + esbuild_app_dir: '"' + config.kit.appDir + '"' + } }); utils.log.minor('Prerendering static pages'); diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index ce8cb5f20f4c..26c2d099d7ed 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -1,11 +1,11 @@ +import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved +import { getRawBody } from '@sveltejs/kit/node'; // eslint-disable-line import/no-unresolved +import compression from 'compression'; import fs from 'fs'; import { dirname, join } from 'path'; -import { fileURLToPath } from 'url'; -import compression from 'compression'; import polka from 'polka'; import sirv from 'sirv'; -import { getRawBody } from '@sveltejs/kit/node'; // eslint-disable-line import/no-unresolved -import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved +import { fileURLToPath } from 'url'; // App is a dynamic file built from the application layer. @@ -23,14 +23,36 @@ export function createServer({ render }) { maxAge: 0 }); + const immutable_path = (pathname) => { + // eslint-disable-next-line no-undef + let app_dir = esbuild_app_dir; + + // hard to tell when app_dir is mixed with static + if (app_dir === '/') { + return false; + } + + if (app_dir.startsWith('/')) { + app_dir = app_dir.slice(1); + } + if (app_dir.endsWith('/')) { + app_dir = app_dir.slice(0, -1); + } + + return pathname.startsWith(`/${app_dir}/`); + }; + const prerendered_handler = fs.existsSync(paths.prerendered) ? mutable(paths.prerendered) : noop_handler; const assets_handler = fs.existsSync(paths.assets) ? sirv(paths.assets, { - maxAge: 31536000, - immutable: true, + setHeaders: (res, pathname, stats) => { + if (immutable_path(pathname)) { + res.setHeader('cache-control', 'public, max-age=31536000, immutable'); + } + }, gzip: true, brotli: true })