-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ServerOnly executing on the client #299
Comments
And if I use a client fallback that does effectively the same code, I still get a hydration mismatch error export function FenceClient(props: { children?: ReactNode; language: string }) {
const code = Children.toArray(props.children)[0] as string
const [html, setHTML] = useState("")
async function loadHTML() {
const res = await fetch("/syntax_highlighter", {
method: "post",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
code,
language: props.language,
} as SyntaxHighlightPayload),
})
setHTML(await res.text())
}
useEffect(() => {
loadHTML()
}, [])
return (
<div
dangerouslySetInnerHTML={{
__html: html,
}}
/>
)
} export function Fence(props: { children?: ReactNode; language: string }) {
return (
<ServerOnly fallback={<FenceClient {...props} />}>
{() => {
const code = Children.toArray(props.children)[0] as string
const html = highlighter.codeToHtml(code.trim(), {
lang: props.language,
})
return (
<div
dangerouslySetInnerHTML={{
__html: html,
}}
/>
)
}}
</ServerOnly>
)
} export interface SyntaxHighlightPayload {
language: string
code: string
}
export async function action(args: ActionFunctionArgs) {
const data = (await args.request.json()) as SyntaxHighlightPayload
const html = highlighter.codeToHtml(data.code.trim(), {
lang: data.language,
})
return new Response(html)
} |
I think the problem is that in But ServerOnly is intended as the inverse of ClientOnly, in ClientOnly the children is rendered in the client and the fallback in the server, in ServerOnly the children is rendered in the server and the fallback in the browser. If you can create a reproduction repo I could try to see what's happening. |
I suspect that as well, as I use this in other places successfully like: <ServerOnly>
{() => {
return (
<TopNav
isAdmin={data.user?.isAdmin}
redirectTo={data.currentPath}
authed={!!data.user}
subscribed={!!data.user?.subscription}
/>
)
}}
</ServerOnly>
<ClientOnly>
{() => {
return (
<TopNav
isAdmin={data.user?.isAdmin}
redirectTo={window.location.pathname}
authed={!!data.user}
subscribed={!!data.user?.subscription}
/>
)
}}
</ClientOnly> |
Describe the bug
With a shiki
highlighter
in a.server.ts
file, imported into aFence.tsx
file, the browser console is throwing errors ofhighlighter.codeToHtml
not being defined, and renders mismatching between server and client:Your Example Website or App
not public atm
Steps to Reproduce the Bug or Issue
highlighter.server.ts
fetch.tsx
Then render Fence in a route with nothing else (no loaders, etc.)
Expected behavior
Does not try to render on the client
Screenshots or Videos
No response
Platform
Additional context
Discovery in Remix discord: https://discord.com/channels/770287896669978684/770287896669978687/1198623722815361148
The text was updated successfully, but these errors were encountered: