@next/playwright 16.2.0-canary.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # @next/playwright
2
+
3
+ > - **Status:** Experimental. This API is not yet stable.
4
+ > - **Requires:** [Cache Components](https://nextjs.org/docs) to be enabled.
5
+
6
+ Playwright helpers for testing Next.js applications.
7
+
8
+ ## Instant Navigation Testing
9
+
10
+ An **instant navigation** commits immediately without waiting for data fetching.
11
+ The cached shell — including any Suspense loading boundaries — renders right
12
+ away, and dynamic data streams in afterward. The shell is the instant part, not
13
+ the full page.
14
+
15
+ `instant()` lets you test whether a route achieves this. While the callback is
16
+ active, navigations render only cached and prefetched content. Dynamic data is
17
+ deferred until the callback returns. This lets you make deterministic assertions
18
+ against the shell without race conditions.
19
+
20
+ The tool assumes a warm cache: all prefetches have completed and all cacheable
21
+ data is available. This way you're testing whether the route is *structured
22
+ correctly* for instant navigation, independent of network timing. If content you
23
+ expected to be cached is missing inside the callback, it points to a problem — a
24
+ missing `use cache` directive, a misplaced Suspense boundary, or a similar gap.
25
+
26
+ ### Examples
27
+
28
+ **Loading shell appears instantly** (dynamic content behind a Suspense boundary):
29
+
30
+ ```ts
31
+ import { instant } from '@next/playwright'
32
+
33
+ test('shows loading shell during navigation', async ({ page }) => {
34
+ await page.goto('/')
35
+
36
+ await instant(page, async () => {
37
+ await page.click('a[href="/dashboard"]')
38
+
39
+ // The loading shell is visible — dynamic data is deferred
40
+ await expect(page.locator('[data-testid="loading"]')).toBeVisible()
41
+ })
42
+
43
+ // After instant() returns, dynamic data streams in normally
44
+ await expect(page.locator('[data-testid="content"]')).toBeVisible()
45
+ })
46
+ ```
47
+
48
+ **Fully instant navigation** (all content is cached):
49
+
50
+ ```ts
51
+ test('navigates to profile instantly', async ({ page }) => {
52
+ await page.goto('/')
53
+
54
+ await instant(page, async () => {
55
+ await page.click('a[href="/profile"]')
56
+
57
+ // All content renders immediately
58
+ await expect(page.locator('[data-testid="profile-name"]')).toBeVisible()
59
+ await expect(page.locator('[data-testid="profile-bio"]')).toBeVisible()
60
+ })
61
+ })
62
+ ```
63
+
64
+ ### Enabling in production builds
65
+
66
+ In development (`next dev`), the testing API is available by default. In
67
+ production builds, it is disabled unless you explicitly opt in:
68
+
69
+ ```js
70
+ // next.config.js
71
+ module.exports = {
72
+ experimental: {
73
+ exposeTestingApiInProductionBuild: true,
74
+ },
75
+ }
76
+ ```
77
+
78
+ This is not meant to be deployed to live production sites. Only enable it in
79
+ controlled testing environments like preview deployments or CI.
80
+
81
+ ## How it works
82
+
83
+ `instant()` sets a cookie that tells Next.js to serve only cached data during
84
+ navigations. While the cookie is active:
85
+
86
+ - **Client-side navigations**: The router renders only what is available in the
87
+ prefetch cache. Dynamic data is deferred until the cookie is cleared.
88
+ - **Server renders (initial load, reload, MPA navigation)**: The server responds
89
+ with only the static shell, without any per-request dynamic data.
90
+
91
+ When the callback completes, the cookie is cleared and normal behavior resumes.
92
+
93
+ ## Design
94
+
95
+ The layering between this package and Next.js is intentionally very thin. This
96
+ serves as a reference implementation that other testing frameworks and dev tools
97
+ can replicate with minimal effort. The entire mechanism is a single cookie:
98
+
99
+ ```ts
100
+ // Set the cookie to enter instant mode
101
+ document.cookie = 'next-instant-navigation-testing=1; path=/'
102
+
103
+ // ... run assertions ...
104
+
105
+ // Clear the cookie to resume normal behavior
106
+ document.cookie = 'next-instant-navigation-testing=; path=/; max-age=0'
107
+ ```
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Minimal interfaces for Playwright's Page and BrowserContext. We use
3
+ * structural types rather than importing from a specific Playwright package
4
+ * so this works with any version of playwright, playwright-core, or
5
+ * @playwright/test.
6
+ */
7
+ interface PlaywrightBrowserContext {
8
+ addCookies(cookies: Array<{
9
+ name: string;
10
+ value: string;
11
+ url?: string;
12
+ domain?: string;
13
+ path?: string;
14
+ }>): Promise<void>;
15
+ clearCookies(options?: {
16
+ name?: string;
17
+ }): Promise<void>;
18
+ }
19
+ interface PlaywrightPage {
20
+ url(): string;
21
+ context(): PlaywrightBrowserContext;
22
+ }
23
+ /**
24
+ * Runs a function with instant navigation enabled. Within this scope,
25
+ * navigations render the prefetched UI immediately and wait for the
26
+ * callback to complete before streaming in dynamic data.
27
+ *
28
+ * Uses the cookie-based protocol: setting the cookie acquires the
29
+ * navigation lock (via CookieStore change event), and clearing it
30
+ * releases the lock.
31
+ *
32
+ * If the page is already loaded, the URL is inferred
33
+ * automatically. For a fresh page (before any navigation), pass
34
+ * `baseURL` so the cookie can be scoped to the correct domain:
35
+ *
36
+ * await instant(page, async () => {
37
+ * await page.goto(url)
38
+ * // ...
39
+ * }, { baseURL: 'http://localhost:3000' })
40
+ */
41
+ export declare function instant<T>(page: PlaywrightPage, fn: () => Promise<T>, options?: {
42
+ baseURL?: string;
43
+ }): Promise<T>;
44
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.instant = instant;
4
+ const INSTANT_COOKIE = 'next-instant-navigation-testing';
5
+ /**
6
+ * Runs a function with instant navigation enabled. Within this scope,
7
+ * navigations render the prefetched UI immediately and wait for the
8
+ * callback to complete before streaming in dynamic data.
9
+ *
10
+ * Uses the cookie-based protocol: setting the cookie acquires the
11
+ * navigation lock (via CookieStore change event), and clearing it
12
+ * releases the lock.
13
+ *
14
+ * If the page is already loaded, the URL is inferred
15
+ * automatically. For a fresh page (before any navigation), pass
16
+ * `baseURL` so the cookie can be scoped to the correct domain:
17
+ *
18
+ * await instant(page, async () => {
19
+ * await page.goto(url)
20
+ * // ...
21
+ * }, { baseURL: 'http://localhost:3000' })
22
+ */
23
+ async function instant(page, fn, options) {
24
+ // Acquire the lock by setting the cookie via the browser context. This
25
+ // ensures the cookie is present even on the very first navigation.
26
+ // The cookie triggers the CookieStore change event in
27
+ // navigation-testing-lock.ts, which acquires the in-memory navigation lock.
28
+ const { hostname } = new URL(resolveURL(page, options));
29
+ await page
30
+ .context()
31
+ .addCookies([
32
+ { name: INSTANT_COOKIE, value: '1', domain: hostname, path: '/' },
33
+ ]);
34
+ try {
35
+ return await fn();
36
+ }
37
+ finally {
38
+ // Release the lock by clearing the cookie. For SPA navigations, this
39
+ // triggers the CookieStore change event which resolves the in-memory
40
+ // lock. For MPA navigations (reload, plain anchor), the listener in
41
+ // app-bootstrap.ts triggers a page reload to fetch dynamic data.
42
+ await page.context().clearCookies({ name: INSTANT_COOKIE });
43
+ }
44
+ }
45
+ /**
46
+ * Resolves the URL to scope the instant navigation cookie to. Prefers
47
+ * an explicit `baseURL` option, then falls back to the page's current URL.
48
+ * Throws a descriptive error if neither is available (e.g. fresh page
49
+ * before any navigation).
50
+ */
51
+ function resolveURL(page, options) {
52
+ var _a;
53
+ const url = (_a = options === null || options === void 0 ? void 0 : options.baseURL) !== null && _a !== void 0 ? _a : page.url();
54
+ if (url && url !== 'about:blank') {
55
+ return url;
56
+ }
57
+ const error = new Error(`Could not infer the base URL of the application.
58
+
59
+ instant() needs to know the base URL so it can configure the
60
+ browser before the first page load. If the page is already
61
+ loaded, the base URL is detected automatically.
62
+ Otherwise, you can fix this in one of two ways:
63
+
64
+ 1. Pass a baseURL option:
65
+
66
+ await instant(page, async () => {
67
+ await page.goto('http://localhost:3000')
68
+ // ...
69
+ }, { baseURL: 'http://localhost:3000' })
70
+
71
+ Tip: If you use baseURL in your Playwright config, you can
72
+ get it from the test fixture:
73
+
74
+ test('my test', async ({ page, baseURL }) => {
75
+ await instant(page, async () => {
76
+ // ...
77
+ }, { baseURL })
78
+ })
79
+
80
+ 2. Navigate to a page before calling instant():
81
+
82
+ await page.goto('http://localhost:3000')
83
+ await instant(page, async () => {
84
+ // ...
85
+ })`);
86
+ // Remove resolveURL and instant from the stack trace so the error
87
+ // points at the caller's code.
88
+ Error.captureStackTrace(error, instant);
89
+ throw error;
90
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@next/playwright",
3
+ "version": "16.2.0-canary.71",
4
+ "repository": {
5
+ "url": "vercel/next.js",
6
+ "directory": "packages/next-playwright"
7
+ },
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "license": "MIT",
14
+ "scripts": {
15
+ "build": "node ../../scripts/rm.mjs dist && tsc -d -p tsconfig.json",
16
+ "prepublishOnly": "cd ../../ && turbo run build",
17
+ "dev": "tsc -d -w -p tsconfig.json",
18
+ "typescript": "tsec --noEmit -p tsconfig.json"
19
+ },
20
+ "devDependencies": {
21
+ "typescript": "5.9.2"
22
+ }
23
+ }