@@ -4,6 +4,7 @@ import path from 'path'
4
4
import { pipeline } from 'stream'
5
5
import { promisify } from 'util'
6
6
7
+ import { HandlerEvent , HandlerResponse } from '@netlify/functions'
7
8
import { http , https } from 'follow-redirects'
8
9
import type NextNodeServer from 'next/dist/server/next-server'
9
10
@@ -105,8 +106,8 @@ export const augmentFsModule = ({
105
106
const statsOrig = promises . stat
106
107
// ...then money-patch it to see if it's requesting a CDN file
107
108
promises . readFile = ( async ( file , options ) => {
108
- // In production use the public URL (e.g. https://example.com). Otherwise use the deploy URL, e.g. https://deploy-preview-123--example.netlify.app
109
- const baseUrl = process . env . CONTEXT === 'production' ? process . env . URL : process . env . DEPLOY_PRIME_URL
109
+ // In production or dev use the public URL (e.g. https://example.com). Otherwise use the deploy URL, e.g. https://deploy-preview-123--example.netlify.app
110
+ const baseUrl = [ 'production' , 'dev' ] . includes ( process . env . CONTEXT ) ? process . env . URL : process . env . DEPLOY_PRIME_URL
110
111
111
112
// We only care about page files
112
113
if ( file . startsWith ( pageRoot ) ) {
@@ -188,3 +189,19 @@ export const getNextServer = (): NextServerType => {
188
189
}
189
190
return NextServer
190
191
}
192
+ /**
193
+ * Prefetch requests are used to check for middleware redirects, and shouldn't trigger SSR.
194
+ */
195
+ export const getPrefetchResponse = ( event : HandlerEvent , mode : string ) : HandlerResponse | false => {
196
+ if ( event . headers [ 'x-middleware-prefetch' ] && mode === 'ssr' ) {
197
+ return {
198
+ statusCode : 200 ,
199
+ body : '{}' ,
200
+ headers : {
201
+ 'Content-Type' : 'application/json' ,
202
+ 'x-middleware-skip' : '1' ,
203
+ } ,
204
+ }
205
+ }
206
+ return false
207
+ }
0 commit comments