diff --git a/src/backend/service/webring.ts b/src/backend/service/webring.ts index 55537c0..358fbc5 100644 --- a/src/backend/service/webring.ts +++ b/src/backend/service/webring.ts @@ -5,6 +5,7 @@ import { ffetch } from '../utils/fetch.ts' const WEBRING_URL = 'https://otomir23.me/webring/5/data' const WEBRING_TTL = 1000 * 60 * 60 * 24 // 24 hours +const STALE_TTL = 1000 * 60 * 60 * 8 // 8 hours const WebringItem = z.object({ id: z.number(), @@ -29,4 +30,5 @@ export const webring = new AsyncResource({ } }, swr: true, + swrValidator: ({ currentFetchedAt }) => Date.now() - currentFetchedAt < STALE_TTL, }) diff --git a/src/backend/utils/fetch.ts b/src/backend/utils/fetch.ts index 99cc910..57d63ff 100644 --- a/src/backend/utils/fetch.ts +++ b/src/backend/utils/fetch.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ import { ffetchAddons, ffetchBase } from '@fuman/fetch' import { ffetchZodAdapter } from '@fuman/fetch/zod' @@ -6,6 +7,17 @@ export const ffetch = ffetchBase.extend({ ffetchAddons.parser(ffetchZodAdapter()), ffetchAddons.retry(), ], + middlewares: [ + async (req, next) => { + console.log('[%s] fetch(%s %s)', new Date().toISOString(), req.method, req.url) + const start = Date.now() + try { + return await next(req) + } finally { + console.log('[%s] fetch(%s %s) done in %sms', new Date().toISOString(), req.method, req.url, Date.now() - start) + } + }, + ], headers: { 'User-Agent': 'tei.su/1.0', },