Skip to content

Commit

Permalink
fix: only add handlers for directories that exist
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Dec 30, 2021
1 parent a1e980f commit 0407093
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import path from 'path';
import sirv from 'sirv';
import { fileURLToPath } from 'url';
Expand All @@ -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) => {
Expand Down Expand Up @@ -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)
);

0 comments on commit 0407093

Please sign in to comment.