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