Skip to content

Commit 7fa0f39

Browse files
fix: fix exports for react-start so useServerFn is available with RSC (#7292)
Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
1 parent a730094 commit 7fa0f39

7 files changed

Lines changed: 87 additions & 4 deletions

File tree

‎.changeset/angry-flies-taste.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/react-start': patch
3+
---
4+
5+
fix exports for react-start so useServerFn is available with RSC

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
1010

1111
import { Route as rootRouteImport } from './routes/__root'
12+
import { Route as RscUseServerFnRouteImport } from './routes/rsc-use-server-fn'
1213
import { Route as RscTreeRouteImport } from './routes/rsc-tree'
1314
import { Route as RscSuspenseRouteImport } from './routes/rsc-suspense'
1415
import { Route as RscStreamingRouteImport } from './routes/rsc-streaming'
@@ -55,6 +56,11 @@ import { Route as RscParamIdRouteImport } from './routes/rsc-param/$id'
5556
import { Route as RscCssConditionalBranchRouteImport } from './routes/rsc-css-conditional.$branch'
5657
import { Route as ApiRscFlightRouteImport } from './routes/api.rsc-flight'
5758

59+
const RscUseServerFnRoute = RscUseServerFnRouteImport.update({
60+
id: '/rsc-use-server-fn',
61+
path: '/rsc-use-server-fn',
62+
getParentRoute: () => rootRouteImport,
63+
} as any)
5864
const RscTreeRoute = RscTreeRouteImport.update({
5965
id: '/rsc-tree',
6066
path: '/rsc-tree',
@@ -322,6 +328,7 @@ export interface FileRoutesByFullPath {
322328
'/rsc-streaming': typeof RscStreamingRoute
323329
'/rsc-suspense': typeof RscSuspenseRoute
324330
'/rsc-tree': typeof RscTreeRoute
331+
'/rsc-use-server-fn': typeof RscUseServerFnRoute
325332
'/api/rsc-flight': typeof ApiRscFlightRoute
326333
'/rsc-css-conditional/$branch': typeof RscCssConditionalBranchRoute
327334
'/rsc-param/$id': typeof RscParamIdRoute
@@ -369,6 +376,7 @@ export interface FileRoutesByTo {
369376
'/rsc-streaming': typeof RscStreamingRoute
370377
'/rsc-suspense': typeof RscSuspenseRoute
371378
'/rsc-tree': typeof RscTreeRoute
379+
'/rsc-use-server-fn': typeof RscUseServerFnRoute
372380
'/api/rsc-flight': typeof ApiRscFlightRoute
373381
'/rsc-css-conditional/$branch': typeof RscCssConditionalBranchRoute
374382
'/rsc-param/$id': typeof RscParamIdRoute
@@ -417,6 +425,7 @@ export interface FileRoutesById {
417425
'/rsc-streaming': typeof RscStreamingRoute
418426
'/rsc-suspense': typeof RscSuspenseRoute
419427
'/rsc-tree': typeof RscTreeRoute
428+
'/rsc-use-server-fn': typeof RscUseServerFnRoute
420429
'/api/rsc-flight': typeof ApiRscFlightRoute
421430
'/rsc-css-conditional/$branch': typeof RscCssConditionalBranchRoute
422431
'/rsc-param/$id': typeof RscParamIdRoute
@@ -466,6 +475,7 @@ export interface FileRouteTypes {
466475
| '/rsc-streaming'
467476
| '/rsc-suspense'
468477
| '/rsc-tree'
478+
| '/rsc-use-server-fn'
469479
| '/api/rsc-flight'
470480
| '/rsc-css-conditional/$branch'
471481
| '/rsc-param/$id'
@@ -513,6 +523,7 @@ export interface FileRouteTypes {
513523
| '/rsc-streaming'
514524
| '/rsc-suspense'
515525
| '/rsc-tree'
526+
| '/rsc-use-server-fn'
516527
| '/api/rsc-flight'
517528
| '/rsc-css-conditional/$branch'
518529
| '/rsc-param/$id'
@@ -560,6 +571,7 @@ export interface FileRouteTypes {
560571
| '/rsc-streaming'
561572
| '/rsc-suspense'
562573
| '/rsc-tree'
574+
| '/rsc-use-server-fn'
563575
| '/api/rsc-flight'
564576
| '/rsc-css-conditional/$branch'
565577
| '/rsc-param/$id'
@@ -608,6 +620,7 @@ export interface RootRouteChildren {
608620
RscStreamingRoute: typeof RscStreamingRoute
609621
RscSuspenseRoute: typeof RscSuspenseRoute
610622
RscTreeRoute: typeof RscTreeRoute
623+
RscUseServerFnRoute: typeof RscUseServerFnRoute
611624
ApiRscFlightRoute: typeof ApiRscFlightRoute
612625
RscCssConditionalBranchRoute: typeof RscCssConditionalBranchRoute
613626
RscParamIdRoute: typeof RscParamIdRoute
@@ -617,6 +630,13 @@ export interface RootRouteChildren {
617630

618631
declare module '@tanstack/react-router' {
619632
interface FileRoutesByPath {
633+
'/rsc-use-server-fn': {
634+
id: '/rsc-use-server-fn'
635+
path: '/rsc-use-server-fn'
636+
fullPath: '/rsc-use-server-fn'
637+
preLoaderRoute: typeof RscUseServerFnRouteImport
638+
parentRoute: typeof rootRouteImport
639+
}
620640
'/rsc-tree': {
621641
id: '/rsc-tree'
622642
path: '/rsc-tree'
@@ -976,6 +996,7 @@ const rootRouteChildren: RootRouteChildren = {
976996
RscStreamingRoute: RscStreamingRoute,
977997
RscSuspenseRoute: RscSuspenseRoute,
978998
RscTreeRoute: RscTreeRoute,
999+
RscUseServerFnRoute: RscUseServerFnRoute,
9791000
ApiRscFlightRoute: ApiRscFlightRoute,
9801001
RscCssConditionalBranchRoute: RscCssConditionalBranchRoute,
9811002
RscParamIdRoute: RscParamIdRoute,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from 'react'
2+
import { createFileRoute } from '@tanstack/react-router'
3+
import { createServerFn, useServerFn } from '@tanstack/react-start'
4+
import { pageStyles } from '~/utils/styles'
5+
6+
const getMessage = createServerFn({ method: 'GET' }).handler(async () => {
7+
return 'useServerFn works with RSC builds'
8+
})
9+
10+
export const Route = createFileRoute('/rsc-use-server-fn')({
11+
component: RscUseServerFnComponent,
12+
})
13+
14+
function RscUseServerFnComponent() {
15+
const getMessageFn = useServerFn(getMessage)
16+
const [message, setMessage] = React.useState('')
17+
18+
return (
19+
<div style={pageStyles.container}>
20+
<h1 data-testid="rsc-use-server-fn-title" style={pageStyles.title}>
21+
useServerFn with RSC
22+
</h1>
23+
<button
24+
type="button"
25+
data-testid="rsc-use-server-fn-button"
26+
onClick={async () => {
27+
setMessage(await getMessageFn())
28+
}}
29+
>
30+
Call server function
31+
</button>
32+
<p data-testid="rsc-use-server-fn-message">
33+
{message || 'No message yet'}
34+
</p>
35+
</div>
36+
)
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from '@playwright/test'
2+
import { test } from '@tanstack/router-e2e-utils'
3+
import { waitForHydration } from './hydration'
4+
5+
test.describe('RSC useServerFn Tests', () => {
6+
test('useServerFn can be imported and called in an RSC app', async ({
7+
page,
8+
}) => {
9+
await page.goto('/rsc-use-server-fn')
10+
await page.waitForURL('/rsc-use-server-fn')
11+
await waitForHydration(page)
12+
13+
await expect(page.getByTestId('rsc-use-server-fn-title')).toHaveText(
14+
'useServerFn with RSC',
15+
)
16+
17+
await page.getByTestId('rsc-use-server-fn-button').click()
18+
await expect(page.getByTestId('rsc-use-server-fn-message')).toHaveText(
19+
'useServerFn works with RSC builds',
20+
)
21+
})
22+
})

‎packages/react-start/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"exports": {
3535
".": {
3636
"react-server": {
37-
"types": "./dist/esm/index.rsc.d.ts",
38-
"default": "./dist/esm/index.rsc.js"
37+
"types": "./dist/esm/index.d.ts",
38+
"default": "./dist/esm/index.js"
3939
},
4040
"import": {
4141
"types": "./dist/esm/index.d.ts",

‎packages/react-start/src/index.rsc.ts‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎packages/react-start/vite.config.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export default mergeConfig(
2626
exclude: ['./src/default-entry'],
2727
entry: [
2828
'./src/index.ts',
29-
'./src/index.rsc.ts',
3029
'./src/client.tsx',
3130
'./src/client-rpc.ts',
3231
'./src/server.tsx',

0 commit comments

Comments
 (0)