import { MetaTagInjector } from '../components/metaTagInjector'
import Cache from './cache'
import { Data } from './cache'
export async function getMetadataRequest(
res: Promise<Response>,
url: string,
getData: () => Promise<Data | undefined>
) {
try {
const cachedData = await getRequest(url, getData, (data): data is Data => true)
if (cachedData) {
return new HTMLRewriter().on('head', new MetaTagInjector(cachedData)).transform(await res)
} else {
return res
}
} catch (e) {
export async function getRequest<T extends Data>(
getData: () => Promise<T | undefined>,
validateData: (data: Data) => data is T
): Promise<T | undefined> {
const cachedData = await Cache.match(url)
if (cachedData && validateData(cachedData)) {
return cachedData
const data = await getData()
if (!data) {
return undefined
await Cache.put(data, url)
return data