Open History to see the last commit
1import { expect } from '@playwright/test'
2import { collectBrowserErrors, test } from '@tanstack/router-e2e-utils'
3β
4const buildUrl = (baseURL: string, pathname: string) =>
5 baseURL.replace(/\/$/, '') + pathname
6β
7async function getStyle(page: any, testId: string, property: string) {
8 return page.getByTestId(testId).evaluate((element: Element, prop: string) => {
9 return getComputedStyle(element).getPropertyValue(prop)
10 }, property)
11}
12β
13async function getInlineCssTexts(page: any) {
14 return page
15 .locator('style')
16 .evaluateAll((elements: Array<HTMLStyleElement>) =>
17 elements
18 .map((element) => element.textContent ?? '')
19 .filter((text) => text.includes('background')),
20 )
21}
22β
23async function waitForHydration(page: any) {
24 await page.waitForFunction(() => {
25 return (window as any).__CSS_INLINE_E2E_HYDRATED__ === true
26 })
27}
28β
29test('SSR inlines active nested route CSS without manifest stylesheet links', async ({
30 page,
31 request,
32 baseURL,
33}) => {
34 const errors: Array<string> = []
35 page.on('console', (message) => {
36 if (message.type() === 'error') {
37 errors.push(message.text())
38 }
39 })
40β
41 const url = buildUrl(baseURL!, '/app/dashboard')
42 const response = await request.get(url)
43 const html = await response.text()
44β
45 expect(response.ok()).toBe(true)
46 expect(html).toContain('data-testid="dashboard-card"')
47 expect(html).not.toContain('data-tanstack-router-inline-css')
48 expect(html).not.toContain('data-allow-mismatch')
49 expect(html).not.toMatch(/<link[^>]+rel="stylesheet"/)
50 expect(html).not.toContain('rel:"stylesheet"')
51 const styleMatches = Array.from(
52 html.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/g),
53 )
54 expect(styleMatches).toHaveLength(1)
55 const inlineCss = styleMatches[0]?.[1]
56 expect(inlineCss?.length).toBeGreaterThan(100)
57 expect(inlineCss).not.toContain('\n')
58 if (process.env.CSS_INLINE_TRANSFORM_ASSETS === 'true') {
59 expect(inlineCss).toContain('cdn=1')
60 } else {
61 expect(inlineCss).not.toContain('cdn=1')
62 }
63β
64 await page.goto(url)
65β
66 await expect(page.getByTestId('dashboard-card')).toBeVisible()
67 await expect(page.getByTestId('nested-panel')).toBeVisible()
68β
69 await expect
70 .poll(() => getStyle(page, 'shell', 'background-color'))
71 .toBe('rgb(240, 249, 255)')
72 await expect
73 .poll(() => getStyle(page, 'app-layout', 'background-color'))
74 .toBe('rgb(220, 252, 231)')
75 await expect
76 .poll(() => getStyle(page, 'dashboard-layout', 'border-left-color'))
77 .toBe('rgb(124, 58, 237)')
78 await expect
79 .poll(() => getStyle(page, 'dashboard-card', 'background-color'))
80 .toBe('rgb(254, 249, 195)')
81 await expect
82 .poll(() => getStyle(page, 'nested-panel', 'background-color'))
83 .toBe('rgb(219, 234, 254)')
84β
85 const backgroundImage = await getStyle(
86 page,
87 'dashboard-card',
88 'background-image',
89 )
90 expect(backgroundImage).not.toBe('none')
91β
92 const inlineStyleText = (await getInlineCssTexts(page))[0]
93 expect(inlineStyleText?.length).toBeGreaterThan(100)
94β
95 expect(errors.filter((error) => /hydration|Hydration/.test(error))).toEqual(
96 [],
97 )
98})
99β
100test('runtime request options can disable inline CSS', async ({
101 page,
102 request,
103 baseURL,
104}) => {
105 const url = buildUrl(baseURL!, '/app/dashboard')
106 const response = await request.get(url, {
107 headers: {
108 'x-inline-css': 'false',
109 },
110 })
111 const html = await response.text()
112β
113 expect(response.ok()).toBe(true)
114 expect(html).toMatch(/<link[^>]+rel="stylesheet"/)
115 expect(html).not.toMatch(/<style\b[^>]*>[\s\S]*background[\s\S]*<\/style>/)
116β
117 await page.setExtraHTTPHeaders({
118 'x-inline-css': 'false',
119 })
120 await page.goto(url)
121β
122 await expect(page.getByTestId('dashboard-card')).toBeVisible()
123 await expect.poll(() => getInlineCssTexts(page)).toHaveLength(0)
124 await expect
125 .poll(() => getStyle(page, 'dashboard-card', 'background-color'))
126 .toBe('rgb(254, 249, 195)')
127})
128β
129test('client navigation keeps nested CSS module styles applied', async ({
130 page,
131 baseURL,
132}) => {
133 await page.goto(buildUrl(baseURL!, '/'))
134 await waitForHydration(page)
135 await page.getByTestId('nav-details').click()
136 await page.waitForURL('**/app/dashboard/details')
137β
138 await expect(page.getByTestId('details-card')).toBeVisible()
139 await expect
140 .poll(() => getStyle(page, 'details-card', 'background-color'))
141 .toBe('rgb(255, 237, 213)')
142 await expect
143 .poll(() => getStyle(page, 'nested-panel', 'border-top-color'))
144 .toBe('rgb(37, 99, 235)')
145})
146β
147test('client navigation preserves the SSR inline shell stylesheet', async ({
148 page,
149 baseURL,
150}) => {
151 await page.goto(buildUrl(baseURL!, '/'))
152 await waitForHydration(page)
153β
154 const inlineStyle = page.locator('style[data-tsr-inline-css]')
155 await expect(inlineStyle).toHaveCount(1)
156 const original = await inlineStyle.evaluateHandle(
157 (style: HTMLStyleElement) => {
158 const mutations: Array<MutationRecord> = []
159 const observer = new MutationObserver((records) => {
160 mutations.push(...records)
161 })
162 observer.observe(style, {
163 childList: true,
164 characterData: true,
165 subtree: true,
166 })
167 return { style, sheet: style.sheet, observer, mutations }
168 },
169 )
170β
171 await expect.poll(() => getInlineCssTexts(page)).toHaveLength(1)
172 await expect
173 .poll(() => getInlineCssTexts(page))
174 .toEqual([expect.stringContaining('background')])
175 await expect
176 .poll(() => getStyle(page, 'shell', 'background-color'))
177 .toBe('rgb(240, 249, 255)')
178β
179 await page.getByTestId('nav-dashboard').click()
180 await page.waitForURL('**/app/dashboard')
181 await expect(page).toHaveTitle('Inline CSS dashboard')
182 await expect(page.getByTestId('dashboard-card')).toBeVisible()
183β
184 await page.getByTestId('nav-home').click()
185 await expect(page).toHaveTitle('Inline CSS E2E')
186 await expect(page.getByTestId('home')).toBeVisible()
187β
188 // #8250: unchanged CSS text can hide a redundant innerHTML write that
189 // replaces the stylesheet or fails under Trusted Types. Observe native DOM
190 // mutations and CSSOM identity without patching the innerHTML setter.
191 expect(
192 await original.evaluate(({ style, sheet, observer, mutations }) => {
193 const mutationCount = mutations.length + observer.takeRecords().length
194 observer.disconnect()
195 return {
196 connected: style.isConnected,
197 sameSheet: style.sheet === sheet,
198 mutationCount,
199 }
200 }),
201 ).toEqual({ connected: true, sameSheet: true, mutationCount: 0 })
202 await original.dispose()
203β
204 await expect.poll(() => getInlineCssTexts(page)).toHaveLength(1)
205 await expect
206 .poll(() => getInlineCssTexts(page))
207 .toEqual([expect.stringContaining('background')])
208 await expect
209 .poll(() => getStyle(page, 'shell', 'background-color'))
210 .toBe('rgb(240, 249, 255)')
211})
212β
213test('client navigation preserves inline CSS with Trusted Types enforced', async ({
214 page,
215}) => {
216 const browserErrors = collectBrowserErrors(page)
217 const csp = "require-trusted-types-for 'script'; trusted-types 'none'"
218 await page.route('/', async (route) => {
219 const response = await route.fetch()
220 await route.fulfill({
221 response,
222 headers: { ...response.headers(), 'content-security-policy': csp },
223 })
224 })
225β
226 const response = await page.goto('/')
227 expect(response?.headers()['content-security-policy']).toBe(csp)
228 expect(await page.evaluate(() => 'trustedTypes' in window)).toBe(true)
229 await waitForHydration(page)
230β
231 const violations = await page.evaluateHandle(() => {
232 const directives: Array<string> = []
233 document.addEventListener('securitypolicyviolation', (event) => {
234 directives.push(event.effectiveDirective)
235 })
236 return directives
237 })
238β
239 await page.getByTestId('nav-dashboard').click()
240 await expect(page).toHaveTitle('Inline CSS dashboard')
241 await expect(page.getByTestId('dashboard-card')).toBeVisible()
242 await expect
243 .poll(() => getStyle(page, 'shell', 'background-color'))
244 .toBe('rgb(240, 249, 255)')
245β
246 await page.getByTestId('nav-home').click()
247 await expect(page).toHaveTitle('Inline CSS E2E')
248 await expect(page.getByTestId('home')).toBeVisible()
249β
250 expect(await violations.jsonValue()).toEqual([])
251 expect(browserErrors).toEqual([])
252 await violations.dispose()
253})