Open History to see the last commit
1import { expect, test } from '@playwright/test'
2β
3test('concurrent SSR requests keep the same query key isolated by request', async ({
4 request,
5}) => {
6 const names = ['Lucia', 'Omar', 'Maya', 'Felix']
7 const responses = await Promise.all(
8 names.map((name) =>
9 request.get('/preferences', {
10 headers: { Cookie: `reader-name=${name}` },
11 }),
12 ),
13 )
14 for (const [index, response] of responses.entries()) {
15 expect(response.status()).toBe(200)
16 expect(response.headers()['cache-control']).toContain('no-store')
17 const html = await response.text()
18 expect(html).toContain(names[index])
19 for (const other of names.filter((_, otherIndex) => otherIndex !== index)) {
20 expect(html).not.toContain(other)
21 }
22 }
23})
24β
25test('hydration reuses SSR data and mutation invalidation refetches once', async ({
26 page,
27}) => {
28 const reads: string[] = []
29 page.on('request', (request) => {
30 if (
31 request.method() === 'GET' &&
32 new URL(request.url()).pathname.startsWith('/_serverFn/')
33 ) {
34 reads.push(request.url())
35 }
36 })
37 await page.goto('/preferences')
38 await expect(page.getByTestId('reader-name')).toHaveText('Hello, Guest')
39 await expect(page.getByLabel('Display name')).toBeEnabled()
40 await page.waitForLoadState('networkidle')
41 expect(reads).toHaveLength(0)
42β
43 await page.getByLabel('Display name').fill('Ada')
44 const mutation = page.waitForRequest((request) => request.method() === 'POST')
45 await page.getByRole('button', { name: 'Save name' }).click()
46 await expect(page.getByTestId('reader-name')).toHaveText('Hello, Ada')
47 await expect(page.getByRole('button', { name: 'Save name' })).toBeEnabled()
48 await page.waitForLoadState('networkidle')
49 expect(reads).toHaveLength(1)
50β
51 const savedRequest = await mutation
52 const headers = await savedRequest.allHeaders()
53 delete headers['content-length']
54 headers.origin = 'https://untrusted.example'
55 headers['sec-fetch-site'] = 'cross-site'
56 const denied = await page.request.post(savedRequest.url(), {
57 headers,
58 data: savedRequest.postData() ?? '',
59 })
60 expect(denied.status()).toBe(403)
61β
62 await page.reload()
63 await expect(page.getByTestId('reader-name')).toHaveText('Hello, Ada')
64 await expect(page.getByLabel('Display name')).toBeEnabled()
65 await page.waitForLoadState('networkidle')
66 expect(reads).toHaveLength(1)
67})