@@ -2,17 +2,15 @@ import express from 'express'
22import libLanguages from '../lib/languages.js'
33import searchVersions from '../lib/search/versions.js'
44import loadLunrResults from '../lib/search/lunr-search.js'
5+ import { cacheControlFactory } from './cache-control.js'
6+
57const languages = new Set ( Object . keys ( libLanguages ) )
68const versions = new Set ( Object . values ( searchVersions ) )
7-
89const router = express . Router ( )
10+ const cacheControl = cacheControlFactory ( 60 * 60 * 24 )
11+ const noCacheControl = cacheControlFactory ( 0 )
912
10- router . get ( '/' , async function postSearch ( req , res , next ) {
11- res . set ( {
12- 'surrogate-control' : 'private, no-store' ,
13- 'cache-control' : 'private, no-store' ,
14- } )
15-
13+ router . get ( '/' , async function getSearch ( req , res , next ) {
1614 const { query, version, language, filters, limit : limit_ } = req . query
1715 const limit = Math . min ( parseInt ( limit_ , 10 ) || 10 , 100 )
1816 if ( ! versions . has ( version ) || ! languages . has ( language ) ) {
@@ -31,12 +29,19 @@ router.get('/', async function postSearch(req, res, next) {
3129 } )
3230 // Only reply if the headers have not been sent and the request was not aborted...
3331 if ( ! res . headersSent && ! req . aborted ) {
32+ cacheControl ( res )
33+
34+ // Undo the cookie setting that CSRF sets.
35+ // Otherwise it can't be cached in the CDN.
36+ res . removeHeader ( 'set-cookie' )
37+
3438 return res . status ( 200 ) . json ( results )
3539 }
3640 } catch ( err ) {
3741 console . error ( err )
3842 // Only reply if the headers have not been sent and the request was not aborted...
3943 if ( ! res . headersSent && ! req . aborted ) {
44+ noCacheControl ( res )
4045 return res . status ( 400 ) . json ( { error : err . toString ( ) } )
4146 }
4247 }
0 commit comments