SolidStart Adapter
Use oRPC inside a SolidStart project by mounting a handler in an API route.
SolidStart is a full stack JavaScript framework for building web applications with SolidJS. Its API routes follow the Fetch API, so oRPC integrates through the Fetch API Adapter.
Server
import type { APIEvent } from '@solidjs/start/server'
import { onError } from '@orpc/server'
import { RPCHandler } from '@orpc/server/fetch'
const handler = new RPCHandler(router, {
interceptors: [
onError((error) => {
console.error(error)
}),
],
})
async function handle({ request }: APIEvent) {
const { response } = await handler.handle(request, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
return response ?? new Response('Not found', { status: 404 })
}
export const HEAD = handle
export const GET = handle
export const POST = handle
export const PUT = handle
export const PATCH = handle
export const DELETE = handleexport { DELETE, GET, HEAD, PATCH, POST, PUT } from './[...rest]'Client
On the client, use getRequestEvent to resolve the request origin and forward headers during SSR. This enables usage in both server and browser environments.
import { RPCLink } from '@orpc/client/fetch'
import { getRequestEvent } from 'solid-js/web'
const link = new RPCLink({
url: '/rpc',
// Resolve the origin from the incoming request during SSR; defaults to the current origin in the browser.
origin: () => {
const event = getRequestEvent()
return event ? new URL(event.request.url).origin : undefined
},
headers: () => getRequestEvent()?.request.headers ?? {},
})
Optimize SSR
To reduce HTTP requests and improve latency during SSR, you can use a server-side client during SSR. Below is a quick setup, see Optimizing SSR for more details.
import type { RouterClient } from '@orpc/server'
import { createORPCClient } from '@orpc/client'
import { RPCLink } from '@orpc/client/fetch'
if (import.meta.env.SSR) {
await import('./orpc.server')
}
declare global {
var $client: RouterClient<typeof router> | undefined
}
const link = new RPCLink({
url: '/rpc',
origin: () => {
if (typeof window === 'undefined') {
throw new Error('This link is not allowed on the server side.')
}
return window.location.origin
},
})
/**
* Fall back to a browser client when no SSR client is registered.
*/
export const client: RouterClient<typeof router> = globalThis.$client ?? createORPCClient(link)import { createRouterClient } from '@orpc/server'
import { getRequestEvent } from 'solid-js/web'
if (typeof window !== 'undefined') {
throw new Error('This file should not be imported in the browser')
}
globalThis.$client = createRouterClient(router, {
/**
* Provide initial context if needed.
*
* Because this client instance is shared across all requests,
* only include context that's safe to reuse globally.
* For per-request context, use middleware context or pass a function as the initial context.
*/
context: async () => {
const headers = getRequestEvent()?.request.headers
return {
headers, // provide headers if initial context required
}
},
})