-
-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start - Server Function Middleware Included in Client Bundle #2783
Comments
From a cursory overview, it seems that dead-code-elimination isn't being performed on the client references code before being packaged into their necessary bundles. Lines 12 to 21 in 5589151
|
What worked for me was moving the middleware definition (createMiddleware) into its own separate file |
I'm having difficult reproducing this using our "start-supabase-basic" example. import * as React from 'react'
import { createFileRoute } from '@tanstack/react-router'
import { createMiddleware, createServerFn, useServerFn } from '@tanstack/start'
import { getSupabaseServerClient } from '~/utils/supabase'
const authMiddleware = createMiddleware().server(async ({ next }) => {
const supa = getSupabaseServerClient()
const user = await supa.auth.getUser()
console.log('user response', user)
return next({ context: { user } })
})
const checkAuthFn = createServerFn()
.middleware([authMiddleware])
.handler(({ context }) => {
console.log('checkAuthFn', context.user)
return { foo: 'bar' }
})
export const Route = createFileRoute('/test')({
component: RouteComponent,
})
function RouteComponent() {
const fn = useServerFn(checkAuthFn)
return (
<div className="flex gap-2">
<button
onClick={() => {
checkAuthFn().then((res) => {
console.log(Date.now(), 'direct', res)
})
}}
>
Test Direct
</button>
<button
onClick={() => {
fn().then((res) => {
console.log(Date.now(), 'useServerFn', res)
})
}}
>
Test useServerFn
</button>
</div>
)
} |
@SeanCassiere - I'll try to pull together a reproduction tomorrow. I attempted yesterday but I was having issues with StackBlitz. I will say my example was overly simplistic. I actually had the Middleware in a separate file. Tonight I tried defining the middleware in the same file as the server function and that worked. The contents of the middleware were the same in both cases.
|
@SeanCassiere - I've created a reproduction on StackBlitz. This example handles it more gracefully than my actual case (where the route crashes and burns), but it still reproduces. If you open your browser dev tools, the console should show node-related errors and also load server side packages I reproduced it with two separate middleware functions using two separate node packages. I added the server function with middleware to First ReproductionThe first is using supabase as mentioned above. Second ReproductionThe second reproduction is in |
Which project does this relate to?
Start
Describe the bug
Calling a serverFn using middleware in the client with
useServerFn
results in server side code being included in client bundle.I'll try to get a minimal reproduction later this weekend or early next week. But here is an example of the issue.
Your Example Website or App
StackBlitz Reproduction Link
Steps to Reproduce the Bug or Issue
Basic Example:
Client Side Error:
I see a request for the file "@/features/supabase/utils", and get this console error.
Expected behavior
No error & the file "@/features/supabase/utils" is not included in client side code.
Removing the middleware from the server function resolves the issue. The same reference to
getSupbaseServerClient
inside the serverFn handler runs without issue.Screenshots or Videos
No response
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: