Skip to content

Referencing ServerRoute in component body #4275

Description

@brenelz

Which project does this relate to?

Start

Describe the bug

In the alpha branch"1.121.0-alpha.20" I am trying to reference the ServerRoute I've created as shown in the docs. I think this should be possible.

Image
import { createServerFileRoute } from "@tanstack/react-start/server"
import { useState } from "react"

export const ServerRoute = createServerFileRoute().methods({
    POST: async ({ request }) => {
        const body = await request.json()
        return new Response(JSON.stringify({ message: `Hello, ${body.name}!` }))
    },
})

export const Route = createFileRoute({
    component: HelloComponent,
})

function HelloComponent() {
    const [reply, setReply] = useState('')

    return (
        <div>
            <button
                onClick={() => {
                    ServerRoute.methods
                        .post({
                            body: JSON.stringify({ name: 'John' }),
                        })
                        .then((res) => res.json())
                        .then((data) => setReply(data.message))
                }}
            >
                Say Hello
            </button>
        </div>
    )
}

I am also getting some TypeScript errors

Image Image

Your Example Website or App

brenelz/tanstack-faster-react#2

Steps to Reproduce the Bug or Issue

  1. pnpm dev
  2. http://localhost:3000/test
  3. See error "The requested module '/src/routes/test.tsx' does not provide an export named 'ServerRoute'"

Expected behavior

I expect to be able to click the button which will make a POST request to my server route and return

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 136.0.7103.114

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions