Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

script-duplication.spec.ts

186 lines6.8 KB

1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3​
4test.describe('Async Script Hydration', () => {
5 test('should not show hydration warning for async scripts', async ({
6 page,
7 }) => {
8 await page.goto('/async-scripts')
9 await expect(
10 page.getByTestId('async-scripts-test-heading'),
11 ).toBeInViewport()
12​
13 await page.waitForFunction(() => (window as any).SCRIPT_1 === true)
14 })
15​
16 test('should load async and defer scripts correctly', async ({ page }) => {
17 await page.goto('/async-scripts')
18 await expect(
19 page.getByTestId('async-scripts-test-heading'),
20 ).toBeInViewport()
21​
22 // script.js (async) sets window.SCRIPT_1 = true
23 // script2.js (defer) sets window.SCRIPT_2 = true
24 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
25 expect(await page.evaluate('window.SCRIPT_2')).toBe(true)
26 })
27})
28​
29test.describe('Script Duplication Prevention', () => {
30 test('should not create duplicate scripts on SSR route', async ({ page }) => {
31 await page.goto('/scripts')
32​
33 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
34​
35 // Wait for the script to execute β€” React 19 hoists <script src> as a
36 // resource during SSR hydration, so the DOM element may not persist in
37 // a queryable form. Execution is the reliable check.
38 await page.waitForFunction(() => (window as any).SCRIPT_1 === true)
39​
40 const scriptCount = await page.evaluate(() => {
41 return document.querySelectorAll('script[src="/script.js"]').length
42 })
43 expect(scriptCount).toBeLessThanOrEqual(1)
44​
45 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
46 })
47​
48 test('should not create duplicate scripts during client-side navigation', async ({
49 page,
50 }) => {
51 await page.goto('/')
52​
53 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
54 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
55​
56 const firstNavCount = await page.evaluate(() => {
57 return document.querySelectorAll('script[src="script.js"]').length
58 })
59 expect(firstNavCount).toBe(1)
60​
61 await page.getByRole('link', { name: 'Home' }).click()
62 await expect(page.getByRole('link', { name: 'Posts' })).toBeVisible()
63​
64 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
65 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
66​
67 const secondNavCount = await page.evaluate(() => {
68 return document.querySelectorAll('script[src="script.js"]').length
69 })
70 expect(secondNavCount).toBe(1)
71​
72 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
73 })
74​
75 test('should not create duplicate scripts with multiple navigation cycles', async ({
76 page,
77 }) => {
78 await page.goto('/')
79​
80 for (let i = 0; i < 3; i++) {
81 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
82 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
83​
84 await page.getByRole('link', { name: 'Home' }).click()
85 await expect(page.getByRole('link', { name: 'Posts' })).toBeVisible()
86 }
87​
88 await page.getByRole('link', { name: 'Scripts', exact: true }).click()
89 await expect(page.getByTestId('scripts-test-heading')).toBeInViewport()
90​
91 const finalCount = await page.evaluate(() => {
92 return document.querySelectorAll('script[src="script.js"]').length
93 })
94 expect(finalCount).toBe(1)
95​
96 expect(await page.evaluate('window.SCRIPT_1')).toBe(true)
97 })
98​
99 test('should not create duplicate inline scripts', async ({ page }) => {
100 await page.goto('/inline-scripts')
101​
102 await expect(
103 page.getByTestId('inline-scripts-test-heading'),
104 ).toBeInViewport()
105​
106 // Wait for scripts to execute β€” useEffect may need a tick after hydration.
107 // React 19 may hoist inline scripts as resources, so the DOM element may
108 // not persist; script execution is the reliable check for SSR routes.
109 await page.waitForFunction(() => (window as any).INLINE_SCRIPT_1 === true)
110 await page.waitForFunction(() => (window as any).INLINE_SCRIPT_2 === 'test')
111​
112 // React 19 can hoist/dedupe <script> tags during hydration. Between that
113 // and TanStack Router's client-side imperative injection (which may
114 // intentionally skip injection if a matching script already exists), the
115 // resulting script node might not be consistently queryable in a single
116 // fixed place.
117 //
118 // What we *can* assert reliably for SSR routes is "not duplicated" (<= 1),
119 // not "exactly one".
120 const inlineScript1Count = await page.evaluate(() => {
121 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
122 return scripts.filter(
123 (script) =>
124 script.textContent &&
125 script.textContent.includes('window.INLINE_SCRIPT_1 = true'),
126 ).length
127 })
128 expect(inlineScript1Count).toBeLessThanOrEqual(1)
129​
130 const inlineScript2Count = await page.evaluate(() => {
131 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
132 return scripts.filter(
133 (script) =>
134 script.textContent &&
135 script.textContent.includes('window.INLINE_SCRIPT_2 = "test"'),
136 ).length
137 })
138 expect(inlineScript2Count).toBeLessThanOrEqual(1)
139​
140 expect(await page.evaluate('window.INLINE_SCRIPT_1')).toBe(true)
141 expect(await page.evaluate('window.INLINE_SCRIPT_2')).toBe('test')
142 })
143​
144 test('should not create duplicate inline scripts during client-side navigation', async ({
145 page,
146 }) => {
147 await page.goto('/')
148​
149 await page.getByRole('link', { name: 'Inline Scripts' }).click()
150 await expect(
151 page.getByTestId('inline-scripts-test-heading'),
152 ).toBeInViewport()
153​
154 const firstNavScript1Count = await page.evaluate(() => {
155 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
156 return scripts.filter(
157 (script) =>
158 script.textContent &&
159 script.textContent.includes('window.INLINE_SCRIPT_1 = true'),
160 ).length
161 })
162 expect(firstNavScript1Count).toBe(1)
163​
164 await page.getByRole('link', { name: 'Home' }).click()
165 await expect(page.getByRole('link', { name: 'Posts' })).toBeVisible()
166​
167 await page.getByRole('link', { name: 'Inline Scripts' }).click()
168 await expect(
169 page.getByTestId('inline-scripts-test-heading'),
170 ).toBeInViewport()
171​
172 const secondNavScript1Count = await page.evaluate(() => {
173 const scripts = Array.from(document.querySelectorAll('script:not([src])'))
174 return scripts.filter(
175 (script) =>
176 script.textContent &&
177 script.textContent.includes('window.INLINE_SCRIPT_1 = true'),
178 ).length
179 })
180 expect(secondNavScript1Count).toBe(1)
181​
182 // Verify the scripts are still working
183 expect(await page.evaluate('window.INLINE_SCRIPT_1')).toBe(true)
184 expect(await page.evaluate('window.INLINE_SCRIPT_2')).toBe('test')
185 })
186})