Skip to content

Commit e8f2174

Browse files
fix: trace nested server function factory re-export chain (#6166)
1 parent 1f98e84 commit e8f2174

File tree

12 files changed

+588
-200
lines changed

12 files changed

+588
-200
lines changed

e2e/react-start/server-functions/src/routes/factory/-functions/functions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { createFakeFn } from './createFakeFn'
66
import { reexportFactory } from './reexportIndex'
77
// Test star re-export syntax: `export * from './module'`
88
import { starReexportFactory } from './starReexportIndex'
9+
// Test nested star re-export syntax: A -> B -> C chain
10+
import { nestedReexportFactory } from './nestedReexportA'
911

1012
export const fooFn = createFooServerFn().handler(({ context }) => {
1113
return {
@@ -115,3 +117,14 @@ export const starReexportedFactoryFn = starReexportFactory().handler(
115117
}
116118
},
117119
)
120+
121+
// Test that nested star re-exported factories (A -> B -> C chain) work correctly
122+
// The middleware from nestedReexportFactory should execute and add { nested: 'nested-middleware-executed' } to context
123+
export const nestedReexportedFactoryFn = nestedReexportFactory().handler(
124+
({ context }) => {
125+
return {
126+
name: 'nestedReexportedFactoryFn',
127+
context,
128+
}
129+
},
130+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Top-level module in the nested re-export chain.
3+
* Re-exports everything from nestedReexportB.
4+
*
5+
* Chain: nestedReexportA (this file) -> nestedReexportB -> nestedReexportC
6+
*/
7+
export * from './nestedReexportB'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Middle module in the nested re-export chain.
3+
* Re-exports everything from nestedReexportC.
4+
*
5+
* Chain: nestedReexportA -> nestedReexportB (this file) -> nestedReexportC
6+
*/
7+
export * from './nestedReexportC'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* This is the deepest module in the nested re-export chain.
3+
* It defines a server function factory with middleware.
4+
*
5+
* Chain: nestedReexportA -> nestedReexportB -> nestedReexportC (this file)
6+
*/
7+
import { createMiddleware, createServerFn } from '@tanstack/react-start'
8+
9+
const nestedMiddleware = createMiddleware({ type: 'function' }).server(
10+
({ next }) => {
11+
console.log('nested middleware triggered')
12+
return next({
13+
context: { nested: 'nested-middleware-executed' } as const,
14+
})
15+
},
16+
)
17+
18+
export const nestedReexportFactory = createServerFn({
19+
method: 'GET',
20+
}).middleware([nestedMiddleware])

e2e/react-start/server-functions/src/routes/factory/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
fooFnPOST,
1313
localFn,
1414
localFnPOST,
15+
nestedReexportedFactoryFn,
1516
reexportedFactoryFn,
1617
starReexportedFactoryFn,
1718
} from './-functions/functions'
@@ -152,6 +153,16 @@ const functions = {
152153
context: { starReexport: 'star-reexport-middleware-executed' },
153154
},
154155
},
156+
// Test that nested star re-exported factories (A -> B -> C chain) work correctly
157+
// The middleware from nestedReexportFactory should execute and add { nested: 'nested-middleware-executed' } to context
158+
nestedReexportedFactoryFn: {
159+
fn: nestedReexportedFactoryFn,
160+
type: 'serverFn',
161+
expected: {
162+
name: 'nestedReexportedFactoryFn',
163+
context: { nested: 'nested-middleware-executed' },
164+
},
165+
},
155166
} satisfies Record<string, TestCase>
156167

157168
interface TestCase {

e2e/react-start/server-functions/tests/server-functions.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,27 @@ test('star re-exported server function factory middleware executes correctly', a
591591
page.getByTestId('fn-comparison-starReexportedFactoryFn'),
592592
).toContainText('equal')
593593
})
594+
595+
test('nested star re-exported server function factory middleware executes correctly', async ({
596+
page,
597+
}) => {
598+
// This test specifically verifies that when a server function factory is re-exported
599+
// through a nested chain (A -> B -> C) using `export * from './module'` syntax,
600+
// the middleware still executes correctly.
601+
await page.goto('/factory')
602+
603+
await expect(page.getByTestId('factory-route-component')).toBeInViewport()
604+
605+
// Click the button for the nested re-exported factory function
606+
await page.getByTestId('btn-fn-nestedReexportedFactoryFn').click()
607+
608+
// Wait for the result
609+
await expect(
610+
page.getByTestId('fn-result-nestedReexportedFactoryFn'),
611+
).toContainText('nested-middleware-executed')
612+
613+
// Verify the full context was returned (middleware executed)
614+
await expect(
615+
page.getByTestId('fn-comparison-nestedReexportedFactoryFn'),
616+
).toContainText('equal')
617+
})

0 commit comments

Comments
 (0)