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,11 +44,15 @@ export default class FetchCache implements CacheHandler { ...@@ -44,11 +44,15 @@ export default class FetchCache implements CacheHandler {
this.headers[k] = newHeaders[k] this.headers[k] = newHeaders[k]
} }
} }
this.cacheEndpoint = `https://${ctx._requestHeaders['x-vercel-sc-host']}${ if (ctx._requestHeaders['x-vercel-sc-host']) {
ctx._requestHeaders['x-vercel-sc-basepath'] || '' this.cacheEndpoint = `https://${ctx._requestHeaders['x-vercel-sc-host']}${
}` ctx._requestHeaders['x-vercel-sc-basepath'] || ''
if (this.debug) { }`
console.log('using cache endpoint', this.cacheEndpoint) if (this.debug) {
console.log('using cache endpoint', this.cacheEndpoint)
}
} else if (this.debug) {
console.log('no cache endpoint available')
} }
} }
...@@ -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,41 +137,43 @@ export default class FetchCache implements CacheHandler { ...@@ -133,41 +137,43 @@ export default class FetchCache implements CacheHandler {
lastModified: Date.now(), lastModified: Date.now(),
}) })
try { if (this.cacheEndpoint) {
const start = Date.now() try {
if (data !== null && 'revalidate' in data) { const start = Date.now()
this.headers['x-vercel-revalidate'] = data.revalidate.toString() if (data !== null && 'revalidate' in data) {
} this.headers['x-vercel-revalidate'] = data.revalidate.toString()
if (data !== null && 'data' in data) {
this.headers['x-vercel-cache-control'] =
data.data.headers['cache-control']
}
const body = JSON.stringify(data)
const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/${key}`,
{
method: 'POST',
headers: this.headers,
body: body,
} }
) if (data !== null && 'data' in data) {
this.headers['x-vercel-cache-control'] =
data.data.headers['cache-control']
}
const body = JSON.stringify(data)
const res = await fetch(
`${this.cacheEndpoint}/v1/suspense-cache/${key}`,
{
method: 'POST',
headers: this.headers,
body: body,
}
)
if (!res.ok) { if (!res.ok) {
this.debug && console.log(await res.text()) this.debug && console.log(await res.text())
throw new Error(`invalid response ${res.status}`) throw new Error(`invalid response ${res.status}`)
} }
if (this.debug) { if (this.debug) {
console.log( console.log(
`successfully set to fetch-cache for ${key}, duration: ${ `successfully set to fetch-cache for ${key}, duration: ${
Date.now() - start Date.now() - start
}ms, size: ${body.length}` }ms, size: ${body.length}`
) )
} }
} catch (err) { } catch (err) {
// unable to set to fetch-cache // unable to set to fetch-cache
if (this.debug) { if (this.debug) {
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