@@ -5,7 +5,7 @@ const path = require('path')
5
5
6
6
const { Bridge } = require ( '@vercel/node/dist/bridge' )
7
7
8
- const { downloadFile } = require ( './handlerUtils' )
8
+ const { downloadFile, getMaxAge , getMultiValueHeaders } = require ( './handlerUtils' )
9
9
10
10
const makeHandler =
11
11
( ) =>
@@ -17,6 +17,10 @@ const makeHandler =
17
17
// eslint-disable-next-line node/no-missing-require
18
18
require . resolve ( './pages.js' )
19
19
} catch { }
20
+ // eslint-disable-next-line no-underscore-dangle
21
+ process . env . _BYPASS_SSG = 'true'
22
+
23
+ const ONE_YEAR_IN_SECONDS = 31536000
20
24
21
25
// We don't want to write ISR files to disk in the lambda environment
22
26
conf . experimental . isrFlushToDisk = false
@@ -106,6 +110,7 @@ const makeHandler =
106
110
bridge . listen ( )
107
111
108
112
return async ( event , context ) => {
113
+ let requestMode = mode
109
114
// Ensure that paths are encoded - but don't double-encode them
110
115
event . path = new URL ( event . path , event . rawUrl ) . pathname
111
116
// Next expects to be able to parse the query from the URL
@@ -118,17 +123,12 @@ const makeHandler =
118
123
base = `${ protocol } ://${ host } `
119
124
}
120
125
const { headers, ...result } = await bridge . launcher ( event , context )
126
+
121
127
/** @type import("@netlify/functions").HandlerResponse */
122
128
123
129
// 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 )
132
132
133
133
if ( multiValueHeaders [ 'set-cookie' ] ?. [ 0 ] ?. includes ( '__prerender_bypass' ) ) {
134
134
delete multiValueHeaders . etag
@@ -137,12 +137,20 @@ const makeHandler =
137
137
138
138
// Sending SWR headers causes undefined behaviour with the Netlify CDN
139
139
const cacheHeader = multiValueHeaders [ 'cache-control' ] ?. [ 0 ]
140
+
140
141
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
+ }
142
151
multiValueHeaders [ 'cache-control' ] = [ 'public, max-age=0, must-revalidate' ]
143
152
}
144
- multiValueHeaders [ 'x-render-mode' ] = [ mode ]
145
-
153
+ multiValueHeaders [ 'x-render-mode' ] = [ requestMode ]
146
154
return {
147
155
...result ,
148
156
multiValueHeaders,
@@ -157,7 +165,7 @@ const { tmpdir } = require('os')
157
165
const { promises, existsSync } = require("fs");
158
166
// We copy the file here rather than requiring from the node module
159
167
const { Bridge } = require("./bridge");
160
- const { downloadFile } = require('./handlerUtils')
168
+ const { downloadFile, getMaxAge, getMultiValueHeaders } = require('./handlerUtils')
161
169
162
170
const { builder } = require("@netlify/functions");
163
171
const { config } = require("${ publishDir } /required-server-files.json")
0 commit comments