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 15 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
16 changes: 10 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 @@ -29,8 +29,12 @@ export function createServer({ render }) {

const assets_handler = fs.existsSync(paths.assets)
? sirv(paths.assets, {
maxAge: 31536000,
immutable: true,
setHeaders: (res, pathname, stats) => {
// eslint-disable-next-line no-undef
if (pathname.startsWith(`/${esbuild_app_dir}/`)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require that the user not prefix or suffix config.kit.appDir with /. I think it'd be good to add either some sanitation or validation. E.g. we could automatically strip that prefix / suffix character. Or we could assert its value as we're doing for the base path in dc56d3c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benmccann added check for leading slash + app_dir as root

Copy link
Contributor Author

@bjon bjon Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and app_dir can not be an empty string

Copy link
Contributor Author

@bjon bjon Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But maybe these checks are unnecessary? SvelteKit doesn't work with app_dir: "/" or app_dir: "/my_app_dir" anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should probably be fixed in another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still looks like it will not work if someone specifies config.kit.appDir with a trailing slash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaa stupid me...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benmccann fixed

res.setHeader('cache-control', 'public, max-age=31536000, immutable');
}
},
gzip: true,
brotli: true
})
Expand Down