From 7abd703f72bb3ee7808539927750355649063174 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 10 Mar 2020 20:46:44 +0000 Subject: [PATCH] fix: addressed issues with cache headers (#2873) Co-authored-by: Kim Gardner --- src/core/server/app/router/api/account.ts | 5 +++-- src/core/server/app/router/api/auth.ts | 4 +++- src/core/server/app/router/api/helpers.ts | 16 ++++++++++++++++ src/core/server/app/router/api/index.ts | 4 +++- src/core/server/app/router/api/install.ts | 6 ++++-- src/core/server/app/router/api/story.ts | 12 ++++++------ src/core/server/app/router/api/user.ts | 6 +++--- src/core/server/app/router/index.ts | 7 +------ 8 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/core/server/app/router/api/helpers.ts diff --git a/src/core/server/app/router/api/account.ts b/src/core/server/app/router/api/account.ts index 12cc7f466..100fbc652 100644 --- a/src/core/server/app/router/api/account.ts +++ b/src/core/server/app/router/api/account.ts @@ -1,5 +1,4 @@ import bodyParser from "body-parser"; -import express from "express"; import { AppOptions } from "coral-server/app"; import { @@ -17,11 +16,13 @@ import { jsonMiddleware } from "coral-server/app/middleware/json"; import { authenticate } from "coral-server/app/middleware/passport"; import { RouterOptions } from "coral-server/app/router/types"; +import { createAPIRouter } from "./helpers"; + export function createNewAccountRouter( app: AppOptions, { passport }: Pick ) { - const router = express.Router(); + const router = createAPIRouter(); router.post( "/confirm", diff --git a/src/core/server/app/router/api/auth.ts b/src/core/server/app/router/api/auth.ts index e081c6874..6bc5e1b56 100644 --- a/src/core/server/app/router/api/auth.ts +++ b/src/core/server/app/router/api/auth.ts @@ -19,6 +19,8 @@ import { } from "coral-server/app/middleware/passport"; import { RouterOptions } from "coral-server/app/router/types"; +import { createAPIRouter } from "./helpers"; + function wrapPath( app: AppOptions, { passport }: Pick, @@ -36,7 +38,7 @@ export function createNewAuthRouter( app: AppOptions, { passport }: Pick ) { - const router = express.Router(); + const router = createAPIRouter(); // Mount the Local Authentication handlers. router.post( diff --git a/src/core/server/app/router/api/helpers.ts b/src/core/server/app/router/api/helpers.ts new file mode 100644 index 000000000..9dc08e196 --- /dev/null +++ b/src/core/server/app/router/api/helpers.ts @@ -0,0 +1,16 @@ +import express from "express"; + +import { cacheHeadersMiddleware } from "coral-server/app/middleware/cacheHeaders"; + +interface Options { + cache?: false | string; +} + +export function createAPIRouter({ cache = false }: Options = {}) { + const router = express.Router(); + + // Add the cache headers middleware. + router.use(cacheHeadersMiddleware(cache)); + + return router; +} diff --git a/src/core/server/app/router/api/index.ts b/src/core/server/app/router/api/index.ts index 1bf7e6319..cc138c48d 100644 --- a/src/core/server/app/router/api/index.ts +++ b/src/core/server/app/router/api/index.ts @@ -36,6 +36,9 @@ export function createAPIRouter(app: AppOptions, options: RouterOptions) { // only proceed if there is a valid Tenant for the hostname. router.use(tenantMiddleware({ cache: app.tenantCache })); + // We don't need auth for the story router, so mount it earlier. + router.use("/story", createStoryRouter(app)); + // Setup Passport middleware. router.use(passport.initialize()); @@ -43,7 +46,6 @@ export function createAPIRouter(app: AppOptions, options: RouterOptions) { router.use("/auth", createNewAuthRouter(app, options)); router.use("/account", createNewAccountRouter(app, options)); router.use("/user", createNewUserRouter(app)); - router.use("/story", createStoryRouter(app)); // Configure the GraphQL route. router.use( diff --git a/src/core/server/app/router/api/install.ts b/src/core/server/app/router/api/install.ts index eb437619a..a270548b3 100644 --- a/src/core/server/app/router/api/install.ts +++ b/src/core/server/app/router/api/install.ts @@ -1,13 +1,15 @@ -import express, { Router } from "express"; +import { Router } from "express"; import { AppOptions } from "coral-server/app"; import { installCheckHandler, installHandler } from "coral-server/app/handlers"; import { jsonMiddleware } from "coral-server/app/middleware/json"; import { tenantMiddleware } from "coral-server/app/middleware/tenant"; +import { createAPIRouter } from "./helpers"; + export function createNewInstallRouter(app: AppOptions): Router { // Create a router. - const router = express.Router(); + const router = createAPIRouter(); router.get( "/", diff --git a/src/core/server/app/router/api/story.ts b/src/core/server/app/router/api/story.ts index a40ae7075..cdd6fd1c2 100644 --- a/src/core/server/app/router/api/story.ts +++ b/src/core/server/app/router/api/story.ts @@ -1,13 +1,13 @@ -import express from "express"; - import { AppOptions } from "coral-server/app"; import { countHandler } from "coral-server/app/handlers"; -import { cacheHeadersMiddleware } from "coral-server/app/middleware/cacheHeaders"; + +import { createAPIRouter } from "./helpers"; export function createStoryRouter(app: AppOptions) { - const router = express.Router(); - // TODO: (cvle) make caching time configurable? - router.get("/count.js", cacheHeadersMiddleware("2m"), countHandler(app)); + const router = createAPIRouter({ cache: "2m" }); + + router.get("/count.js", countHandler(app)); + return router; } diff --git a/src/core/server/app/router/api/user.ts b/src/core/server/app/router/api/user.ts index 837749b35..d954dd0e0 100644 --- a/src/core/server/app/router/api/user.ts +++ b/src/core/server/app/router/api/user.ts @@ -1,10 +1,10 @@ -import express from "express"; - import { AppOptions } from "coral-server/app"; import { userDownloadHandler } from "coral-server/app/handlers"; +import { createAPIRouter } from "./helpers"; + export function createNewUserRouter(app: AppOptions) { - const router = express.Router(); + const router = createAPIRouter(); router.get("/download", userDownloadHandler(app)); diff --git a/src/core/server/app/router/index.ts b/src/core/server/app/router/index.ts index 5e1ec0126..7696dd0a7 100644 --- a/src/core/server/app/router/index.ts +++ b/src/core/server/app/router/index.ts @@ -18,12 +18,7 @@ export function createRouter(app: AppOptions, options: RouterOptions) { const router = express.Router(); // Attach the API router. - router.use( - "/api", - noCacheMiddleware, - cookies(), - createAPIRouter(app, options) - ); + router.use("/api", cookies(), createAPIRouter(app, options)); // Attach the GraphiQL if enabled. if (app.config.get("enable_graphiql")) {