-
Notifications
You must be signed in to change notification settings - Fork 89
feat: make CDN SWR background revalidation discard stale cache content in order to produce fresh responses #2765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ng background SWR requests
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { purgeCache, Config } from '@netlify/functions' | ||
|
||
export default async function handler(request: Request) { | ||
const url = new URL(request.url) | ||
const pathToPurge = url.searchParams.get('path') | ||
|
||
if (!pathToPurge) { | ||
return Response.json( | ||
{ | ||
status: 'error', | ||
error: 'missing "path" query parameter', | ||
}, | ||
{ status: 400 }, | ||
) | ||
} | ||
try { | ||
await purgeCache({ tags: [`_N_T_${encodeURI(pathToPurge)}`] }) | ||
return Response.json( | ||
{ | ||
status: 'ok', | ||
}, | ||
{ | ||
status: 200, | ||
}, | ||
) | ||
} catch (error) { | ||
return Response.json( | ||
{ | ||
status: 'error', | ||
error: error.toString(), | ||
}, | ||
{ | ||
status: 500, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
export const config: Config = { | ||
path: '/api/purge-cdn', | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had some troubles with this function being in Next.js api handler - in the end I don't rely on it for added test, but overall it doesn't really make sense to use Next.js API for it, if it can use just vanilla Netlify function, as it only purge cdn cache, and not any Next.js caches in blobs |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const Show = ({ time, easyTimeToCompare, slug }) => ( | ||
<div> | ||
<p> | ||
This page uses getStaticProps() at | ||
<span data-testid="date-now">{time}</span> | ||
</p> | ||
<p> | ||
Time string: <span data-testid="date-easy-time">{easyTimeToCompare}</span> | ||
</p> | ||
<p>Slug {slug}</p> | ||
</div> | ||
) | ||
|
||
/** @type {import('next').getStaticPaths} */ | ||
export const getStaticPaths = () => { | ||
return { | ||
paths: [], | ||
fallback: 'blocking', | ||
} | ||
} | ||
|
||
/** @type {import('next').GetStaticProps} */ | ||
export async function getStaticProps({ params }) { | ||
const date = new Date() | ||
return { | ||
props: { | ||
slug: params.slug, | ||
time: date.toISOString(), | ||
easyTimeToCompare: date.toTimeString(), | ||
}, | ||
revalidate: 60, | ||
} | ||
} | ||
|
||
export default Show |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:( need for Durable so reduce potential flakiness if different edge nodes would be used - tested page has 60s revalidate time because of that as well