From bacb22ba22df4dfeab928aa75c89d7a7bf7cf190 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Sun, 17 Nov 2024 20:20:04 -0500 Subject: [PATCH] Don't store cached responses by default This prevents us from showing stale pages. For example, if the user logs out and then clicks the "back" button, they should not see a page that looks like they are still logged in. --- server.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/server.ts b/server.ts index eecd6435b..efe4d2859 100644 --- a/server.ts +++ b/server.ts @@ -1,6 +1,7 @@ import { type RequestHandler, createRequestHandler } from '@remix-run/architect' import * as build from '@remix-run/dev/server-build' import { installGlobals } from '@remix-run/node' +import { type APIGatewayProxyStructuredResultV2 } from 'aws-lambda' import sourceMapSupport from 'source-map-support' sourceMapSupport.install() @@ -13,10 +14,20 @@ const remixHandler = createRequestHandler({ const { CDN_SECRET } = process.env -export const handler: RequestHandler = (event, ...args) => { +export const handler: RequestHandler = async (event, ...args) => { if (CDN_SECRET && event.headers['x-cdn-secret'] !== CDN_SECRET) // FIXME: this shouldn't need to be a promise. // See https://github.com/DefinitelyTyped/DefinitelyTyped/pull/69466 return Promise.resolve({ statusCode: 502 }) - return remixHandler(event, ...args) + + // By default, don't cache responses. + // This prevents us from showing stale pages. For example, if the user + // logs out and then clicks the "back" button, they should not see a page + // that looks like they are still logged in. + const result = (await remixHandler( + event, + ...args + )) as APIGatewayProxyStructuredResultV2 + ;(result.headers ??= {})['Cache-Control'] ??= 'max-age=0, no-store' + return result }