Skip to content

Commit ca5123c

Browse files
committed
docs: svelte kit adapter with optimize ssr
1 parent fdd2389 commit ca5123c

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

apps/content/docs/adapters/svelte-kit.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Use oRPC inside an Svelte Kit project
77

88
[Svelte Kit](https://svelte.dev/docs/kit/introduction) is a framework for rapidly developing robust, performant web applications using Svelte. For additional context, refer to the [HTTP Adapter](/docs/adapters/http) guide.
99

10-
## Basic
10+
## Server
1111

1212
::: code-group
1313

@@ -38,3 +38,61 @@ export const DELETE = handle
3838
::: info
3939
The `handler` can be any supported oRPC handler, such as [RPCHandler](/docs/rpc-handler), [OpenAPIHandler](/docs/openapi/openapi-handler), or another custom handler.
4040
:::
41+
42+
## Optimize SSR
43+
44+
To reduce HTTP requests and improve latency during SSR, you can utilize [Svelte's special `fetch`](https://svelte.dev/docs/kit/web-standards#Fetch-APIs) during SSR. Below is a quick setup, see [Optimize SSR](/docs/best-practices/optimize-ssr) for more details.
45+
46+
::: code-group
47+
48+
```ts [src/lib/orpc.ts]
49+
import type { RouterClient } from '@orpc/server'
50+
import { RPCLink } from '@orpc/client/fetch'
51+
import { createORPCClient } from '@orpc/client'
52+
53+
declare global {
54+
var $client: RouterClient<typeof router> | undefined
55+
}
56+
57+
const link = new RPCLink({
58+
url: () => {
59+
if (typeof window === 'undefined') {
60+
throw new Error('This link is not allowed on the server side.')
61+
}
62+
63+
return `${window.location.origin}/rpc`
64+
},
65+
})
66+
67+
export const client: RouterClient<typeof router> = globalThis.$client ?? createORPCClient(link)
68+
```
69+
70+
```ts [src/lib/orpc.server.ts]
71+
import type { RouterClient } from '@orpc/server'
72+
import { createORPCClient } from '@orpc/client'
73+
import { RPCLink } from '@orpc/client/fetch'
74+
import { getRequestEvent } from '$app/server'
75+
76+
if (typeof window !== 'undefined') {
77+
throw new Error('This file should only be imported on the server')
78+
}
79+
80+
const link = new RPCLink({
81+
url: async () => {
82+
return `${getRequestEvent().url.origin}/rpc`
83+
},
84+
async fetch(request, init) {
85+
return getRequestEvent().fetch(request, init)
86+
},
87+
})
88+
89+
const serverClient: RouterClient<typeof router> = createORPCClient(link)
90+
globalThis.$client = serverClient
91+
```
92+
93+
```ts [src/hooks.server.ts]
94+
import './lib/orpc.server'
95+
// ...
96+
```
97+
98+
:::
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './lib/orpc.server'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { RouterClient } from '@orpc/server'
2+
import type { router } from '../routers'
3+
import { createORPCClient } from '@orpc/client'
4+
import { RPCLink } from '@orpc/client/fetch'
5+
import { getRequestEvent } from '$app/server'
6+
7+
if (typeof window !== 'undefined') {
8+
throw new Error('This file should only be imported on the server')
9+
}
10+
11+
/**
12+
* This is part of the Optimize SSR setup.
13+
*
14+
* @see {@link https://orpc.unnoq.com/docs/adapters/svelte-kit#optimize-ssr}
15+
*/
16+
const link = new RPCLink({
17+
url: async () => {
18+
return `${getRequestEvent().url.origin}/rpc`
19+
},
20+
async fetch(request, init) {
21+
return getRequestEvent().fetch(request, init)
22+
},
23+
})
24+
25+
const serverClient: RouterClient<typeof router> = createORPCClient(link)
26+
globalThis.$client = serverClient

playgrounds/svelte-kit/src/lib/orpc.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import { RPCLink } from '@orpc/client/fetch'
55
import { BatchLinkPlugin } from '@orpc/client/plugins'
66
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
77

8-
const rpcLink = new RPCLink({
8+
/**
9+
* This is part of the Optimize SSR setup.
10+
*
11+
* @see {@link https://orpc.unnoq.com/docs/adapters/svelte-kit#optimize-ssr}
12+
*/
13+
declare global {
14+
var $client: RouterClient<typeof router> | undefined
15+
}
16+
17+
const link = new RPCLink({
918
url: `${typeof window !== 'undefined' ? window.location.origin : 'http://localhost:3000'}/rpc`,
10-
headers: () => ({
11-
Authorization: 'Bearer default-token',
12-
}),
1319
plugins: [
1420
new BatchLinkPlugin({
1521
exclude: ({ path }) => path[0] === 'sse',
@@ -21,6 +27,6 @@ const rpcLink = new RPCLink({
2127
],
2228
})
2329

24-
export const client: RouterClient<typeof router> = createORPCClient(rpcLink)
30+
export const client: RouterClient<typeof router> = globalThis.$client ?? createORPCClient(link)
2531

2632
export const orpc = createTanstackQueryUtils(client)

0 commit comments

Comments
 (0)