Skip to content

Commit 97d9f30

Browse files
feat: remove ClientOnly children from server build (#6193)
1 parent f5232a5 commit 97d9f30

23 files changed

Lines changed: 526 additions & 5 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* This component accesses `window` at module scope, which would throw
3+
* if this module is ever imported on the server. The compiler optimization
4+
* for <ClientOnly> ensures this module is DCE'd from the server bundle
5+
* entirely, preventing the error.
6+
*/
7+
8+
// This throws at module import time on the server since `window` doesn't exist
9+
const initialWidth = window.innerWidth
10+
const initialHeight = window.innerHeight
11+
12+
export function WindowSize() {
13+
return (
14+
<div data-testid="window-size">
15+
<p data-testid="window-width">Window width: {initialWidth}</p>
16+
<p data-testid="window-height">Window height: {initialHeight}</p>
17+
</div>
18+
)
19+
}

‎e2e/react-start/basic/src/routeTree.gen.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Route as PostsRouteImport } from './routes/posts'
1717
import { Route as LinksRouteImport } from './routes/links'
1818
import { Route as InlineScriptsRouteImport } from './routes/inline-scripts'
1919
import { Route as DeferredRouteImport } from './routes/deferred'
20+
import { Route as ClientOnlyRouteImport } from './routes/client-only'
2021
import { Route as LayoutRouteImport } from './routes/_layout'
2122
import { Route as SearchParamsRouteRouteImport } from './routes/search-params/route'
2223
import { Route as NotFoundRouteRouteImport } from './routes/not-found/route'
@@ -92,6 +93,11 @@ const DeferredRoute = DeferredRouteImport.update({
9293
path: '/deferred',
9394
getParentRoute: () => rootRouteImport,
9495
} as any)
96+
const ClientOnlyRoute = ClientOnlyRouteImport.update({
97+
id: '/client-only',
98+
path: '/client-only',
99+
getParentRoute: () => rootRouteImport,
100+
} as any)
95101
const LayoutRoute = LayoutRouteImport.update({
96102
id: '/_layout',
97103
getParentRoute: () => rootRouteImport,
@@ -268,6 +274,7 @@ export interface FileRoutesByFullPath {
268274
'/': typeof IndexRoute
269275
'/not-found': typeof NotFoundRouteRouteWithChildren
270276
'/search-params': typeof SearchParamsRouteRouteWithChildren
277+
'/client-only': typeof ClientOnlyRoute
271278
'/deferred': typeof DeferredRoute
272279
'/inline-scripts': typeof InlineScriptsRoute
273280
'/links': typeof LinksRoute
@@ -307,6 +314,7 @@ export interface FileRoutesByFullPath {
307314
}
308315
export interface FileRoutesByTo {
309316
'/': typeof IndexRoute
317+
'/client-only': typeof ClientOnlyRoute
310318
'/deferred': typeof DeferredRoute
311319
'/inline-scripts': typeof InlineScriptsRoute
312320
'/links': typeof LinksRoute
@@ -346,6 +354,7 @@ export interface FileRoutesById {
346354
'/not-found': typeof NotFoundRouteRouteWithChildren
347355
'/search-params': typeof SearchParamsRouteRouteWithChildren
348356
'/_layout': typeof LayoutRouteWithChildren
357+
'/client-only': typeof ClientOnlyRoute
349358
'/deferred': typeof DeferredRoute
350359
'/inline-scripts': typeof InlineScriptsRoute
351360
'/links': typeof LinksRoute
@@ -390,6 +399,7 @@ export interface FileRouteTypes {
390399
| '/'
391400
| '/not-found'
392401
| '/search-params'
402+
| '/client-only'
393403
| '/deferred'
394404
| '/inline-scripts'
395405
| '/links'
@@ -429,6 +439,7 @@ export interface FileRouteTypes {
429439
fileRoutesByTo: FileRoutesByTo
430440
to:
431441
| '/'
442+
| '/client-only'
432443
| '/deferred'
433444
| '/inline-scripts'
434445
| '/links'
@@ -467,6 +478,7 @@ export interface FileRouteTypes {
467478
| '/not-found'
468479
| '/search-params'
469480
| '/_layout'
481+
| '/client-only'
470482
| '/deferred'
471483
| '/inline-scripts'
472484
| '/links'
@@ -511,6 +523,7 @@ export interface RootRouteChildren {
511523
NotFoundRouteRoute: typeof NotFoundRouteRouteWithChildren
512524
SearchParamsRouteRoute: typeof SearchParamsRouteRouteWithChildren
513525
LayoutRoute: typeof LayoutRouteWithChildren
526+
ClientOnlyRoute: typeof ClientOnlyRoute
514527
DeferredRoute: typeof DeferredRoute
515528
InlineScriptsRoute: typeof InlineScriptsRoute
516529
LinksRoute: typeof LinksRoute
@@ -586,6 +599,13 @@ declare module '@tanstack/react-router' {
586599
preLoaderRoute: typeof DeferredRouteImport
587600
parentRoute: typeof rootRouteImport
588601
}
602+
'/client-only': {
603+
id: '/client-only'
604+
path: '/client-only'
605+
fullPath: '/client-only'
606+
preLoaderRoute: typeof ClientOnlyRouteImport
607+
parentRoute: typeof rootRouteImport
608+
}
589609
'/_layout': {
590610
id: '/_layout'
591611
path: ''
@@ -955,6 +975,7 @@ const rootRouteChildren: RootRouteChildren = {
955975
NotFoundRouteRoute: NotFoundRouteRouteWithChildren,
956976
SearchParamsRouteRoute: SearchParamsRouteRouteWithChildren,
957977
LayoutRoute: LayoutRouteWithChildren,
978+
ClientOnlyRoute: ClientOnlyRoute,
958979
DeferredRoute: DeferredRoute,
959980
InlineScriptsRoute: InlineScriptsRoute,
960981
LinksRoute: LinksRoute,

‎e2e/react-start/basic/src/routes/__root.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ function RootDocument({ children }: { children: React.ReactNode }) {
165165
>
166166
redirect
167167
</Link>{' '}
168+
<Link
169+
to="/client-only"
170+
activeProps={{
171+
className: 'font-bold',
172+
}}
173+
>
174+
Client Only
175+
</Link>{' '}
168176
<Link
169177
// @ts-expect-error
170178
to="/this-route-does-not-exist"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ClientOnly, createFileRoute } from '@tanstack/react-router'
2+
import { WindowSize } from '~/components/WindowSize'
3+
4+
export const Route = createFileRoute('/client-only')({
5+
component: ClientOnlyPage,
6+
})
7+
8+
function ClientOnlyPage() {
9+
return (
10+
<div className="p-2">
11+
<h3 data-testid="client-only-heading">Client Only Demo</h3>
12+
<p>
13+
The component below uses <code>window</code> APIs that only exist in the
14+
browser.
15+
</p>
16+
<ClientOnly
17+
fallback={
18+
<div data-testid="loading-fallback">Loading window size...</div>
19+
}
20+
>
21+
<WindowSize />
22+
</ClientOnly>
23+
</div>
24+
)
25+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { expect } from '@playwright/test'
2+
import { test } from '@tanstack/router-e2e-utils'
3+
4+
/**
5+
* These tests verify the <ClientOnly> compiler optimization.
6+
*
7+
* The WindowSize component accesses `window` at module scope, which would
8+
* throw if the module is ever imported on the server. The compiler optimization
9+
* strips <ClientOnly> children from the server bundle, allowing DCE to remove
10+
* the WindowSize import entirely. This prevents the server from crashing.
11+
*
12+
* If these tests pass in SSR mode, it proves the compiler is correctly
13+
* removing client-only code from the server bundle.
14+
*/
15+
16+
test('ClientOnly renders fallback on server, then client content after hydration', async ({
17+
page,
18+
}) => {
19+
// Navigate directly to the client-only route
20+
// If the compiler optimization isn't working, this would crash the server
21+
// because WindowSize.tsx accesses `window` at module scope
22+
await page.goto('/client-only')
23+
await page.waitForURL('/client-only')
24+
25+
// The heading should be visible
26+
await expect(page.getByTestId('client-only-heading')).toContainText(
27+
'Client Only Demo',
28+
)
29+
30+
// After hydration, the WindowSize component should render with actual values
31+
// Wait for the client-side component to render
32+
await expect(page.getByTestId('window-size')).toBeVisible()
33+
await expect(page.getByTestId('window-width')).toContainText('Window width:')
34+
await expect(page.getByTestId('window-height')).toContainText(
35+
'Window height:',
36+
)
37+
})
38+
39+
test('ClientOnly works with client-side navigation', async ({ page }) => {
40+
// Start from home
41+
await page.goto('/')
42+
await page.waitForURL('/')
43+
44+
// Navigate to client-only route via client-side navigation
45+
await page.getByRole('link', { name: 'Client Only' }).click()
46+
await page.waitForURL('/client-only')
47+
48+
// The WindowSize component should render
49+
await expect(page.getByTestId('window-size')).toBeVisible()
50+
await expect(page.getByTestId('window-width')).toContainText('Window width:')
51+
})
52+
53+
test('ClientOnly component displays actual window dimensions', async ({
54+
page,
55+
}) => {
56+
// Set a specific viewport size
57+
await page.setViewportSize({ width: 800, height: 600 })
58+
59+
await page.goto('/client-only')
60+
await page.waitForURL('/client-only')
61+
62+
// Wait for client-side hydration
63+
await expect(page.getByTestId('window-size')).toBeVisible()
64+
65+
// Check that the displayed dimensions match the viewport
66+
await expect(page.getByTestId('window-width')).toContainText(
67+
'Window width: 800',
68+
)
69+
await expect(page.getByTestId('window-height')).toContainText(
70+
'Window height: 600',
71+
)
72+
})

0 commit comments

Comments
 (0)