fix: addressed issues with cache headers (#2873)

Co-authored-by: Kim Gardner <kgardnr@gmail.com>
This commit is contained in:
Wyatt Johnson
2020-03-10 16:46:44 -04:00
committed by GitHub
co-authored by Kim Gardner
parent 6f1014289b
commit 7abd703f72
8 changed files with 39 additions and 21 deletions
+3 -2
View File
@@ -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<RouterOptions, "passport">
) {
const router = express.Router();
const router = createAPIRouter();
router.post(
"/confirm",
+3 -1
View File
@@ -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<RouterOptions, "passport">,
@@ -36,7 +38,7 @@ export function createNewAuthRouter(
app: AppOptions,
{ passport }: Pick<RouterOptions, "passport">
) {
const router = express.Router();
const router = createAPIRouter();
// Mount the Local Authentication handlers.
router.post(
+16
View File
@@ -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;
}
+3 -1
View File
@@ -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(
+4 -2
View File
@@ -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(
"/",
+6 -6
View File
@@ -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;
}
+3 -3
View File
@@ -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));
+1 -6
View File
@@ -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")) {