-
Notifications
You must be signed in to change notification settings - Fork 87
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
fix: apply caching headers to pages router 404 with getStaticProps #2764
base: main
Are you sure you want to change the base?
Conversation
bac472e
to
330dca0
Compare
📊 Package size report 0.02%↑
Unchanged files
🤖 This report was automatically generated by pkg-size-action |
@@ -126,6 +126,10 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a | |||
load(responseImplicitDefaultLocale.body)('[data-testid="locale"]').text(), | |||
'Served 404 page content should use default locale if locale is not explicitly used in pathname (after basePath)', | |||
).toBe('en') | |||
expect( | |||
responseImplicitDefaultLocale.headers['netlify-cdn-cache-control'], | |||
'Response for not existing route if locale is not explicitly used in pathname (after basePath) should have 404 status', |
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.
ops this is not right msg - just copied some expect()
line from before and forgot to adjust it (and same for other assertions here)
typeof requestContext.pageHandlerRevalidate !== 'undefined' && | ||
['GET', 'HEAD'].includes(request.method) && | ||
!headers.has('cdn-cache-control') && | ||
!headers.has('netlify-cdn-cache-control') |
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.
Probably should add check for cache-control
being ~no-cache
and/or no-store
to let us use known working case this is after this
const requestContext = getRequestContext() | ||
if (requestContext) { | ||
requestContext.pageHandlerRevalidate = revalidate | ||
} | ||
|
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.
not sure if we should do this for app router - maybe for this PR if we treat it as hot fix - remove this
@@ -210,6 +213,7 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void> | |||
const key = routeToFilePath(route) | |||
const value = await buildPagesCacheValue( | |||
join(ctx.publishDir, 'server/pages', key), | |||
undefined, |
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.
I think you're right that this is the safest approach, seeing as we're not seeing any specific problems with the fallbacks at present.
// handle CDN Cache Control on 404 Page responses | ||
setCacheControlFromRequestContext(headers, requestContext.pageHandlerRevalidate) | ||
return | ||
} |
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.
Just making sure I understand correctly - this will only add cache control headers to 404 pages with a revalidate value. What about 404 pages that use getStaticProps but don't specify revalidate?
Description
Documentation
Tests
You can test this change yourself like so:
Relevant links (GitHub issues, etc.) or a picture of cute animal
Fixes https://linear.app/netlify/issue/FRB-1636/404-page-with-getstaticprops-is-not-cached-on-cdn