From 411f82a3c011c7dfd469ea6ec2f3b132a87d510b Mon Sep 17 00:00:00 2001 From: hotwater Date: Sat, 4 May 2024 15:22:51 +0300 Subject: [PATCH] feat: add `number` type to `refreshing` --- src/frog-base.tsx | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/frog-base.tsx b/src/frog-base.tsx index e5255837..1e335f00 100644 --- a/src/frog-base.tsx +++ b/src/frog-base.tsx @@ -184,8 +184,10 @@ export type RouteOptions = Pick< initial?: Omit & ( | { - refreshing: true - image: () => FrameResponse['image'] + refreshing: true | number + image: () => + | Promise + | FrameResponse['image'] } | { refreshing?: false | undefined @@ -434,7 +436,7 @@ export class FrogBase< `Image handler cannot return an image at ${c.req.path} if there are no query parameters and if \`initial\` property is not set. Either add query parameters, or add \`initial\` property in the third argument of this frame handler.`, ) - const image_ = await (initial.refreshing + const image_ = await (typeof initial.image === 'function' ? initial.image() : initial.image) @@ -452,11 +454,21 @@ export class FrogBase< const headers_ = new Headers(defaultHeaders).toJSON() if (headers_['Cache-Control']) return headers_ - headers_['Cache-Control'] = 'max-age=0' + headers_['Cache-Control'] = `max-age=${ + typeof initial.refreshing === 'number' + ? initial.refreshing + : 0 + }` return headers_ })() : (() => { - return { 'Cache-Control': 'max-age=0' } + return { + 'Cache-Control': `max-age=${ + typeof initial.refreshing === 'number' + ? initial.refreshing + : 0 + }`, + } })() : defaultHeaders