Commit 3f5f6a9c authored by Sam Selikoff's avatar Sam Selikoff

wip

parent 3ae9d4c8
...@@ -2,88 +2,89 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,88 +2,89 @@ import { nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright' import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act' import { createRouterAct } from 'router-act'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
;(process.env.__NEXT_CACHE_COMPONENTS ? describe.skip : describe)( import { outdent } from 'outdent'
'segment cache - export const unstable_staleTime',
() => { const isCacheComponentsEnabled = process.env.__NEXT_CACHE_COMPONENTS === 'true'
const { next, isNextDev } = nextTestSetup({
files: __dirname, describe('segment cache - export const unstable_staleTime', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (isNextDev || isCacheComponentsEnabled) {
test('skipped in development', () => {})
return
}
it('overrides global staleTimes config', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
}) })
const act = createRouterAct(page)
await page.clock.install()
const pageContent = 'Page with unstable_staleTime = 300'
// Prefetch page with unstable_staleTime=300 (5 minutes)
const toggleLink = await browser.elementByCss(
'input[data-link-accordion="/stale-5-minutes"]'
)
await act(
async () => {
await toggleLink.click()
await browser.elementByCss('a[href="/stale-5-minutes"]')
},
{ includes: pageContent }
)
// Hide link
await toggleLink.click()
// Advance 31 seconds - past global staleTimes (30s), within page unstable_staleTime (300s)
await page.clock.fastForward(31 * 1000)
if (isNextDev) { /*
test('skipped in development', () => {})
return
}
it('overrides global staleTimes config', async () => {
let page: Playwright.Page
const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page)
await page.clock.install()
const pageContent = 'Page with unstable_staleTime = 300'
// Prefetch page with unstable_staleTime=300 (5 minutes)
const toggleLink = await browser.elementByCss(
'input[data-link-accordion="/stale-5-minutes"]'
)
await act(
async () => {
await toggleLink.click()
await browser.elementByCss('a[href="/stale-5-minutes"]')
},
{ includes: pageContent }
)
// Hide link
await toggleLink.click()
// Advance 31 seconds - past global staleTimes (30s), within page unstable_staleTime (300s)
await page.clock.fastForward(31 * 1000)
/*
Should NOT refetch the content - page's unstable_staleTime=300 hasn't elapsed. Should NOT refetch the content - page's unstable_staleTime=300 hasn't elapsed.
Note there may be another tree prefetch, since that's controlled separately. So Note there may be another tree prefetch, since that's controlled separately. So
we just assert that the actual content of the page is not refetched. we just assert that the actual content of the page is not refetched.
*/ */
await act( await act(
async () => { async () => {
await toggleLink.click() await toggleLink.click()
await browser.elementByCss('a[href="/stale-5-minutes"]') await browser.elementByCss('a[href="/stale-5-minutes"]')
}, },
{ includes: pageContent, block: 'reject' } { includes: pageContent, block: 'reject' }
) )
// Hide link
await toggleLink.click()
// Advance to 5 minutes + 1ms total - past unstable_staleTime=300
await page.clock.fastForward(5 * 60 * 1000 - 31 * 1000 + 1)
// Should refetch - unstable_staleTime has elapsed
await act(
async () => {
await toggleLink.click()
await browser.elementByCss('a[href="/stale-5-minutes"]')
},
{ includes: pageContent }
)
})
// TODO: Test for caching unstable_staleTime on navigation without prefetch // Hide link
// await toggleLink.click()
// Currently, navigation responses (without prefetch) cache the route tree
// but not the segment data. The route tree cache entry is found on subsequent // Advance to 5 minutes + 1ms total - past unstable_staleTime=300
// navigations, but since segment data isn't cached, a server request is still await page.clock.fastForward(5 * 60 * 1000 - 31 * 1000 + 1)
// made. Fully implementing this feature requires writing segment data to the
// segment cache during navigation, which is a more significant change. // Should refetch - unstable_staleTime has elapsed
// await act(
// The unstable_staleTime segment config works correctly for prefetched routes. async () => {
} await toggleLink.click()
) await browser.elementByCss('a[href="/stale-5-minutes"]')
},
{ includes: pageContent }
)
})
// TODO: Test for caching unstable_staleTime on navigation without prefetch
//
// Currently, navigation responses (without prefetch) cache the route tree
// but not the segment data. The route tree cache entry is found on subsequent
// navigations, but since segment data isn't cached, a server request is still
// made. Fully implementing this feature requires writing segment data to the
// segment cache during navigation, which is a more significant change.
//
// The unstable_staleTime segment config works correctly for prefetched routes.
})
describe('unstable_staleTime - layout build error', () => { describe('unstable_staleTime - layout build error', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, skipped } = nextTestSetup({
...@@ -99,20 +100,21 @@ describe('unstable_staleTime - layout build error', () => { ...@@ -99,20 +100,21 @@ describe('unstable_staleTime - layout build error', () => {
it('should error when unstable_staleTime is used in a layout', async () => { it('should error when unstable_staleTime is used in a layout', async () => {
await next.patchFile( await next.patchFile(
'app/layout.tsx', 'app/layout.tsx',
`export const unstable_staleTime = 60 outdent`
export const unstable_staleTime = 60
export default function RootLayout({
children, export default function RootLayout({
}: { children,
children: React.ReactNode }: {
}) { children: React.ReactNode
return ( }) {
<html lang="en"> return (
<body>{children}</body> <html lang="en">
</html> <body>{children}</body>
) </html>
} )
`, }
`,
async () => { async () => {
try { try {
await next.start() await next.start()
...@@ -149,10 +151,11 @@ describe('unstable_staleTime - cacheComponents build error', () => { ...@@ -149,10 +151,11 @@ describe('unstable_staleTime - cacheComponents build error', () => {
it('should error when unstable_staleTime is used with cacheComponents', async () => { it('should error when unstable_staleTime is used with cacheComponents', async () => {
await next.patchFile( await next.patchFile(
'next.config.js', 'next.config.js',
`module.exports = { outdent`
cacheComponents: true, module.exports = {
} cacheComponents: true,
`, }
`,
async () => { async () => {
try { try {
await next.start() await next.start()
......
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