Skip to content

Commit

Permalink
Only cache content in /_app (#1416)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjon committed Jun 16, 2021
1 parent 1c78dec commit 318cdd7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-files-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

Only cache files in config.kit.appDir
9 changes: 6 additions & 3 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -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 {{
Expand All @@ -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);
Expand All @@ -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');
Expand Down
34 changes: 28 additions & 6 deletions packages/adapter-node/src/server.js
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
})
Expand Down

0 comments on commit 318cdd7

Please sign in to comment.