Skip to content
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: set swr delta #61330

Merged
merged 30 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4561f49
set swr delta
adrianha Jan 29, 2024
be80b23
Merge branch 'canary' into set-swr-delta
adrianha Feb 1, 2024
6e9fe22
use revalidate value for swr
adrianha Feb 2, 2024
dc80cf8
Merge branch 'canary' into set-swr-delta
adrianha Feb 4, 2024
65e96eb
Update revalidate.ts
adrianha Feb 5, 2024
40bdcca
Merge branch 'canary' into set-swr-delta
ijjk Feb 5, 2024
8d02440
fix failed tests
adrianha Feb 5, 2024
64f5199
other tests
ijjk Feb 5, 2024
1c33796
fix /gssp test swr value
adrianha Feb 5, 2024
aedaa68
Merge branch 'canary' into set-swr-delta
ijjk Feb 5, 2024
2f7aff4
fix /gssp test swr value
adrianha Feb 5, 2024
4b91b9f
Merge branch 'canary' into set-swr-delta
adrianha Feb 6, 2024
af3f842
Merge branch 'canary' into set-swr-delta
adrianha Feb 12, 2024
8c4775f
adjust prefetching tests
adrianha Feb 12, 2024
79acc91
Merge branch 'canary' into set-swr-delta
ijjk Feb 13, 2024
5ec40b5
Merge branch 'canary' into set-swr-delta
adrianha Feb 14, 2024
d579077
Merge branch 'canary' of github.com:vercel/next.js into set-swr-delta
adrianha Feb 14, 2024
570ec49
remove calculate _rsc test case
adrianha Feb 14, 2024
c8b3979
Merge branch 'canary' of github.com:vercel/next.js into set-swr-delta
adrianha Feb 14, 2024
3283dd7
Revert "remove calculate _rsc test case"
adrianha Feb 17, 2024
bea46bb
Revert "adjust prefetching tests"
adrianha Feb 17, 2024
21c842a
Revert "fix /gssp test swr value"
adrianha Feb 17, 2024
8c56ef7
Revert "fix /gssp test swr value"
adrianha Feb 17, 2024
b4c0093
Revert "other tests"
adrianha Feb 17, 2024
875ebf5
Revert "fix failed tests"
adrianha Feb 17, 2024
d74b5a2
add swrDelta to nextConfig
adrianha Feb 17, 2024
bfd3e2f
Merge branch 'canary' of github.com:vercel/next.js into set-swr-delta
adrianha Feb 17, 2024
62e6a70
Merge branch 'canary' of github.com:vercel/next.js into set-swr-delta
adrianha Feb 19, 2024
2fefae4
move swrDelta to experimental
adrianha Feb 19, 2024
10d9c2e
Merge branch 'canary' into set-swr-delta
ijjk Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function formatRevalidate(revalidate: Revalidate): string {
if (revalidate === 0) {
return 'private, no-cache, no-store, max-age=0, must-revalidate'
} else if (typeof revalidate === 'number') {
return `s-maxage=${revalidate}, stale-while-revalidate`
return `s-maxage=${revalidate}, stale-while-revalidate=${CACHE_ONE_YEAR}`
}

return `s-maxage=${CACHE_ONE_YEAR}, stale-while-revalidate`
return `s-maxage=${CACHE_ONE_YEAR}, stale-while-revalidate=${CACHE_ONE_YEAR}`
}
82 changes: 14 additions & 68 deletions test/e2e/app-dir/app-prefetch/prefetching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,59 +84,45 @@ createNextDescribe(
let requests: string[] = []

browser.on('request', (req) => {
requests.push(new URL(req.url()).pathname)
const url = new URL(req.url())
if (url.toString().includes(`/static-page?${NEXT_RSC_UNION_QUERY}=`)) {
requests.push(`${url.pathname}${url.search}`)
}
})
await browser.eval('location.href = "/"')

await browser.eval('location.href = "/"')
await browser.eval(
'window.nd.router.prefetch("/static-page", {kind: "auto"})'
)

await check(() => {
return requests.some(
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success'
: JSON.stringify(requests)
}, 'success')

await browser
.elementByCss('#to-static-page')
.click()
.waitForElementByCss('#static-page')

expect(
requests.filter((request) => request === '/static-page').length
).toBe(1)
await check(() => {
return requests.length === 2 ? 'success' : JSON.stringify(requests)
}, 'success')
})

it('should not fetch again when a static page was prefetched when navigating to it twice', async () => {
const browser = await next.browser('/404', browserConfigWithFixedTime)
let requests: string[] = []

browser.on('request', (req) => {
requests.push(new URL(req.url()).pathname)
const url = new URL(req.url())
if (url.toString().includes(`/static-page?${NEXT_RSC_UNION_QUERY}=`)) {
requests.push(`${url.pathname}${url.search}`)
}
})
await browser.eval('location.href = "/"')

await browser.eval('location.href = "/"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return requests.some(
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success'
: JSON.stringify(requests)
}, 'success')

await browser
.elementByCss('#to-static-page')
.click()
.waitForElementByCss('#static-page')

await browser
.elementByCss('#to-home')
// Go back to home page
Expand All @@ -148,49 +134,9 @@ createNextDescribe(
// Wait for the static page to load again
.waitForElementByCss('#static-page')

expect(
requests.filter(
(request) =>
request === '/static-page' || request.includes(NEXT_RSC_UNION_QUERY)
).length
).toBe(1)
})

