update id
This commit is contained in:
@@ -3,6 +3,8 @@ import type { Route } from './+types'
|
||||
export const loader = proxy
|
||||
export const action = proxy
|
||||
|
||||
const maxAge = 3600 * 8 // 8 hours
|
||||
|
||||
async function proxy({
|
||||
request,
|
||||
context
|
||||
@@ -10,6 +12,20 @@ async function proxy({
|
||||
const pathname = new URL(request.url).pathname
|
||||
const url = new URL(pathname, context.cloudflare.env.ISSUER_URL)
|
||||
const headers = new Headers(request.headers)
|
||||
|
||||
const shouldCache =
|
||||
request.method === 'GET' && pathname.startsWith('/.well-known/')
|
||||
|
||||
const cache = caches.default
|
||||
const cacheKey = new Request(url.toString(), request)
|
||||
|
||||
if (shouldCache) {
|
||||
const cached = await cache.match(cacheKey)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: request.method,
|
||||
headers,
|
||||
@@ -18,14 +34,29 @@ async function proxy({
|
||||
: { body: await request.text() })
|
||||
})
|
||||
|
||||
const contentType = response.headers.get('content-type') || ''
|
||||
const body =
|
||||
contentType.includes('application/json') || contentType.startsWith('text/')
|
||||
? await response.text()
|
||||
: await response.arrayBuffer()
|
||||
if (shouldCache && response.ok) {
|
||||
const headers_ = new Headers(response.headers)
|
||||
headers_.set('Cache-Control', `public, max-age=${maxAge}`)
|
||||
|
||||
return new Response(body, {
|
||||
status: response.status,
|
||||
headers: response.headers
|
||||
})
|
||||
const cacheResponse = new Response(response.clone().body, {
|
||||
status: response.status,
|
||||
headers: headers_
|
||||
})
|
||||
|
||||
// Store asynchronously (don’t block response)
|
||||
context.cloudflare.ctx.waitUntil(cache.put(cacheKey, cacheResponse))
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || ''
|
||||
if (
|
||||
contentType.includes('application/json') ||
|
||||
contentType.startsWith('text/')
|
||||
) {
|
||||
return new Response(await response.text(), {
|
||||
status: response.status,
|
||||
headers: response.headers
|
||||
})
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user