|
| 1 | +import { expect } from '@playwright/test' |
| 2 | +import { test } from '../utils/create-e2e-fixture.js' |
| 3 | + |
| 4 | +test.describe('app router on-demand revalidation', () => { |
| 5 | + test('revalidatePath', async ({ page, serverComponents }) => { |
| 6 | + // in case there is retry or some other test did hit that path before |
| 7 | + // we want to make sure that cdn cache is not warmed up |
| 8 | + const purgeCdnCache = await page.goto( |
| 9 | + new URL('/api/purge-cdn?path=/static-fetch/1', serverComponents.url).href, |
| 10 | + ) |
| 11 | + expect(purgeCdnCache?.status()).toBe(200) |
| 12 | + |
| 13 | + const response1 = await page.goto(new URL('static-fetch/1', serverComponents.url).href) |
| 14 | + const headers1 = response1?.headers() || {} |
| 15 | + expect(response1?.status()).toBe(200) |
| 16 | + expect(headers1['x-nextjs-cache']).toBeUndefined() |
| 17 | + // either first time hitting this route or we invalidated |
| 18 | + // just CDN node in earlier step |
| 19 | + // we will invoke function and see Next cache hit status \ |
| 20 | + // in the response because it was prerendered at build time |
| 21 | + // or regenerated in previous attempt to run this test |
| 22 | + expect(headers1['cache-status']).toMatch(/"Netlify Edge"; fwd=(miss|stale)/m) |
| 23 | + expect(headers1['cache-status']).toMatch(/"Next.js"; hit/m) |
| 24 | + expect(headers1['netlify-cdn-cache-control']).toBe( |
| 25 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 26 | + ) |
| 27 | + |
| 28 | + const date1 = await page.textContent('[data-testid="date-now"]') |
| 29 | + |
| 30 | + const h1 = await page.textContent('h1') |
| 31 | + expect(h1).toBe('Hello, Statically fetched show 1') |
| 32 | + |
| 33 | + const response2 = await page.goto(new URL('static-fetch/1', serverComponents.url).href) |
| 34 | + const headers2 = response2?.headers() || {} |
| 35 | + expect(response2?.status()).toBe(200) |
| 36 | + expect(headers2['x-nextjs-cache']).toBeUndefined() |
| 37 | + // we are hitting the same page again and we most likely will see |
| 38 | + // CDN hit (in this case Next reported cache status is omitted |
| 39 | + // as it didn't actually take place in handling this request) |
| 40 | + // or we will see CDN miss because different CDN node handled request |
| 41 | + expect(headers2['cache-status']).toMatch(/"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m) |
| 42 | + if (!headers2['cache-status'].includes('hit')) { |
| 43 | + // if we missed CDN cache, we will see Next cache hit status |
| 44 | + // as we reuse cached response |
| 45 | + expect(headers2['cache-status']).toMatch(/"Next.js"; hit/m) |
| 46 | + } |
| 47 | + expect(headers2['netlify-cdn-cache-control']).toBe( |
| 48 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 49 | + ) |
| 50 | + |
| 51 | + // the page is cached |
| 52 | + const date2 = await page.textContent('[data-testid="date-now"]') |
| 53 | + expect(date2).toBe(date1) |
| 54 | + |
| 55 | + const revalidate = await page.goto( |
| 56 | + new URL('/api/on-demand-revalidate/path', serverComponents.url).href, |
| 57 | + ) |
| 58 | + expect(revalidate?.status()).toBe(200) |
| 59 | + |
| 60 | + // now after the revalidation it should have a different date |
| 61 | + const response3 = await page.goto(new URL('static-fetch/1', serverComponents.url).href) |
| 62 | + const headers3 = response3?.headers() || {} |
| 63 | + expect(response3?.status()).toBe(200) |
| 64 | + expect(headers3?.['x-nextjs-cache']).toBeUndefined() |
| 65 | + // revalidatePath just marks the page(s) as invalid and does NOT |
| 66 | + // automatically refreshes the cache. This request will cause |
| 67 | + // Next.js cache miss and new response will be generated and cached |
| 68 | + // Depending if we hit same CDN node as previous request, we might |
| 69 | + // get either fwd=miss or fwd=stale |
| 70 | + expect(headers3['cache-status']).toMatch(/"Next.js"; fwd=miss/m) |
| 71 | + expect(headers3['cache-status']).toMatch(/"Netlify Edge"; fwd=(miss|stale)/m) |
| 72 | + expect(headers3['netlify-cdn-cache-control']).toBe( |
| 73 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 74 | + ) |
| 75 | + |
| 76 | + // the page has now an updated date |
| 77 | + const date3 = await page.textContent('[data-testid="date-now"]') |
| 78 | + expect(date3).not.toBe(date2) |
| 79 | + |
| 80 | + const response4 = await page.goto(new URL('static-fetch/1', serverComponents.url).href) |
| 81 | + const headers4 = response4?.headers() || {} |
| 82 | + expect(response4?.status()).toBe(200) |
| 83 | + expect(headers4?.['x-nextjs-cache']).toBeUndefined() |
| 84 | + // we are hitting the same page again and we most likely will see |
| 85 | + // CDN hit (in this case Next reported cache status is omitted |
| 86 | + // as it didn't actually take place in handling this request) |
| 87 | + // or we will see CDN miss because different CDN node handled request |
| 88 | + expect(headers4['cache-status']).toMatch(/"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m) |
| 89 | + if (!headers4['cache-status'].includes('hit')) { |
| 90 | + // if we missed CDN cache, we will see Next cache hit status |
| 91 | + // as we reuse cached response |
| 92 | + expect(headers4['cache-status']).toMatch(/"Next.js"; hit/m) |
| 93 | + } |
| 94 | + expect(headers4['netlify-cdn-cache-control']).toBe( |
| 95 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 96 | + ) |
| 97 | + |
| 98 | + // the page is cached |
| 99 | + const date4 = await page.textContent('[data-testid="date-now"]') |
| 100 | + expect(date4).toBe(date3) |
| 101 | + }) |
| 102 | + |
| 103 | + test('revalidateTag', async ({ page, serverComponents }) => { |
| 104 | + // in case there is retry or some other test did hit that path before |
| 105 | + // we want to make sure that cdn cache is not warmed up |
| 106 | + const purgeCdnCache = await page.goto( |
| 107 | + new URL('/api/purge-cdn?path=/static-fetch-1', serverComponents.url).href, |
| 108 | + ) |
| 109 | + expect(purgeCdnCache?.status()).toBe(200) |
| 110 | + |
| 111 | + const response1 = await page.goto(new URL('static-fetch-1', serverComponents.url).href) |
| 112 | + const headers1 = response1?.headers() || {} |
| 113 | + expect(response1?.status()).toBe(200) |
| 114 | + expect(headers1['x-nextjs-cache']).toBeUndefined() |
| 115 | + // either first time hitting this route or we invalidated |
| 116 | + // just CDN node in earlier step |
| 117 | + // we will invoke function and see Next cache hit status \ |
| 118 | + // in the response because it was prerendered at build time |
| 119 | + // or regenerated in previous attempt to run this test |
| 120 | + expect(headers1['cache-status']).toMatch(/"Netlify Edge"; fwd=(miss|stale)/m) |
| 121 | + expect(headers1['cache-status']).toMatch(/"Next.js"; hit/m) |
| 122 | + expect(headers1['netlify-cdn-cache-control']).toBe( |
| 123 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 124 | + ) |
| 125 | + |
| 126 | + const date1 = await page.textContent('[data-testid="date-now"]') |
| 127 | + |
| 128 | + const h1 = await page.textContent('h1') |
| 129 | + expect(h1).toBe('Hello, Static Fetch 1') |
| 130 | + |
| 131 | + const response2 = await page.goto(new URL('static-fetch-1', serverComponents.url).href) |
| 132 | + const headers2 = response2?.headers() || {} |
| 133 | + expect(response2?.status()).toBe(200) |
| 134 | + expect(headers2['x-nextjs-cache']).toBeUndefined() |
| 135 | + // we are hitting the same page again and we most likely will see |
| 136 | + // CDN hit (in this case Next reported cache status is omitted |
| 137 | + // as it didn't actually take place in handling this request) |
| 138 | + // or we will see CDN miss because different CDN node handled request |
| 139 | + expect(headers2['cache-status']).toMatch(/"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m) |
| 140 | + if (!headers2['cache-status'].includes('hit')) { |
| 141 | + // if we missed CDN cache, we will see Next cache hit status |
| 142 | + // as we reuse cached response |
| 143 | + expect(headers2['cache-status']).toMatch(/"Next.js"; hit/m) |
| 144 | + } |
| 145 | + expect(headers2['netlify-cdn-cache-control']).toBe( |
| 146 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 147 | + ) |
| 148 | + |
| 149 | + // the page is cached |
| 150 | + const date2 = await page.textContent('[data-testid="date-now"]') |
| 151 | + expect(date2).toBe(date1) |
| 152 | + |
| 153 | + const revalidate = await page.goto( |
| 154 | + new URL('/api/on-demand-revalidate/tag', serverComponents.url).href, |
| 155 | + ) |
| 156 | + expect(revalidate?.status()).toBe(200) |
| 157 | + |
| 158 | + // now after the revalidation it should have a different date |
| 159 | + const response3 = await page.goto(new URL('static-fetch-1', serverComponents.url).href) |
| 160 | + const headers3 = response3?.headers() || {} |
| 161 | + expect(response3?.status()).toBe(200) |
| 162 | + expect(headers3?.['x-nextjs-cache']).toBeUndefined() |
| 163 | + // revalidateTag just marks the page(s) as invalid and does NOT |
| 164 | + // automatically refreshes the cache. This request will cause |
| 165 | + // Next.js cache miss and new response will be generated and cached |
| 166 | + // Depending if we hit same CDN node as previous request, we might |
| 167 | + // get either fwd=miss or fwd=stale |
| 168 | + expect(headers3['cache-status']).toMatch(/"Next.js"; fwd=miss/m) |
| 169 | + expect(headers3['cache-status']).toMatch(/"Netlify Edge"; fwd=(miss|stale)/m) |
| 170 | + expect(headers3['netlify-cdn-cache-control']).toBe( |
| 171 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 172 | + ) |
| 173 | + |
| 174 | + // the page has now an updated date |
| 175 | + const date3 = await page.textContent('[data-testid="date-now"]') |
| 176 | + expect(date3).not.toBe(date2) |
| 177 | + |
| 178 | + const response4 = await page.goto(new URL('static-fetch-1', serverComponents.url).href) |
| 179 | + const headers4 = response4?.headers() || {} |
| 180 | + expect(response4?.status()).toBe(200) |
| 181 | + expect(headers4?.['x-nextjs-cache']).toBeUndefined() |
| 182 | + // we are hitting the same page again and we most likely will see |
| 183 | + // CDN hit (in this case Next reported cache status is omitted |
| 184 | + // as it didn't actually take place in handling this request) |
| 185 | + // or we will see CDN miss because different CDN node handled request |
| 186 | + expect(headers4['cache-status']).toMatch(/"Netlify Edge"; (hit|fwd=miss|fwd=stale)/m) |
| 187 | + if (!headers4['cache-status'].includes('hit')) { |
| 188 | + // if we missed CDN cache, we will see Next cache hit status |
| 189 | + // as we reuse cached response |
| 190 | + expect(headers4['cache-status']).toMatch(/"Next.js"; hit/m) |
| 191 | + } |
| 192 | + expect(headers4['netlify-cdn-cache-control']).toBe( |
| 193 | + 's-maxage=31536000, stale-while-revalidate=31536000', |
| 194 | + ) |
| 195 | + |
| 196 | + // the page is cached |
| 197 | + const date4 = await page.textContent('[data-testid="date-now"]') |
| 198 | + expect(date4).toBe(date3) |
| 199 | + }) |
| 200 | +}) |
0 commit comments