Separate cache for HTTP methods (#2720)

This commit is contained in:
Greg Bergé
2025-01-10 16:09:35 +01:00
committed by GitHub
parent 6059efe094
commit a6f65916f1
2 changed files with 13 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'gitbook': patch
---
Fix server actions cache compromised. Leading to some bugs on frontend.
+8 -1
View File
@@ -290,6 +290,9 @@ export async function middleware(request: NextRequest) {
resolved.cookies,
);
// Add method so Cloudflare can use it for caching
setMiddlewareHeader(response, 'x-http-method', request.method);
setMiddlewareHeader(response, 'x-gitbook-version', buildVersion());
// Add Content Security Policy header
@@ -304,7 +307,11 @@ export async function middleware(request: NextRequest) {
// When the request is authenticated, we don't want to cache the response on the server
!resolved.visitorToken
) {
const cacheControl = `public, max-age=0, s-maxage=${resolved.cacheMaxAge}, stale-if-error=0`;
// For server-actions, we don't want to cache the response on the server
const cacheControl =
request.method === 'POST'
? 'no-store'
: `public, max-age=0, s-maxage=${resolved.cacheMaxAge}, stale-if-error=0`;
if (process.env.GITBOOK_OUTPUT_CACHE === 'true' && process.env.NODE_ENV !== 'development') {
setMiddlewareHeader(response, 'cache-control', cacheControl);