Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update app dir cache handling (#46271 #46271

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let memoryCache: LRUCache<string, CacheHandlerValue> | undefined

export default class FetchCache implements CacheHandler {
private headers: Record<string, string>
private cacheEndpoint: string
private cacheEndpoint?: string
private debug: boolean

constructor(ctx: CacheHandlerContext) {
Expand Down Expand Up @@ -44,11 +44,15 @@ export default class FetchCache implements CacheHandler {
this.headers[k] = newHeaders[k]
}
}
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 (ctx._requestHeaders['x-vercel-sc-host']) {
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)
}
} else if (this.debug) {
console.log('no cache endpoint available')
}
}

Expand All @@ -58,7 +62,7 @@ export default class FetchCache implements CacheHandler {
let data = memoryCache?.get(key)

// get data from fetch cache
if (!data) {
if (!data && this.cacheEndpoint) {
try {
const start = Date.now()
const res = await fetch(
Expand Down Expand Up @@ -133,41 +137,43 @@ export default class FetchCache implements CacheHandler {
lastModified: Date.now(),
})

try {
const start = Date.now()
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 (this.cacheEndpoint) {
try {
const start = Date.now()
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 (!res.ok) {
this.debug && console.log(await res.text())
throw new Error(`invalid response ${res.status}`)
}
if (!res.ok) {
this.debug && console.log(await res.text())
throw new Error(`invalid response ${res.status}`)
}

if (this.debug) {
console.log(
`successfully set to fetch-cache for ${key}, duration: ${
Date.now() - start
}ms, size: ${body.length}`
)
}
} catch (err) {
// unable to set to fetch-cache
if (this.debug) {
console.error(`Failed to update fetch cache`, err)
if (this.debug) {
console.log(
`successfully set to fetch-cache for ${key}, duration: ${
Date.now() - start
}ms, size: ${body.length}`
)
}
} catch (err) {
// unable to set to fetch-cache
if (this.debug) {
console.error(`Failed to update fetch cache`, err)
}
}
}
return
Expand Down