-
Notifications
You must be signed in to change notification settings - Fork 89
fix: improved cdn cache hooks for dynamic 404 pages #2786
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2ea0e52
test: fixture for dynamic cms results
mrstork d278f66
test: case for invalidating dynamic 404 pages
mrstork beec5f5
fix: wip - fix for caching issues on catch all routes
mrstork e95639a
test: not all versions of next support next.config.ts
mrstork 1aee505
test: older versions of next have different cache headers
mrstork 2b0280b
test: edge cache response depends on the node we hit
mrstork 0fd916d
fix: set cache tags on 404 pages
mrstork d96b20c
chore: clean up variable names
mrstork de960f4
chore: remove previous attempt to set cache tags on 404s
mrstork 4cf7b2f
fix: also target 404 pages when purging cache
mrstork 298bd8b
Merge remote-tracking branch 'origin/main' into FRB-1708/purge-cache-…
mrstork ca7ca4e
chore: remove wip check for cached api calls
mrstork cf47c74
chore: run format:fix
mrstork 9e49b26
Merge branch 'main' into FRB-1708/purge-cache-catch-all-routes
mrstork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { expect } from '@playwright/test' | ||
import { test } from '../utils/playwright-helpers.js' | ||
|
||
test.describe('Dynamic CMS', () => { | ||
test('Invalidates 404 pages from durable cache', async ({ page, dynamicCms }) => { | ||
// 1. Verify the status and headers of the dynamic page | ||
const response1 = await page.goto(new URL('/content/blog', dynamicCms.url).href) | ||
const headers1 = response1?.headers() || {} | ||
|
||
expect(response1?.status()).toEqual(404) | ||
expect(headers1['cache-control']).toEqual('public,max-age=0,must-revalidate') | ||
expect(headers1['cache-status']).toEqual( | ||
'"Next.js"; fwd=miss, "Netlify Durable"; fwd=uri-miss; stored, "Netlify Edge"; fwd=miss', | ||
) | ||
expect(headers1['netlify-cache-tag']).toEqual('_n_t_/content/blog') | ||
expect(headers1['netlify-cdn-cache-control']).toEqual('s-maxage=31536000, durable') | ||
|
||
// 2. Publish the blob, revalidate the dynamic page, and wait to regenerate | ||
await page.goto(new URL('/cms/publish', dynamicCms.url).href) | ||
await page.goto(new URL('/api/revalidate?path=/content/blog', dynamicCms.url).href) | ||
await page.waitForTimeout(1000) | ||
|
||
// 3. Verify the status and headers of the dynamic page | ||
const response2 = await page.goto(new URL('/content/blog', dynamicCms.url).href) | ||
const headers2 = response2?.headers() || {} | ||
|
||
expect(response2?.status()).toEqual(200) | ||
expect(headers2['cache-control']).toEqual('public,max-age=0,must-revalidate') | ||
expect(headers2['cache-status']).toMatch( | ||
/"Next.js"; hit, "Netlify Durable"; fwd=stale; ttl=[0-9]+; stored, "Netlify Edge"; fwd=stale/, | ||
) | ||
expect(headers2['netlify-cache-tag']).toEqual('_n_t_/content/blog') | ||
expect(headers2['netlify-cdn-cache-control']).toEqual('s-maxage=31536000, durable') | ||
|
||
// 4. Unpublish the blob, revalidate the dynamic page, and wait to regenerate | ||
await page.goto(new URL('/cms/unpublish', dynamicCms.url).href) | ||
await page.goto(new URL('/api/revalidate?path=/content/blog', dynamicCms.url).href) | ||
await page.waitForTimeout(1000) | ||
|
||
// 5. Verify the status and headers of the dynamic page | ||
const response3 = await page.goto(new URL('/content/blog', dynamicCms.url).href) | ||
const headers3 = response3?.headers() || {} | ||
|
||
expect(response3?.status()).toEqual(404) | ||
expect(headers3['cache-control']).toEqual('public,max-age=0,must-revalidate') | ||
expect(headers3['cache-status']).toMatch( | ||
/"Next.js"; fwd=miss, "Netlify Durable"; fwd=stale; ttl=[0-9]+; stored, "Netlify Edge"; fwd=stale/, | ||
) | ||
expect(headers3['netlify-cache-tag']).toEqual('_n_t_/content/blog') | ||
expect(headers3['netlify-cdn-cache-control']).toEqual('s-maxage=31536000, durable') | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This fixture is meant to emulate dynamic content responses of a CMS-backed next site |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getDeployStore } from '@netlify/blobs' | ||
import { Context } from '@netlify/functions' | ||
|
||
// publish or unpublish "cms content" depending on the sent operation | ||
export default async function handler(_request: Request, context: Context) { | ||
const store = getDeployStore({ name: 'cms-content', consistency: 'strong' }) | ||
const BLOB_KEY = 'key' | ||
|
||
const operation = context.params['operation'] | ||
|
||
if (operation === 'publish') { | ||
await store.setJSON(BLOB_KEY, { content: true }) | ||
} | ||
|
||
if (operation === 'unpublish') { | ||
await store.delete(BLOB_KEY) | ||
} | ||
|
||
return Response.json({ ok: true }) | ||
} | ||
|
||
export const config = { | ||
path: '/cms/:operation', | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
output: 'standalone', | ||
eslint: { | ||
ignoreDuringBuilds: true, | ||
}, | ||
generateBuildId: () => 'build-id', | ||
} | ||
|
||
module.exports = nextConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "dynamic-cms", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"postinstall": "next build", | ||
"dev": "next dev", | ||
"build": "next build" | ||
}, | ||
"dependencies": { | ||
"@netlify/blobs": "^8.1.0", | ||
"@netlify/functions": "^2.7.0", | ||
"@netlify/plugin-nextjs": "^5.10.1", | ||
"netlify-cli": "^19.0.3", | ||
"next": "latest", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "22.13.13", | ||
"@types/react": "19.0.12", | ||
"typescript": "5.8.2" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function NotFound() { | ||
return <p>Custom 404 page</p> | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default async function handler(req, res) { | ||
try { | ||
const pathToPurge = req.query.path ?? '/static/revalidate-manual' | ||
await res.revalidate(pathToPurge) | ||
return res.json({ code: 200, message: 'success' }) | ||
} catch (err) { | ||
return res.status(500).send({ code: 500, message: err.message }) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getDeployStore } from '@netlify/blobs' | ||
|
||
const Content = ({ value }) => ( | ||
<div> | ||
<p> | ||
<span>{JSON.stringify(value)}</span> | ||
</p> | ||
</div> | ||
) | ||
|
||
export async function getStaticProps() { | ||
const store = getDeployStore({ name: 'cms-content', consistency: 'strong' }) | ||
const BLOB_KEY = 'key' | ||
|
||
const value = await store.get(BLOB_KEY, { type: 'json' }) | ||
|
||
if (!value) { | ||
return { | ||
notFound: true, | ||
} | ||
} | ||
|
||
return { | ||
props: { | ||
value: value, | ||
}, | ||
} | ||
} | ||
|
||
export const getStaticPaths = () => { | ||
return { | ||
paths: [], | ||
fallback: 'blocking', // false or "blocking" | ||
} | ||
} | ||
|
||
export default Content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.