@@ -5,7 +5,7 @@ const path = require('path')
55
66const { Bridge } = require ( '@vercel/node/dist/bridge' )
77
8- const { downloadFile } = require ( './handlerUtils' )
8+ const { downloadFile, getMaxAge , getMultiValueHeaders } = require ( './handlerUtils' )
99
1010const makeHandler =
1111 ( ) =>
@@ -17,6 +17,10 @@ const makeHandler =
1717 // eslint-disable-next-line node/no-missing-require
1818 require . resolve ( './pages.js' )
1919 } catch { }
20+ // eslint-disable-next-line no-underscore-dangle
21+ process . env . _BYPASS_SSG = 'true'
22+
23+ const ONE_YEAR_IN_SECONDS = 31536000
2024
2125 // We don't want to write ISR files to disk in the lambda environment
2226 conf . experimental . isrFlushToDisk = false
@@ -106,6 +110,7 @@ const makeHandler =
106110 bridge . listen ( )
107111
108112 return async ( event , context ) => {
113+ let requestMode = mode
109114 // Ensure that paths are encoded - but don't double-encode them
110115 event . path = new URL ( event . path , event . rawUrl ) . pathname
111116 // Next expects to be able to parse the query from the URL
@@ -118,17 +123,12 @@ const makeHandler =
118123 base = `${ protocol } ://${ host } `
119124 }
120125 const { headers, ...result } = await bridge . launcher ( event , context )
126+
121127 /** @type import("@netlify/functions").HandlerResponse */
122128
123129 // Convert all headers to multiValueHeaders
124- const multiValueHeaders = { }
125- for ( const key of Object . keys ( headers ) ) {
126- if ( Array . isArray ( headers [ key ] ) ) {
127- multiValueHeaders [ key ] = headers [ key ]
128- } else {
129- multiValueHeaders [ key ] = [ headers [ key ] ]
130- }
131- }
130+
131+ const multiValueHeaders = getMultiValueHeaders ( headers )
132132
133133 if ( multiValueHeaders [ 'set-cookie' ] ?. [ 0 ] ?. includes ( '__prerender_bypass' ) ) {
134134 delete multiValueHeaders . etag
@@ -137,12 +137,20 @@ const makeHandler =
137137
138138 // Sending SWR headers causes undefined behaviour with the Netlify CDN
139139 const cacheHeader = multiValueHeaders [ 'cache-control' ] ?. [ 0 ]
140+
140141 if ( cacheHeader ?. includes ( 'stale-while-revalidate' ) ) {
141- console . log ( { cacheHeader } )
142+ if ( requestMode === 'odb' && process . env . EXPERIMENTAL_ODB_TTL ) {
143+ requestMode = 'isr'
144+ const ttl = getMaxAge ( cacheHeader )
145+ // Long-expiry TTL is basically no TTL
146+ if ( ttl > 0 && ttl < ONE_YEAR_IN_SECONDS ) {
147+ result . ttl = ttl
148+ }
149+ multiValueHeaders [ 'x-rendered-at' ] = [ new Date ( ) . toISOString ( ) ]
150+ }
142151 multiValueHeaders [ 'cache-control' ] = [ 'public, max-age=0, must-revalidate' ]
143152 }
144- multiValueHeaders [ 'x-render-mode' ] = [ mode ]
145-
153+ multiValueHeaders [ 'x-render-mode' ] = [ requestMode ]
146154 return {
147155 ...result ,
148156 multiValueHeaders,
@@ -157,7 +165,7 @@ const { tmpdir } = require('os')
157165const { promises, existsSync } = require("fs");
158166// We copy the file here rather than requiring from the node module
159167const { Bridge } = require("./bridge");
160- const { downloadFile } = require('./handlerUtils')
168+ const { downloadFile, getMaxAge, getMultiValueHeaders } = require('./handlerUtils')
161169
162170const { builder } = require("@netlify/functions");
163171const { config } = require("${ publishDir } /required-server-files.json")
0 commit comments