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 ffba4a0
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 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,34 @@ 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
});
const paths = {
client: path.join(__dirname, '/client'),
prerendered: path.join(__dirname, '/prerendered'),
static: path.join(__dirname, '/static')
};

/**
* @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
})
);
}

const serve_client = serve(paths.client, 31536000);

const serve_static = serve(paths.static, 31536000);

const serve_prerendered = serve(paths.prerendered, 0);

/** @type {import('polka').Middleware} */
const ssr = async (req, res) => {
Expand Down Expand Up @@ -83,4 +90,6 @@ function sequence(handlers) {
};
}

export const handler = sequence([serve_client, serve_static, serve_prerendered, ssr]);
export const handler = sequence(
[serve_client, serve_static, serve_prerendered, ssr].filter(Boolean)
);

0 comments on commit ffba4a0

Please sign in to comment.