From 78a1bad8b715a101cc9d9087f5c3957d2550bf76 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 30 Dec 2021 12:13:26 -0800 Subject: [PATCH] [fix] only add handlers for directories that exist --- .changeset/little-tables-decide.md | 5 +++ packages/adapter-node/src/handler.js | 48 +++++++++++++++------------- 2 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 .changeset/little-tables-decide.md diff --git a/.changeset/little-tables-decide.md b/.changeset/little-tables-decide.md new file mode 100644 index 000000000000..4389ec468342 --- /dev/null +++ b/.changeset/little-tables-decide.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': patch +--- + +[fix] only add handlers for directories that exist diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 1bfeecb2b3af..ca766e68ca9c 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -1,3 +1,4 @@ +import fs from 'fs'; import path from 'path'; import sirv from 'sirv'; import { fileURLToPath } from 'url'; @@ -14,28 +15,22 @@ const app = new App(manifest); const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const serve_client = sirv(path.join(__dirname, '/client'), { - etag: true, - maxAge: 31536000, - immutable: true, - gzip: true, - brotli: true -}); - -const serve_static = sirv(path.join(__dirname, '/static'), { - etag: true, - maxAge: 31536000, - immutable: true, - gzip: true, - brotli: true -}); - -const serve_prerendered = sirv(path.join(__dirname, '/prerendered'), { - etag: true, - maxAge: 0, - gzip: true, - brotli: true -}); +/** + * @param {string} path + * @param {number} max_age + */ +function serve(path, max_age) { + return ( + fs.existsSync(path) && + sirv(path, { + etag: true, + maxAge: max_age, + immutable: true, + gzip: true, + brotli: true + }) + ); +} /** @type {import('polka').Middleware} */ const ssr = async (req, res) => { @@ -83,4 +78,11 @@ function sequence(handlers) { }; } -export const handler = sequence([serve_client, serve_static, serve_prerendered, ssr]); +export const handler = sequence( + [ + serve(path.join(__dirname, '/client'), 31536000), + serve(path.join(__dirname, '/static'), 31536000), + serve(path.join(__dirname, '/prerendered'), 0), + ssr + ].filter(Boolean) +);