-
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.01%↑
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)
src/run/headers.ts
Outdated
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
src/run/handlers/cache.cts
Outdated
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?
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.
The method (I extracted from existing handling for App Router Route Handlers) supports false | number
for revalidate
:
opennextjs-netlify/src/run/headers.ts
Line 220 in 330dca0
: `s-maxage=${revalidate === false ? 31536000 : revalidate}, stale-while-revalidate=31536000, durable` |
false
meaning that revalidate
is not specified and then it would set ~permanent / 1 year caching header
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.
The test adjustment I did (minus currently wrong "message") -
opennextjs-netlify/tests/integration/page-router.test.ts
Lines 117 to 132 in 330dca0
const responseImplicitDefaultLocale = await invokeFunction(ctx, { | |
url: '/base/path/not-existing-page', | |
}) | |
expect( | |
responseImplicitDefaultLocale.statusCode, | |
'Response for not existing route if locale is not explicitly used in pathname (after basePath) should have 404 status', | |
).toBe(404) | |
expect( | |
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', | |
).toBe('s-maxage=31536000, stale-while-revalidate=31536000, durable') |
revalidate
not being specified and test asserting 1 year caching headers - this is used test fixture for it /** @type {import('next').GetStaticProps} */ | |
export const getStaticProps = ({ locale }) => { | |
return { | |
props: { | |
locale, | |
}, | |
} | |
} |
We currently don't have test case for 404 with getStaticProps
+ revalidate
being actually defined - I did some quick manual test and it seemed to be working (but this was real quick test and with rushing it maybe it's not). We should add a test case for it
… to limit potentially unwanted impact of the change
…od would have set cacheable header
// if we are serving already stale response, instruct edge to not attempt to cache that response | ||
headers.get('x-nextjs-cache') === 'STALE' | ||
? 'public, max-age=0, must-revalidate, durable' | ||
: `s-maxage=${revalidate || 31536000}, stale-while-revalidate=31536000, durable` |
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 it should be okay to use ${revalidate || 31536000}
because we need to account for false
and undefined
(see comment below), and even if revalidate
was 0
then we should be using 1 year.
['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.
In addition to removing !cacheControl || cacheControl.includes('no-store')
from the condition, I found I had to also remove typeof requestContext.pageHandlerRevalidate !== 'undefined'
because 404s without getStaticProps
do not go through the cache handler so pageHandlerRevalidate
is undefined
in those cases. Was it there as a precaution, or was there a specific case you were guarding against? @pieh
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.
Was it there as a precaution, or was there a specific case you were guarding against? @pieh
Precaution mainly (same like previous cache-control
check that we figure out we should remove during our previous sync), but I think the change you made make sense overall - it does widen scenarios of 404 caching where it possibly could not be intentional, but at the same time it catch more cases of 404 not being cached today
@@ -153,3 +153,124 @@ test<FixtureTestContext>('Should serve correct locale-aware custom 404 pages', a | |||
'Served 404 page content should use non-default locale if non-default locale is explicitly used in pathname (after basePath)', | |||
).toBe('fr') | |||
}) | |||
|
|||
describe.only('404 caching', () => { | |||
describe('404 without getStaticProps', () => { |
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 found it was necessary to keep these tests because handling is different for 404 without getStaticProps
, however I did remove cases for 'matching dynamic path without revalidate' because behaviour should always be the same as 'matching dynamic path with revalidate' and cache handler should be called in both cases
Description
404 responses are currently uncached and this PR introduces cache control headers that are consistent with those generated by Vercel
The change is breaking, as some customers may be relying on the current behaviour, so it is behind an env var for now, but it paves the way to releasing this in the next major version bump.
Tests
Integration tests added
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