@@ -20,7 +20,7 @@ import {
2020import { race } from '@/lib/async' ;
2121import { buildVersion } from '@/lib/build' ;
2222import { createContentSecurityPolicyNonce , getContentSecurityPolicy } from '@/lib/csp' ;
23- import { getURLLookupAlternatives , normalizeURL } from '@/lib/middleware' ;
23+ import { getURLLookupAlternatives , normalizeURL , setMiddlewareHeader } from '@/lib/middleware' ;
2424import {
2525 VisitorAuthCookieValue ,
2626 getVisitorAuthCookieName ,
@@ -253,43 +253,31 @@ export async function middleware(request: NextRequest) {
253253 resolved . cookies ,
254254 ) ;
255255
256- response . headers . set ( 'x-gitbook-version' , buildVersion ( ) ) ;
256+ setMiddlewareHeader ( response , 'x-gitbook-version' , buildVersion ( ) ) ;
257257
258258 // Add Content Security Policy header
259- response . headers . set ( 'content-security-policy' , csp ) ;
259+ setMiddlewareHeader ( response , 'content-security-policy' , csp ) ;
260260 // Basic security headers
261- response . headers . set ( 'strict-transport-security' , 'max-age=31536000' ) ;
262- response . headers . set ( 'referrer-policy' , 'no-referrer-when-downgrade' ) ;
263- response . headers . set ( 'x-content-type-options' , 'nosniff' ) ;
264-
265- const isPrefetch = request . headers . has ( 'x-middleware-prefetch' ) ;
266-
267- if ( isPrefetch ) {
268- // To avoid cache poisoning, we don't cache prefetch requests
269- response . headers . set (
270- 'cache-control' ,
271- 'private, no-cache, no-store, max-age=0, must-revalidate' ,
272- ) ;
273- } else {
274- if ( typeof resolved . cacheMaxAge === 'number' ) {
275- const cacheControl = `public, max-age=0, s-maxage=${ resolved . cacheMaxAge } , stale-if-error=0` ;
276-
277- if (
278- process . env . GITBOOK_OUTPUT_CACHE === 'true' &&
279- process . env . NODE_ENV !== 'development'
280- ) {
281- response . headers . set ( 'cache-control' , cacheControl ) ;
282- response . headers . set ( 'Cloudflare-CDN-Cache-Control' , cacheControl ) ;
283- } else {
284- response . headers . set ( 'x-gitbook-cache-control' , cacheControl ) ;
285- }
261+ setMiddlewareHeader ( response , 'strict-transport-security' , 'max-age=31536000' ) ;
262+ setMiddlewareHeader ( response , 'referrer-policy' , 'no-referrer-when-downgrade' ) ;
263+ setMiddlewareHeader ( response , 'x-content-type-options' , 'nosniff' ) ;
264+
265+ if ( typeof resolved . cacheMaxAge === 'number' ) {
266+ const cacheControl = `public, max-age=0, s-maxage=${ resolved . cacheMaxAge } , stale-if-error=0` ;
267+
268+ if ( process . env . GITBOOK_OUTPUT_CACHE === 'true' && process . env . NODE_ENV !== 'development' ) {
269+ setMiddlewareHeader ( response , 'cache-control' , cacheControl ) ;
270+ setMiddlewareHeader ( response , 'Cloudflare-CDN-Cache-Control' , cacheControl ) ;
271+ } else {
272+ setMiddlewareHeader ( response , 'x-gitbook-cache-control' , cacheControl ) ;
286273 }
274+ }
275+ // }
287276
288- if ( resolved . cacheTags && resolved . cacheTags . length > 0 ) {
289- const headerCacheTag = resolved . cacheTags . join ( ',' ) ;
290- response . headers . set ( 'cache-tag' , headerCacheTag ) ;
291- response . headers . set ( 'x-gitbook-cache-tag' , headerCacheTag ) ;
292- }
277+ if ( resolved . cacheTags && resolved . cacheTags . length > 0 ) {
278+ const headerCacheTag = resolved . cacheTags . join ( ',' ) ;
279+ setMiddlewareHeader ( response , 'cache-tag' , headerCacheTag ) ;
280+ setMiddlewareHeader ( response , 'x-gitbook-cache-tag' , headerCacheTag ) ;
293281 }
294282
295283 return response ;
0 commit comments