Files
saladeaula.digital/apps/id.saladeaula.digital/app/routes/upstream.ts
2025-11-07 16:09:01 -03:00

63 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Route } from './+types'
export const loader = proxy
export const action = proxy
const maxAge = 3600 * 8 // 8 hours
async function proxy({
request,
context
}: Route.ActionArgs): Promise<Response> {
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,
...(['GET', 'HEAD'].includes(request.method)
? {}
: { body: await request.text() })
})
if (shouldCache && response.ok) {
const headers_ = new Headers(response.headers)
headers_.set('Cache-Control', `public, max-age=${maxAge}`)
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
}