Unverified Commit 001e6cab authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update app dir cache handling (#46271

follow-up to https://github.com/vercel/next.js/pull/46081 gracefully
handles no endpoint being available.
parent 03025762
...@@ -6,7 +6,7 @@ let memoryCache: LRUCache<string, CacheHandlerValue> | undefined ...@@ -6,7 +6,7 @@ let memoryCache: LRUCache<string, CacheHandlerValue> | undefined
export default class FetchCache implements CacheHandler { export default class FetchCache implements CacheHandler {
private headers: Record<string, string> private headers: Record<string, string>
private cacheEndpoint: string private cacheEndpoint?: string
private debug: boolean private debug: boolean
constructor(ctx: CacheHandlerContext) { constructor(ctx: CacheHandlerContext) {
...@@ -44,12 +44,16 @@ export default class FetchCache implements CacheHandler { ...@@ -44,12 +44,16 @@ export default class FetchCache implements CacheHandler {
this.headers[k] = newHeaders[k] this.headers[k] = newHeaders[k]
} }
} }
if (ctx._requestHeaders['x-vercel-sc-host']) {
this.cacheEndpoint = `https://${ctx._requestHeaders['x-vercel-sc-host']}${ this.cacheEndpoint = `https://${ctx._requestHeaders['x-vercel-sc-host']}${
ctx._requestHeaders['x-vercel-sc-basepath'] || '' ctx._requestHeaders['x-vercel-sc-basepath'] || ''
}` }`
if (this.debug) { if (this.debug) {
console.log('using cache endpoint', this.cacheEndpoint) console.log('using cache endpoint', this.cacheEndpoint)
} }
} else if (this.debug) {
console.log('no cache endpoint available')
}
} }
public async get(key: string, fetchCache?: boolean) { public async get(key: string, fetchCache?: boolean) {
...@@ -58,7 +62,7 @@ export default class FetchCache implements CacheHandler { ...@@ -58,7 +62,7 @@ export default class FetchCache implements CacheHandler {
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
// get data from fetch cache // get data from fetch cache
if (!data) { if (!data && this.cacheEndpoint) {
try { try {
const start = Date.now() const start = Date.now()
const res = await fetch( const res = await fetch(
...@@ -133,6 +137,7 @@ export default class FetchCache implements CacheHandler { ...@@ -133,6 +137,7 @@ export default class FetchCache implements CacheHandler {
lastModified: Date.now(), lastModified: Date.now(),
}) })
if (this.cacheEndpoint) {
try { try {
const start = Date.now() const start = Date.now()
if (data !== null && 'revalidate' in data) { if (data !== null && 'revalidate' in data) {
...@@ -170,6 +175,7 @@ export default class FetchCache implements CacheHandler { ...@@ -170,6 +175,7 @@ export default class FetchCache implements CacheHandler {
console.error(`Failed to update fetch cache`, err) console.error(`Failed to update fetch cache`, err)
} }
} }
}
return return
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment