update id

This commit is contained in:
2025-11-07 16:09:01 -03:00
parent c8b4d9beeb
commit 4c50692cd9
27 changed files with 210 additions and 517 deletions

View File

@@ -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 (dont 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
}