Files
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

200 lines
6.5 KiB
TypeScript

import { createNextDescribe } from 'e2e-utils'
import { check, findAllTelemetryEvents } from 'next-test-utils'
createNextDescribe(
'ppr',
{
files: __dirname,
env: {
NEXT_TELEMETRY_DEBUG: '1',
},
},
({ next, isNextDev, isNextStart }) => {
it('should indicate the feature is experimental', async () => {
await check(() => {
return next.cliOutput.includes('Experiments (use with caution)') &&
next.cliOutput.includes('ppr')
? 'success'
: 'fail'
}, 'success')
})
if (isNextStart) {
describe('build output', () => {
it('correctly marks pages as being partially prerendered in the build output', () => {
expect(next.cliOutput).toContain('◐ /loading/nested/[slug]')
expect(next.cliOutput).toContain('◐ /suspense/node')
expect(next.cliOutput).toContain('◐ /suspense/node/gsp/[slug]')
expect(next.cliOutput).toContain('◐ /suspense/node/nested/[slug]')
})
})
describe('telemetry', () => {
it('should send ppr feature usage event', async () => {
const events = findAllTelemetryEvents(
next.cliOutput,
'NEXT_BUILD_FEATURE_USAGE'
)
expect(events).toContainEqual({
featureName: 'experimental/ppr',
invocationCount: 1,
})
})
})
}
describe.each([
{ pathname: '/suspense/node' },
{ pathname: '/suspense/node/nested/1' },
{ pathname: '/suspense/node/nested/2' },
{ pathname: '/suspense/node/nested/3' },
{ pathname: '/loading/nested/1' },
{ pathname: '/loading/nested/2' },
{ pathname: '/loading/nested/3' },
])('for $pathname', ({ pathname }) => {
// When we're partially pre-rendering, we should get the static parts
// immediately, and the dynamic parts after the page loads. So we should
// see the static part in the output, but the dynamic part should be
// missing.
it('should serve the static part', async () => {
const $ = await next.render$(pathname)
expect($('#page').length).toBe(1)
})
if (isNextDev) {
it('should have the dynamic part', async () => {
let $ = await next.render$(pathname)
let dynamic = $('#container > #dynamic > #state')
expect(dynamic.length).toBe(1)
expect(dynamic.text()).toBe('Not Signed In')
$ = await next.render$(
pathname,
{},
{
headers: {
cookie: 'session=1',
},
}
)
dynamic = $('#container > #dynamic > #state')
expect(dynamic.length).toBe(1)
expect(dynamic.text()).toBe('Signed In')
})
} else {
it('should not have the dynamic part', async () => {
const $ = await next.render$(pathname)
expect($('#container > #dynamic > #state').length).toBe(0)
})
}
})
describe.each([
{ pathname: '/suspense/node' },
{ pathname: '/suspense/edge' },
])('with suspense for $pathname', ({ pathname }) => {
// When the browser loads the page, we expect that the dynamic part will
// be rendered.
it('should eventually render the dynamic part', async () => {
const browser = await next.browser(pathname)
try {
// Wait for the page part to load.
await browser.waitForElementByCss('#page')
await browser.waitForIdleNetwork()
// Wait for the dynamic part to load.
await browser.waitForElementByCss('#container > #dynamic > #state')
// Ensure we've got the right dynamic part.
let dynamic = await browser
.elementByCss('#container > #dynamic > #state')
.text()
expect(dynamic).toBe('Not Signed In')
// Re-visit the page with the cookie.
await browser.addCookie({ name: 'session', value: '1' }).refresh()
// Wait for the page part to load.
await browser.waitForElementByCss('#page')
await browser.waitForIdleNetwork()
// Wait for the dynamic part to load.
await browser.waitForElementByCss('#container > #dynamic > #state')
// Ensure we've got the right dynamic part.
dynamic = await browser
.elementByCss('#container > #dynamic > #state')
.text()
expect(dynamic).toBe('Signed In')
} finally {
await browser.deleteCookies()
await browser.close()
}
})
})
describe('search parameters', () => {
it('should render the page with the search parameters', async () => {
const expected = `${Date.now()}:${Math.random()}`
const res = await next.fetch(
`/search?query=${encodeURIComponent(expected)}`
)
expect(res.status).toBe(200)
const html = await res.text()
expect(html).toContain(expected)
})
})
describe.each([{ pathname: '/no-suspense' }])(
'without suspense for $pathname',
({ pathname }) => {
// When the browser loads the page, we expect that the dynamic part will
// be rendered.
it('should immediately render the dynamic part', async () => {
let $ = await next.render$(pathname)
let dynamic = $('#container > #dynamic > #state').text()
expect(dynamic).toBe('Not Signed In')
// Re-visit the page with the cookie.
$ = await next.render$(
pathname,
{},
{
headers: {
cookie: 'session=1',
},
}
)
dynamic = $('#container > #dynamic > #state').text()
expect(dynamic).toBe('Signed In')
})
}
)
describe('/no-suspense/node/gsp/[slug]', () => {
it('should serve the static & dynamic parts', async () => {
const $ = await next.render$('/no-suspense/node/gsp/foo')
expect($('#page').length).toBe(1)
expect($('#container > #dynamic > #state').length).toBe(1)
})
})
describe('/suspense/node/gsp/[slug]', () => {
it('should serve the static part first', async () => {
const $ = await next.render$('/suspense/node/gsp/foo')
expect($('#page').length).toBe(1)
})
it('should not have the dynamic part', async () => {
const $ = await next.render$('/suspense/node/gsp/foo')
expect($('#container > #dynamic > #state').length).toBe(0)
})
})
}
)