Skip to content

Commit

Permalink
Move static handlers for Auspice and Gatsby into endpoints/static
Browse files Browse the repository at this point in the history
Better separation of concerns here.  Note that instead of using the
"fallthrough" option to expressStaticGzip (which passes it thru to
express.static, which passes it thru to serve-static), I stack an
"always 404" handler after the static handler.  Otherwise, the
fallthrough behaviour would be non-obvious and baffling to anyone
reading app.js.  Falling through is part of the routing, which belongs
solely in app.js, while how to serve the assets is part of the endpoint.
  • Loading branch information
tsibley committed Nov 19, 2021
1 parent f4a6059 commit 5171a53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const sslRedirect = require('heroku-ssl-redirect');
const nakedRedirect = require('express-naked-redirect');
const express = require("express");
const expressStaticGzip = require("express-static-gzip");
const favicon = require('serve-favicon');
const compression = require('compression');
const utils = require("./utils");
Expand All @@ -14,7 +13,7 @@ const production = process.env.NODE_ENV === "production";

const charon = require("./endpoints/charon");
const users = require("./endpoints/users");
const {assetPath, auspiceAssetPath, gatsbyAssetPath, sendAuspiceEntrypoint, sendGatsbyEntrypoint, sendGatsbyPage, sendGatsby404} = require("./endpoints/static");
const {assetPath, auspiceAssets, gatsbyAssets, sendAuspiceEntrypoint, sendGatsbyEntrypoint, sendGatsbyPage, sendGatsby404} = require("./endpoints/static");
const {setSource, setGroupSource, setDataset, setNarrative, canonicalizeDataset, ifDatasetExists, ifNarrativeExists} = require("./endpoints/sources");
const authn = require("./authn");
const redirects = require("./redirects");
Expand Down Expand Up @@ -257,7 +256,7 @@ app.route(["/users", "/users/:name"])
* so we must use that prefix here too.
*/
app.route("/dist/*")
.all(expressStaticGzip(auspiceAssetPath(), {fallthrough: false, maxAge: '30d'}));
.all(auspiceAssets, (req, res, next) => next(new NotFound()));


/* Gatsby static HTML pages and other assets.
Expand All @@ -281,7 +280,7 @@ if (app.locals.gatsbyDevUrl) {
*/
app.use(createProxyMiddleware(app.locals.gatsbyDevUrl));
} else {
app.use(express.static(gatsbyAssetPath()));
app.use(gatsbyAssets);
}


Expand Down
13 changes: 11 additions & 2 deletions src/endpoints/static.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const express = require("express");
const expressStaticGzip = require("express-static-gzip");
const {InternalServerError} = require("http-errors");
const path = require("path");

Expand All @@ -16,6 +18,13 @@ const gatsbyAssetPath = (...subpath) =>
assetPath("static-site", "public", ...subpath);


/* Handlers for static assets.
*/
const auspiceAssets = expressStaticGzip(auspiceAssetPath(), {maxAge: '30d'});

const gatsbyAssets = express.static(gatsbyAssetPath());


/**
* Send the file at the given `filePath` as the response.
*
Expand Down Expand Up @@ -149,8 +158,8 @@ const sendGatsby404 = async (req, res) => {

module.exports = {
assetPath,
auspiceAssetPath,
gatsbyAssetPath,
auspiceAssets,
gatsbyAssets,

sendFile,
sendAuspiceEntrypoint,
Expand Down

0 comments on commit 5171a53

Please sign in to comment.