Open History to see the last commit
1import { createServerFn } from '@tanstack/react-start'
2import { createCompositeComponent } from '@tanstack/react-start/rsc'
3β
4// Shared styles for server-rendered content (blue theme)
5const serverStyles = {
6 container: {
7 backgroundColor: '#e0f2fe',
8 border: '2px solid #0284c7',
9 borderRadius: '8px',
10 padding: '16px',
11 },
12 badge: {
13 backgroundColor: '#0284c7',
14 color: 'white',
15 padding: '2px 8px',
16 borderRadius: '4px',
17 fontSize: '12px',
18 fontWeight: 'bold',
19 },
20 label: {
21 color: '#0369a1',
22 fontSize: '12px',
23 fontWeight: 'bold',
24 textTransform: 'uppercase' as const,
25 marginBottom: '4px',
26 },
27}
28β
29/**
30 * Product Detail RSC - Fetches product data from the "database" and renders on the server.
31 *
32 * This demonstrates a realistic use case where:
33 * - Product info (name, price, description, stock) is fetched and rendered on the server
34 * - The RSC accepts a slot for client-side interactive elements (add to cart, quantity selector)
35 * - React Query caches the RSC, so navigating away and back doesn't refetch
36 * - Explicitly refetching updates the server timestamp (simulating fresh data)
37 */
38export const getProductDetailComponent = createServerFn({ method: 'GET' })
39 .validator((data: { productId: string }) => data)
40 .handler(async ({ data }) => {
41 const fetchedAt = Date.now()
42β
43 // Simulate database fetch with realistic product data
44 const product = {
45 id: data.productId,
46 name: 'Wireless Bluetooth Headphones',
47 price: 79.99,
48 originalPrice: 99.99,
49 description:
50 'Premium wireless headphones with active noise cancellation, 30-hour battery life, and crystal-clear audio quality.',
51 rating: 4.5,
52 reviewCount: 1247,
53 inStock: true,
54 stockCount: 23,
55 sku: `SKU-${data.productId}-WBH`,
56 }
57β
58 const Info = (
59 <div style={serverStyles.container} data-testid="rsc-query-content">
60 {/* Server badge */}
61 <div
62 style={{
63 display: 'flex',
64 justifyContent: 'space-between',
65 alignItems: 'center',
66 marginBottom: '16px',
67 }}
68 >
69 <span style={serverStyles.badge}>SERVER RENDERED (RSC)</span>
70 <span
71 style={{ fontSize: '12px', color: '#64748b' }}
72 data-testid="rsc-server-timestamp"
73 >
74 Fetched: {new Date(fetchedAt).toLocaleTimeString()}
75 </span>
76 </div>
77β
78 {/* Product Header */}
79 <h2
80 style={{ margin: '0 0 8px 0', color: '#0c4a6e' }}
81 data-testid="rsc-item-name"
82 >
83 {product.name}
84 </h2>
85β
86 {/* Rating */}
87 <div
88 style={{
89 display: 'flex',
90 alignItems: 'center',
91 gap: '8px',
92 marginBottom: '12px',
93 }}
94 >
95 <span style={{ color: '#f59e0b' }}>
96 {'*'.repeat(Math.floor(product.rating))}
97 </span>
98 <span style={{ color: '#64748b', fontSize: '14px' }}>
99 {product.rating} ({product.reviewCount} reviews)
100 </span>
101 </div>
102β
103 {/* Price */}
104 <div style={{ marginBottom: '16px' }}>
105 <span
106 style={{
107 fontSize: '28px',
108 fontWeight: 'bold',
109 color: '#0c4a6e',
110 }}
111 >
112 ${product.price}
113 </span>
114 <span
115 style={{
116 marginLeft: '12px',
117 textDecoration: 'line-through',
118 color: '#94a3b8',
119 }}
120 >
121 ${product.originalPrice}
122 </span>
123 <span
124 style={{
125 marginLeft: '8px',
126 color: '#16a34a',
127 fontWeight: 'bold',
128 }}
129 >
130 Save ${(product.originalPrice - product.price).toFixed(2)}
131 </span>
132 </div>
133β
134 {/* Description */}
135 <p
136 style={{
137 color: '#334155',
138 lineHeight: '1.6',
139 marginBottom: '16px',
140 }}
141 data-testid="rsc-item-description"
142 >
143 {product.description}
144 </p>
145β
146 {/* Stock Status */}
147 <div
148 style={{
149 display: 'flex',
150 alignItems: 'center',
151 gap: '8px',
152 marginBottom: '16px',
153 }}
154 >
155 <span
156 style={{
157 width: '8px',
158 height: '8px',
159 borderRadius: '50%',
160 backgroundColor: product.inStock ? '#16a34a' : '#dc2626',
161 }}
162 />
163 <span
164 style={{
165 color: product.inStock ? '#16a34a' : '#dc2626',
166 fontWeight: '500',
167 }}
168 >
169 {product.inStock
170 ? `In Stock (${product.stockCount} available)`
171 : 'Out of Stock'}
172 </span>
173 </div>
174β
175 {/* Product Details */}
176 <div
177 style={{
178 borderTop: '1px solid #bae6fd',
179 paddingTop: '12px',
180 marginBottom: '16px',
181 }}
182 >
183 <div style={serverStyles.label}>Product Details</div>
184 <div
185 style={{
186 display: 'grid',
187 gridTemplateColumns: '1fr 1fr',
188 gap: '8px',
189 fontSize: '14px',
190 }}
191 >
192 <span style={{ color: '#64748b' }}>Product ID:</span>
193 <span style={{ color: '#0c4a6e' }} data-testid="rsc-item-id">
194 {product.id}
195 </span>
196 <span style={{ color: '#64748b' }}>SKU:</span>
197 <span style={{ color: '#0c4a6e' }}>{product.sku}</span>
198 </div>
199 </div>
200 </div>
201 )
202β
203 const src = await createCompositeComponent(
204 (props: { children?: React.ReactNode }) => {
205 return (
206 <div data-testid="rsc-slot-content">
207 {Info}
208 {props.children}
209 </div>
210 )
211 },
212 )
213β
214 return { src }
215 })