Files
next.js/test/e2e/app-dir/app-client-cache/client-cache.experimental.test.ts
Zack TannerandGitHub eddd1ee186 add telemetry events for ppr & staleTimes experimental flags (#63981)
Adds `NextConfig.experimental.ppr` and
`NextConfig.experimental.staletimes` to the `NEXT_BUILD_FEATURE_USAGE`
telemetry event.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2981
2024-04-02 21:11:47 +00:00

312 lines
9.8 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import { browserConfigWithFixedTime, fastForwardTo } from './test-utils'
import { findAllTelemetryEvents } from 'next-test-utils'
describe('app dir client cache semantics (experimental staleTimes)', () => {
describe('dynamic: 0', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
nextConfig: {
experimental: { staleTimes: { dynamic: 0 } },
},
env: {
NEXT_TELEMETRY_DEBUG: '1',
},
})
if (isNextDev) {
// since the router behavior is different in development mode (no viewport prefetching + liberal revalidation)
// we only check the production behavior
it('should skip dev', () => {})
return
}
describe('prefetch={true}', () => {
it('should re-use the cache for 5 minutes (default "static" time)', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
let initialRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
await browser.elementByCss('[href="/"]').click()
let newRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).toBe(newRandomNumber)
await browser.eval(fastForwardTo, 30 * 1000) // fast forward 30 seconds
await browser.elementByCss('[href="/"]').click()
newRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).toBe(newRandomNumber)
await browser.eval(fastForwardTo, 5 * 60 * 1000) // fast forward 5 minutes
await browser.elementByCss('[href="/"]').click()
newRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).not.toBe(newRandomNumber)
})
})
describe('prefetch={false}', () => {
it('should trigger a loading state before fetching the page, followed by fresh data on every subsequent navigation', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
// this test introduces an artificial delay in rendering the requested page, so we verify a loading state is rendered
await browser
.elementByCss('[href="/2?timeout=1000"]')
.click()
.waitForElementByCss('#loading')
const initialRandomNumber = await browser
.waitForElementByCss('#random-number')
.text()
await browser.elementByCss('[href="/"]').click()
await browser.eval(fastForwardTo, 5 * 1000) // fast forward 5 seconds
const newRandomNumber = await browser
.elementByCss('[href="/2?timeout=1000"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).not.toBe(newRandomNumber)
})
describe('without a loading boundary', () => {
it('should get fresh data on every subsequent navigation', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
const initialRandomNumber = await browser
.elementByCss('[href="/2?timeout=1000"]')
.click()
.waitForElementByCss('#random-number')
.text()
await browser.elementByCss('[href="/"]').click()
await browser.eval(fastForwardTo, 5 * 1000) // fast forward 5 seconds
const newRandomNumber = await browser
.elementByCss('[href="/2?timeout=1000"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).not.toBe(newRandomNumber)
})
})
})
describe('prefetch={undefined} - default', () => {
it('should trigger a loading state before fetching the page, followed by fresh data on every subsequent navigation', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
// this test introduces an artificial delay in rendering the requested page, so we verify a loading state is rendered
await browser
.elementByCss('[href="/1?timeout=1000"]')
.click()
.waitForElementByCss('#loading')
const initialRandomNumber = await browser
.waitForElementByCss('#random-number')
.text()
await browser.elementByCss('[href="/"]').click()
await browser.eval(fastForwardTo, 5 * 1000) // fast forward 5 seconds
const newRandomNumber = await browser
.elementByCss('[href="/1?timeout=1000"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).not.toBe(newRandomNumber)
})
describe('without a loading boundary', () => {
it('should get fresh data on every subsequent navigation', async () => {
const browser = await next.browser(
'/without-loading',
browserConfigWithFixedTime
)
const initialRandomNumber = await browser
.elementByCss('[href="/without-loading/1?timeout=1000"]')
.click()
.waitForElementByCss('#random-number')
.text()
await browser.elementByCss('[href="/without-loading"]').click()
const newRandomNumber = await browser
.elementByCss('[href="/without-loading/1?timeout=1000"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).not.toBe(newRandomNumber)
})
})
})
describe('telemetry', () => {
it('should send staleTimes feature usage event', async () => {
const events = findAllTelemetryEvents(
next.cliOutput,
'NEXT_CLI_SESSION_STARTED'
)
expect(events).toEqual(
expect.arrayContaining([
expect.objectContaining({
staticStaleTime: null,
dynamicStaleTime: 0,
}),
])
)
})
})
})
describe('static: 180', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
nextConfig: {
experimental: { staleTimes: { static: 180 } },
},
env: {
NEXT_TELEMETRY_DEBUG: '1',
},
})
if (isNextDev) {
// since the router behavior is different in development mode (no viewport prefetching + liberal revalidation)
// we only check the production behavior
it('should skip dev', () => {})
return
}
describe('prefetch={true}', () => {
it('should use the custom static override time (3 minutes)', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
let initialRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
await browser.elementByCss('[href="/"]').click()
let newRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).toBe(newRandomNumber)
await browser.eval(fastForwardTo, 30 * 1000) // fast forward 30 seconds
await browser.elementByCss('[href="/"]').click()
newRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).toBe(newRandomNumber)
await browser.eval(fastForwardTo, 3 * 60 * 1000) // fast forward 3 minutes
await browser.elementByCss('[href="/"]').click()
newRandomNumber = await browser
.elementByCss('[href="/0?timeout=0"]')
.click()
.waitForElementByCss('#random-number')
.text()
expect(initialRandomNumber).not.toBe(newRandomNumber)
})
})
describe('prefetch={undefined} - default', () => {
it('should re-use the loading boundary for the custom static override time (3 minutes)', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
const loadingRandomNumber = await browser
.elementByCss('[href="/1?timeout=1000"]')
.click()
.waitForElementByCss('#loading')
.text()
await browser.elementByCss('[href="/"]').click()
await browser.eval(fastForwardTo, 2 * 60 * 1000) // fast forward 2 minutes
let newLoadingNumber = await browser
.elementByCss('[href="/1?timeout=1000"]')
.click()
.waitForElementByCss('#loading')
.text()
expect(loadingRandomNumber).toBe(newLoadingNumber)
await browser.elementByCss('[href="/"]').click()
await browser.eval(fastForwardTo, 2 * 60 * 1000) // fast forward 2 minutes
newLoadingNumber = await browser
.elementByCss('[href="/1?timeout=1000"]')
.click()
.waitForElementByCss('#loading')
.text()
expect(loadingRandomNumber).not.toBe(newLoadingNumber)
})
})
describe('telemetry', () => {
it('should send staleTimes feature usage event', async () => {
const events = findAllTelemetryEvents(
next.cliOutput,
'NEXT_CLI_SESSION_STARTED'
)
expect(events).toEqual(
expect.arrayContaining([
expect.objectContaining({
staticStaleTime: 180,
dynamicStaleTime: null,
}),
])
)
})
})
})
})