@@ -139,38 +139,39 @@ function authorizedHeaders(
139
139
: headers ;
140
140
}
141
141
142
- function isCloudflareWorkerRuntime ( ) {
142
+ // NextJS 13 and 14 cache fetch requests by default.
143
+ //
144
+ // Since adminDB.query uses fetch, this means that it would also cache by default.
145
+ //
146
+ // We don't want this behavior. `adminDB.query` should return the latest result by default.
147
+ //
148
+ // To get around this, we set an explicit `cache` header for NextJS 13 and 14.
149
+ // This is no longer needed in NextJS 15 onwards, as the default is `no-store` again.
150
+ // Once NextJS 13 and 14 are no longer common, we can remove this code.
151
+ function isNextJSVersionThatCachesFetchByDefault ( ) {
143
152
return (
144
- // @ts -ignore
145
- typeof WebSocketPair !== 'undefined' ||
146
- // @ts -ignore
147
- ( typeof navigator !== 'undefined' &&
148
- navigator . userAgent === 'Cloudflare-Workers' ) ||
149
- // @ts -ignore
150
- ( typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel' )
153
+ // NextJS 13 onwards added a `__nextPatched` property to the fetch function
154
+ fetch [ '__nextPatched' ] &&
155
+ // NextJS 15 onwards _also_ added a global `next-patch` symbol.
156
+ ! globalThis [ Symbol . for ( 'next-patch' ) ]
151
157
) ;
152
158
}
153
159
154
- // (XXX): Cloudflare Workers don't support cache: "no-store"
155
- // We need to set `cache: "no-store"` so Next.js doesn't cache the fetch
156
- // To keep Cloudflare Workers working, we need to conditionally set the fetch options
157
- // Once Cloudflare Workers support `cache: "no-store"`, we can remove this conditional
158
- // https://github.com/cloudflare/workerd/issues/698
159
-
160
- const FETCH_OPTS : RequestInit = isCloudflareWorkerRuntime ( )
161
- ? { }
162
- : { cache : 'no-store' } ;
160
+ function getDefaultFetchOpts ( ) : RequestInit {
161
+ return isNextJSVersionThatCachesFetchByDefault ( ) ? { cache : 'no-store' } : { } ;
162
+ }
163
163
164
164
async function jsonFetch (
165
165
input : RequestInfo ,
166
166
init : RequestInit | undefined ,
167
167
) : Promise < any > {
168
+ const defaultFetchOpts = getDefaultFetchOpts ( ) ;
168
169
const headers = {
169
170
...( init . headers || { } ) ,
170
171
'Instant-Admin-Version' : version ,
171
172
'Instant-Core-Version' : coreVersion ,
172
173
} ;
173
- const res = await fetch ( input , { ...FETCH_OPTS , ...init , headers } ) ;
174
+ const res = await fetch ( input , { ...defaultFetchOpts , ...init , headers } ) ;
174
175
const json = await res . json ( ) ;
175
176
return res . status === 200
176
177
? Promise . resolve ( json )
0 commit comments