-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathnext-ssr-plugin.tsx
More file actions
54 lines (48 loc) Β· 1.41 KB
/
Copy pathnext-ssr-plugin.tsx
File metadata and controls
54 lines (48 loc) Β· 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* OpencodeSSRPlugin - Inject OpenCode configuration before React hydrates
*
* Pattern: uploadthing's useServerInsertedHTML approach
*
* Usage:
* ```tsx
* // app/layout.tsx or page component
* <OpencodeSSRPlugin config={{ baseUrl: "/api/opencode/4056", directory: "/path" }} />
* ```
*/
"use client"
import { useServerInsertedHTML } from "next/navigation"
export interface OpencodeConfig {
baseUrl: string
directory?: string
}
export interface OpencodeSSRPluginProps {
config: OpencodeConfig
}
/**
* Injects OpenCode configuration into globalThis before React hydration
*
* This eliminates the need for a React provider wrapper by making config
* available synchronously during client-side rendering.
*
* Works in two ways:
* 1. SSR: useServerInsertedHTML injects a script tag during server rendering
* 2. Client: Synchronously sets window.__OPENCODE during render (before hooks run)
*/
export function OpencodeSSRPlugin({ config }: OpencodeSSRPluginProps) {
// SSR: Inject script tag during server rendering
useServerInsertedHTML(() => {
return (
<script
dangerouslySetInnerHTML={{
__html: `window.__OPENCODE = ${JSON.stringify(config)};`,
}}
/>
)
})
// Client: Set config synchronously during render (not in useEffect!)
// This ensures config is available before any hooks call getOpencodeConfig()
if (typeof window !== "undefined") {
window.__OPENCODE = config
}
return null
}