diff --git a/.changeset/two-mirrors-move.md b/.changeset/two-mirrors-move.md new file mode 100644 index 000000000..b6098021d --- /dev/null +++ b/.changeset/two-mirrors-move.md @@ -0,0 +1,5 @@ +--- +'gitbook': patch +--- + +Fix server actions cache compromised. Leading to some bugs on frontend. diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index c7a99f4f9..84df0da31 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -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);