it('should calculate `_rsc` query based on `Next-Url`', async () => {
const browser = await next.browser('/404', browserConfigWithFixedTime)
let staticPageRequests: string[] = []

browser.on('request', (req) => {
const url = new URL(req.url())
if (url.toString().includes(`/static-page?${NEXT_RSC_UNION_QUERY}=`)) {
staticPageRequests.push(`${url.pathname}${url.search}`)
}
})
await browser.eval('location.href = "/"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return staticPageRequests.length === 1
? 'success'
: JSON.stringify(staticPageRequests)
}, 'success')

// Unable to clear router cache so mpa navigation
await browser.eval('location.href = "/dashboard"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return staticPageRequests.length === 2
? 'success'
: JSON.stringify(staticPageRequests)
return requests.length === 2 ? 'success' : JSON.stringify(requests)
}, 'success')

expect(staticPageRequests[0]).toMatch('/static-page?_rsc=')
expect(staticPageRequests[1]).toMatch('/static-page?_rsc=')
// `_rsc` does not match because it depends on the `Next-Url`
expect(staticPageRequests[0]).not.toBe(staticPageRequests[1])
})

it('should not prefetch for a bot user agent', async () => {
Expand Down
8 changes: 6 additions & 2 deletions test/e2e/app-dir/ppr-full/ppr-full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ createNextDescribe(
expect(cacheControl).toEqual('no-store, must-revalidate')
} else if (dynamic === false || dynamic === 'force-static') {
expect(cacheControl).toEqual(
`s-maxage=${revalidate || '31536000'}, stale-while-revalidate`
`s-maxage=${
revalidate || '31536000'
}, stale-while-revalidate=31536000`
)
} else {
expect(cacheControl).toEqual(
Expand Down Expand Up @@ -338,7 +340,9 @@ createNextDescribe(
expect(cache).toEqual('public, max-age=0, must-revalidate')
} else {
expect(cache).toEqual(
`s-maxage=${revalidate || '31536000'}, stale-while-revalidate`
`s-maxage=${
revalidate || '31536000'
}, stale-while-revalidate=31536000`
)
}

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/prerender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ describe('Prerender', () => {
expect(initialRes.headers.get('cache-control')).toBe(
isDeploy
? 'public, max-age=0, must-revalidate'
: 's-maxage=2, stale-while-revalidate'
: 's-maxage=2, stale-while-revalidate=31536000'
)
})
}
Expand Down Expand Up @@ -1265,7 +1265,7 @@ describe('Prerender', () => {
expect(initialRes.headers.get('cache-control')).toBe(
isDeploy
? 'public, max-age=0, must-revalidate'
: 's-maxage=31536000, stale-while-revalidate'
: 's-maxage=31536000, stale-while-revalidate=31536000'
)
const initialHtml = await initialRes.text()
expect(initialHtml).toMatch(/hello.*?world/)
Expand Down
12 changes: 6 additions & 6 deletions test/integration/not-found-revalidate/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const runTests = () => {

const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
Expand All @@ -114,7 +114,7 @@ const runTests = () => {

const props2 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
expect(res.status).toBe(200)
expect(props2.found).toBe(true)
Expand All @@ -127,7 +127,7 @@ const runTests = () => {

const props3 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
expect(res.status).toBe(200)
expect(props3.found).toBe(true)
Expand Down Expand Up @@ -157,7 +157,7 @@ const runTests = () => {

const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
Expand All @@ -170,7 +170,7 @@ const runTests = () => {

const props2 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
expect(res.status).toBe(200)
expect(props2.found).toBe(true)
Expand All @@ -183,7 +183,7 @@ const runTests = () => {

const props3 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
expect(res.status).toBe(200)
expect(props3.found).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('required server files i18n', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)

await waitFor(2000)
Expand All @@ -183,7 +183,7 @@ describe('required server files i18n', () => {
})
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ describe('required server files', () => {
case: 'redirect no revalidate',
path: '/optional-ssg/redirect-1',
dest: '/somewhere',
cacheControl: 's-maxage=31536000, stale-while-revalidate',
cacheControl: 's-maxage=31536000, stale-while-revalidate=31536000',
},
{
case: 'redirect with revalidate',
path: '/optional-ssg/redirect-2',
dest: '/somewhere-else',
cacheControl: 's-maxage=5, stale-while-revalidate',
cacheControl: 's-maxage=5, stale-while-revalidate=31536000',
},
])(
`should have correct cache-control for $case`,
Expand Down Expand Up @@ -234,13 +234,13 @@ describe('required server files', () => {
case: 'notFound no revalidate',
path: '/optional-ssg/not-found-1',
dest: '/somewhere',
cacheControl: 's-maxage=31536000, stale-while-revalidate',
cacheControl: 's-maxage=31536000, stale-while-revalidate=31536000',
},
{
case: 'notFound with revalidate',
path: '/optional-ssg/not-found-2',
dest: '/somewhere-else',
cacheControl: 's-maxage=5, stale-while-revalidate',
cacheControl: 's-maxage=5, stale-while-revalidate=31536000',
},
])(
`should have correct cache-control for $case`,
Expand All @@ -267,7 +267,7 @@ describe('required server files', () => {
const res = await fetchViaHTTP(appPort, '/optional-ssg/props-no-revalidate')
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
's-maxage=31536000, stale-while-revalidate=31536000'
)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#props').text()).params).toEqual({
Expand All @@ -281,7 +281,7 @@ describe('required server files', () => {
)
expect(dataRes.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
's-maxage=31536000, stale-while-revalidate=31536000'
)
expect((await dataRes.json()).pageProps.params).toEqual({
rest: ['props-no-revalidate'],
Expand Down Expand Up @@ -449,7 +449,7 @@ describe('required server files', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)

await waitFor(2000)
Expand All @@ -460,7 +460,7 @@ describe('required server files', () => {
})
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31536000'
)
})

Expand Down