UNPKG

@netlify/next-runtime

Version:
23 lines (22 loc) 919 B
import { purgeCache } from '@netlify/functions'; // Needing to proxy the response object to intercept the revalidate call for on-demand revalidation on page routes export const nextResponseProxy = (res) => { return new Proxy(res, { get(target, key) { const originalValue = target[key]; if (key === 'revalidate') { return async function newRevalidate(...args) { try { console.debug('Purging cache for:', [args[0]]); await purgeCache({ tags: [args[0]] }); } catch (err) { throw new Error(`An internal error occurred while trying to purge cache for ${args[0]}}`); } return originalValue?.apply(target, args); }; } return originalValue; }, }); };