Skip to content

Commit 944b155

Browse files
authored
fix(solid-router): hydration mismatch for ssr='data-only' with pendingComponent (#7266)
1 parent 65874cd commit 944b155

7 files changed

Lines changed: 125 additions & 6 deletions

File tree

‎.changeset/tangy-falcons-hang.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/solid-router': patch
3+
---
4+
5+
Fix hydration mismatch for ssr='data-only' with pendingComponent

‎e2e/e2e-utils/src/fixture.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { test as base, expect } from '@playwright/test'
2+
import type { Page } from '@playwright/test'
23

34
export interface TestFixtureOptions {
45
/**
@@ -46,3 +47,25 @@ export const test = base.extend<TestFixtureOptions>({
4647
expect(errorMessages).toEqual([])
4748
},
4849
})
50+
51+
export function collectBrowserErrors(page: Page): Array<string> {
52+
const browserErrors: Array<string> = []
53+
54+
page.on('pageerror', (error) => {
55+
browserErrors.push(error.message)
56+
})
57+
58+
page.on('console', (message) => {
59+
if (message.type() === 'error' || message.type() === 'warning') {
60+
browserErrors.push(message.text())
61+
}
62+
})
63+
64+
page.on('requestfailed', (request) => {
65+
browserErrors.push(
66+
`${request.url()} ${request.failure()?.errorText ?? 'failed'}`,
67+
)
68+
})
69+
70+
return browserErrors
71+
}

‎e2e/e2e-utils/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export { resolveRuntimeSuffix } from './resolve-runtime-suffix'
55
export { e2eStartDummyServer, e2eStopDummyServer } from './e2eSetupTeardown'
66
export { preOptimizeDevServer, waitForServer } from './devServerWarmup'
77
export type { Post } from './posts'
8-
export { test } from './fixture'
8+
export { collectBrowserErrors, test } from './fixture'

‎e2e/solid-start/selective-ssr/src/routeTree.gen.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import { Route as rootRouteImport } from './routes/__root'
1212
import { Route as PostsRouteImport } from './routes/posts'
13+
import { Route as DataOnlyPendingComponentRouteImport } from './routes/data-only-pending-component'
1314
import { Route as IndexRouteImport } from './routes/index'
1415
import { Route as PostsPostIdRouteImport } from './routes/posts.$postId'
1516

@@ -18,6 +19,12 @@ const PostsRoute = PostsRouteImport.update({
1819
path: '/posts',
1920
getParentRoute: () => rootRouteImport,
2021
} as any)
22+
const DataOnlyPendingComponentRoute =
23+
DataOnlyPendingComponentRouteImport.update({
24+
id: '/data-only-pending-component',
25+
path: '/data-only-pending-component',
26+
getParentRoute: () => rootRouteImport,
27+
} as any)
2128
const IndexRoute = IndexRouteImport.update({
2229
id: '/',
2330
path: '/',
@@ -31,30 +38,39 @@ const PostsPostIdRoute = PostsPostIdRouteImport.update({
3138

3239
export interface FileRoutesByFullPath {
3340
'/': typeof IndexRoute
41+
'/data-only-pending-component': typeof DataOnlyPendingComponentRoute
3442
'/posts': typeof PostsRouteWithChildren
3543
'/posts/$postId': typeof PostsPostIdRoute
3644
}
3745
export interface FileRoutesByTo {
3846
'/': typeof IndexRoute
47+
'/data-only-pending-component': typeof DataOnlyPendingComponentRoute
3948
'/posts': typeof PostsRouteWithChildren
4049
'/posts/$postId': typeof PostsPostIdRoute
4150
}
4251
export interface FileRoutesById {
4352
__root__: typeof rootRouteImport
4453
'/': typeof IndexRoute
54+
'/data-only-pending-component': typeof DataOnlyPendingComponentRoute
4555
'/posts': typeof PostsRouteWithChildren
4656
'/posts/$postId': typeof PostsPostIdRoute
4757
}
4858
export interface FileRouteTypes {
4959
fileRoutesByFullPath: FileRoutesByFullPath
50-
fullPaths: '/' | '/posts' | '/posts/$postId'
60+
fullPaths: '/' | '/data-only-pending-component' | '/posts' | '/posts/$postId'
5161
fileRoutesByTo: FileRoutesByTo
52-
to: '/' | '/posts' | '/posts/$postId'
53-
id: '__root__' | '/' | '/posts' | '/posts/$postId'
62+
to: '/' | '/data-only-pending-component' | '/posts' | '/posts/$postId'
63+
id:
64+
| '__root__'
65+
| '/'
66+
| '/data-only-pending-component'
67+
| '/posts'
68+
| '/posts/$postId'
5469
fileRoutesById: FileRoutesById
5570
}
5671
export interface RootRouteChildren {
5772
IndexRoute: typeof IndexRoute
73+
DataOnlyPendingComponentRoute: typeof DataOnlyPendingComponentRoute
5874
PostsRoute: typeof PostsRouteWithChildren
5975
}
6076

@@ -67,6 +83,13 @@ declare module '@tanstack/solid-router' {
6783
preLoaderRoute: typeof PostsRouteImport
6884
parentRoute: typeof rootRouteImport
6985
}
86+
'/data-only-pending-component': {
87+
id: '/data-only-pending-component'
88+
path: '/data-only-pending-component'
89+
fullPath: '/data-only-pending-component'
90+
preLoaderRoute: typeof DataOnlyPendingComponentRouteImport
91+
parentRoute: typeof rootRouteImport
92+
}
7093
'/': {
7194
id: '/'
7295
path: '/'
@@ -96,6 +119,7 @@ const PostsRouteWithChildren = PostsRoute._addFileChildren(PostsRouteChildren)
96119

97120
const rootRouteChildren: RootRouteChildren = {
98121
IndexRoute: IndexRoute,
122+
DataOnlyPendingComponentRoute: DataOnlyPendingComponentRoute,
99123
PostsRoute: PostsRouteWithChildren,
100124
}
101125
export const routeTree = rootRouteImport
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createFileRoute } from '@tanstack/solid-router'
2+
import { z } from 'zod'
3+
import { ssrSchema } from '~/search'
4+
5+
export const Route = createFileRoute('/data-only-pending-component')({
6+
validateSearch: z.object({ root: ssrSchema }),
7+
ssr: 'data-only',
8+
loader: async () => {
9+
await new Promise((resolve) => setTimeout(resolve, 1500))
10+
return { loadedAt: new Date().toISOString() }
11+
},
12+
pendingComponent: () => (
13+
<div data-testid="data-only-pending-component-pending" />
14+
),
15+
component: DataOnlyPendingComponentRoute,
16+
})
17+
18+
function DataOnlyPendingComponentRoute() {
19+
const data = Route.useLoaderData()
20+
21+
return (
22+
<div data-testid="data-only-pending-component-ready">
23+
<p data-testid="data-only-pending-component-ready-label">
24+
OK - loader finished
25+
</p>
26+
<pre data-testid="data-only-pending-component-ready-data">
27+
{JSON.stringify(data(), null, 2)}
28+
</pre>
29+
</div>
30+
)
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from '@playwright/test'
2+
import { collectBrowserErrors, test } from '@tanstack/router-e2e-utils'
3+
4+
test.describe('pending component hydration', () => {
5+
test('data-only route hydrates from pending element to loaded state on first load', async ({
6+
page,
7+
}) => {
8+
const browserErrors = collectBrowserErrors(page)
9+
10+
await page.goto('/data-only-pending-component')
11+
12+
await expect(page).toHaveURL(/data-only-pending-component/)
13+
14+
await expect(
15+
page.getByTestId('data-only-pending-component-pending'),
16+
).toBeAttached()
17+
18+
await expect(
19+
page.getByTestId('data-only-pending-component-ready-label'),
20+
).toHaveText('OK - loader finished', { timeout: 10_000 })
21+
22+
expect(browserErrors).toEqual([])
23+
expect(browserErrors).not.toContainEqual(
24+
expect.stringContaining('template is not a function'),
25+
)
26+
expect(browserErrors).not.toContainEqual(
27+
expect.stringContaining('Hydration Mismatch'),
28+
)
29+
})
30+
})

‎packages/solid-router/src/Match.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ export const Match = (props: { matchId: string }) => {
8686
currentMatchState().ssr === false ||
8787
currentMatchState().ssr === 'data-only'
8888

89+
const shouldSkipSuspenseFallback =
90+
(isServer ?? router.isServer)
91+
? resolvedNoSsr
92+
: currentMatchState().ssr === 'data-only'
93+
8994
const ResolvedSuspenseBoundary = () => Solid.Suspense
9095

9196
const ResolvedCatchBoundary = () =>
@@ -105,8 +110,9 @@ export const Match = (props: { matchId: string }) => {
105110
<Dynamic
106111
component={ResolvedSuspenseBoundary()}
107112
fallback={
108-
// Don't show fallback on server when using no-ssr mode to avoid hydration mismatch
109-
(isServer ?? router.isServer) && resolvedNoSsr ? undefined : (
113+
// Data-only SSR renders the inner fallback on the server, so
114+
// avoid adding an extra suspense fallback on the client.
115+
shouldSkipSuspenseFallback ? undefined : (
110116
<Dynamic component={resolvePendingComponent()} />
111117
)
112118
}

0 commit comments

Comments
 (0)