Skip to content

Commit b03e35a

Browse files
committed
fix: handle prefetch correctly
1 parent 13d3c73 commit b03e35a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/runtime/src/templates/getHandler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { outdent as javascript } from 'outdent'
55

66
import type { NextConfig } from '../helpers/config'
77

8-
import type { NextServerType } from './handlerUtils'
8+
import { getPrefetchResponse, NextServerType } from './handlerUtils'
99

1010
/* eslint-disable @typescript-eslint/no-var-requires */
1111

@@ -90,6 +90,10 @@ const makeHandler = (conf: NextConfig, app, pageRoot, staticManifest: Array<[str
9090

9191
return async function handler(event: HandlerEvent, context: HandlerContext) {
9292
let requestMode = mode
93+
const prefetchResponse = getPrefetchResponse(event, mode)
94+
if (prefetchResponse) {
95+
return prefetchResponse
96+
}
9397
// Ensure that paths are encoded - but don't double-encode them
9498
event.path = new URL(event.rawUrl).pathname
9599
// Next expects to be able to parse the query from the URL

packages/runtime/src/templates/handlerUtils.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path'
44
import { pipeline } from 'stream'
55
import { promisify } from 'util'
66

7+
import { HandlerEvent, HandlerResponse } from '@netlify/functions'
78
import { http, https } from 'follow-redirects'
89
import type NextNodeServer from 'next/dist/server/next-server'
910

@@ -105,8 +106,8 @@ export const augmentFsModule = ({
105106
const statsOrig = promises.stat
106107
// ...then money-patch it to see if it's requesting a CDN file
107108
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
110111

111112
// We only care about page files
112113
if (file.startsWith(pageRoot)) {
@@ -188,3 +189,19 @@ export const getNextServer = (): NextServerType => {
188189
}
189190
return NextServer
190191
}
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

Comments
 (0)