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 2 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
---

Moved 'static' to separate folder, for better cache control.
5 changes: 3 additions & 2 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export default function ({ out = 'build' } = {}) {

async adapt(utils) {
utils.log.minor('Copying assets');
const static_directory = join(out, 'assets');
utils.copy_client_files(static_directory);
const static_directory = join(out, 'static');
const assets_directory = join(out, 'assets');
utils.copy_client_files(assets_directory);
utils.copy_static_files(static_directory);

utils.log.minor('Copying server');
Expand Down
16 changes: 12 additions & 4 deletions packages/adapter-node/src/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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.

const __dirname = dirname(fileURLToPath(import.meta.url));
const noop_handler = (_req, _res, next) => next();
const paths = {
assets: join(__dirname, '/assets'),
static: join(__dirname, '/static'),
prerendered: join(__dirname, '/prerendered')
};

Expand All @@ -27,6 +28,12 @@ export function createServer({ render }) {
? mutable(paths.prerendered)
: noop_handler;

const static_handler = fs.existsSync(paths.static)
? sirv(paths.static, {
maxAge: 3600
benmccann marked this conversation as resolved.
Show resolved Hide resolved
})
: noop_handler;

const assets_handler = fs.existsSync(paths.assets)
? sirv(paths.assets, {
maxAge: 31536000,
Expand All @@ -37,6 +44,7 @@ export function createServer({ render }) {
const server = polka().use(
compression({ threshold: 0 }),
assets_handler,
static_handler,
prerendered_handler,
async (req, res) => {
const parsed = new URL(req.url || '', 'http://localhost');
Expand Down