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

wip

parent 3ae9d4c8
......@@ -2,88 +2,89 @@ import { nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act'
import { retry } from 'next-test-utils'
;(process.env.__NEXT_CACHE_COMPONENTS ? describe.skip : describe)(
'segment cache - export const unstable_staleTime',
() => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
import { outdent } from 'outdent'
const isCacheComponentsEnabled = process.env.__NEXT_CACHE_COMPONENTS === 'true'
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.
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.
*/
await act(
async () => {
await toggleLink.click()
await browser.elementByCss('a[href="/stale-5-minutes"]')
},
{ 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 }
)
})
await act(
async () => {
await toggleLink.click()
await browser.elementByCss('a[href="/stale-5-minutes"]')
},
{ includes: pageContent, block: 'reject' }
)
// 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.
}
)
// 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
//
// 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', () => {
const { next, isNextDev, skipped } = nextTestSetup({
......@@ -99,20 +100,21 @@ describe('unstable_staleTime - layout build error', () => {
it('should error when unstable_staleTime is used in a layout', async () => {
await next.patchFile(
'app/layout.tsx',
`export const unstable_staleTime = 60
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
`,
outdent`
export const unstable_staleTime = 60
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
`,
async () => {
try {
await next.start()
......@@ -149,10 +151,11 @@ describe('unstable_staleTime - cacheComponents build error', () => {
it('should error when unstable_staleTime is used with cacheComponents', async () => {
await next.patchFile(
'next.config.js',
`module.exports = {
cacheComponents: true,
}
`,
outdent`
module.exports = {
cacheComponents: true,
}
`,
async () => {
try {
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