Commit 36acd68b authored by Janka Uryga's avatar Janka Uryga

test: renders after a redirect() should not bypass caches

parent ea08bf27
......@@ -1345,6 +1345,33 @@ describe('app-dir action handling', () => {
})
})
it('should use the fetch cache when rendering after a redirect without revalidations', async () => {
const browser = await next.browser('/revalidate')
// Uncached random, should be different every time
const randomNumber = await browser.elementByCss('#random-number').text()
// Cached fetches
const justPutIt = await browser.elementByCss('#justputit').text()
const thankYouNext = await browser.elementByCss('#thankyounext').text()
// Trigger an action that redirects to the same page
await browser.elementByCss('#redirect').click()
// The action should return a freshly rendered version of the page,
// so the random number should change
await retry(async () => {
const newRandomNumber = await browser
.elementByCss('#random-number')
.text()
expect(newRandomNumber).not.toBe(randomNumber)
})
// The new render should re-use the cached fetches, so they should be the same
const newJustPutIt = await browser.elementByCss('#justputit').text()
const newThankYouNext = await browser.elementByCss('#thankyounext').text()
expect(newJustPutIt).toBe(justPutIt)
expect(newThankYouNext).toBe(thankYouNext)
})
it('should store revalidation data in the prefetch cache', async () => {
const browser = await next.browser('/revalidate')
const justPutIt = await browser.elementByCss('#justputit').text()
......
......@@ -10,17 +10,27 @@ async function getCachedValue() {
return Math.random()
}
async function getCachedValue2() {
'use cache'
return Math.random()
}
export default async function Page() {
// Make the page dynamic, as we don't want to deal with ISR in this scenario.
await connection()
const a = await getCachedValue()
const b = await getCachedValue()
const c = await getCachedValue2()
const timestamp = Date.now()
return (
<div>
<p id="a">{a}</p>
<p id="b">{b}</p>
<p id="c">{c}</p>
<div id="timestamp">{timestamp}</div>
<Link href="/revalidate-and-redirect/redirect">
Go to /revalidate-and-redirect/redirect
</Link>
......
......@@ -26,6 +26,15 @@ export default function Page() {
>
Revalidate path and redirect
</button>
<button
id="redirect-without-revalidations"
formAction={async () => {
'use server'
redirect('/revalidate-and-redirect')
}}
>
Redirect without revalidations
</button>
</form>
)
}
......@@ -313,8 +313,11 @@ describe('use-cache', () => {
it('should revalidate caches after redirect', async () => {
const browser = await next.browser('/revalidate-and-redirect')
// A and B come from the same cache
const valueA = await browser.elementById('a').text()
const valueB = await browser.elementById('b').text()
// C is unrelated to A and B
const valueC = await browser.elementById('c').text()
expect(valueA).toBe(valueB)
......@@ -322,15 +325,20 @@ describe('use-cache', () => {
.elementByCss('a[href="/revalidate-and-redirect/redirect"]')
.click()
// Revalidate a tag that A/B are tagged with, and redirect to the page where they are used.
await browser.elementById('revalidate-tag-redirect').click()
const newValueA = await browser.elementById('a').text()
const newValueB = await browser.elementById('b').text()
const newValueC = await browser.elementById('c').text()
expect(newValueA).toBe(newValueB)
// The action should return an updated page with new values for A and B
expect(newValueA).not.toBe(valueA)
expect(newValueB).toBe(newValueB)
expect(newValueA).toBe(newValueB)
// We didn't revalidate C, so it should remain unchanged
expect(newValueC).toBe(valueC)
// Revalidate the path of the original page, which should bypass all the caches it used
await browser
.elementByCss('a[href="/revalidate-and-redirect/redirect"]')
.click()
......@@ -338,10 +346,40 @@ describe('use-cache', () => {
const finalValueA = await browser.elementById('a').text()
const finalValueB = await browser.elementById('b').text()
const finalValueC = await browser.elementById('c').text()
expect(finalValueA).not.toBe(newValueA)
expect(finalValueB).not.toBe(newValueB)
expect(finalValueB).toBe(finalValueB)
expect(finalValueC).not.toBe(valueC)
})
it('should use existing caches after a redirect without any revalidations', async () => {
const browser = await next.browser('/revalidate-and-redirect')
// Uncached, should change on every render
const initialTimestamp = await browser.elementById('timestamp').text()
// A and B come from the same cache
const valueA = await browser.elementById('a').text()
const valueB = await browser.elementById('b').text()
expect(valueA).toBe(valueB)
await browser
.elementByCss('a[href="/revalidate-and-redirect/redirect"]')
.click()
// Redirect back to the page we started on.
await browser.elementById('redirect-without-revalidations').click()
// The action should return an updated page. The timestamp is not cached, so it should change.
const updatedTimestamp = await browser.elementById('timestamp').text()
expect(initialTimestamp).not.toBe(updatedTimestamp)
// We didn't revalidate A/B, so they should use the existing cache entries and return the same value as before.
const newValueA = await browser.elementById('a').text()
const newValueB = await browser.elementById('b').text()
expect(newValueA).toBe(valueA)
expect(newValueB).toBe(valueB)
})
it('should revalidate caches nested in unstable_cache', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment