Server Entry Point

Server Entry Point

Note

The server entry point is optional out of the box. If not provided, TanStack Start will automatically handle the server entry point for you using the below as a default.

This is done via the src/server.ts file.

// src/server.ts
import handler from '@tanstack/react-start/server-entry'

export default {
  fetch(request: Request) {
    return handler.fetch(request)
  },
}
// src/server.ts
import handler from '@tanstack/react-start/server-entry'

export default {
  fetch(request: Request) {
    return handler.fetch(request)
  },
}

The entry point must conform to the following interface:

export default {
  fetch(req: Request): Promise<Response> {
    // ...
  },
}
export default {
  fetch(req: Request): Promise<Response> {
    // ...
  },
}

Whether we are statically generating our app or serving it dynamically, the server.ts file is the entry point for doing all SSR-related work.

Custom Server Handlers

You can create custom server handlers to modify how your application is rendered:

// src/server.ts
import {
  createStartHandler,
  defaultStreamHandler,
} from '@tanstack/react-start/server'

// Custom handler example
const customHandler = (request, response) => {
  // Add custom logic here
  return defaultStreamHandler(request, response)
}

const fetch = createStartHandler(customHandler)

export default {
  fetch,
}
// src/server.ts
import {
  createStartHandler,
  defaultStreamHandler,
} from '@tanstack/react-start/server'

// Custom handler example
const customHandler = (request, response) => {
  // Add custom logic here
  return defaultStreamHandler(request, response)
}

const fetch = createStartHandler(customHandler)

export default {
  fetch,
}

Server Configuration

The server entry point is where you can configure server-specific behavior:

  • Request/response middleware
  • Custom error handling
  • Authentication logic
  • Database connections
  • Logging and monitoring

This flexibility allows you to customize how your TanStack Start application handles server-side rendering while maintaining the framework's conventions.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.