mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
29 lines
709 B
TypeScript
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;
|
|
};
|