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 cache content in /_app #1416

Merged
merged 19 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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