Files
documenso/packages/lib/server-only/http/with-swr.ts
Adithya Krishna 2336e70495 chore: fix linting issues
Signed-off-by: Adithya Krishna <aadithya794@gmail.com>
2024-05-09 12:35:58 +05:30

29 lines
709 B
TypeScript

import type { NextApiResponse } from 'next';
import type { NextResponse } from 'next/server';
type NarrowedResponse<T> = T extends NextResponse
? NextResponse
: T extends NextApiResponse<infer U>
? NextApiResponse<U>
: never;
export const withStaleWhileRevalidate = <T>(
res: NarrowedResponse<T>,
cacheInSeconds = 60,
staleCacheInSeconds = 300,
) => {
if ('headers' in res) {
res.headers.set(
'Cache-Control',
`public, s-maxage=${cacheInSeconds}, stale-while-revalidate=${staleCacheInSeconds}`,
);
} else {
res.setHeader(
'Cache-Control',
`public, s-maxage=${cacheInSeconds}, stale-while-revalidate=${staleCacheInSeconds}`,
);
}
return res;
};