Skip to content

fix(solid-query-devtools): client-side code executing on server, breaking UI #9163

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/solid-query-devtools/src/clientOnly.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
createMemo,
createSignal,
onMount,
sharedConfig,
splitProps,
untrack,
} from 'solid-js'
import { isServer } from 'solid-js/web'
import type { Component, ComponentProps, JSX } from 'solid-js'

/*
This function has been taken from solid-start's codebase
This allows the devtools to be loaded only on the client and bypasses any server side rendering
https://github.com/solidjs/solid-start/blob/2967fc2db3f0df826f061020231dbdafdfa0746b/packages/start/islands/clientOnly.tsx
*/
export default function clientOnly<T extends Component<any>>(
fn: () => Promise<{
default: T
}>,
) {
if (isServer)
return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
props.fallback

const [comp, setComp] = createSignal<T>()
fn().then((m) => setComp(() => m.default))
return (props: ComponentProps<T>) => {
let Comp: T | undefined
let m: boolean
const [, rest] = splitProps(props, ['fallback'])
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
const [mounted, setMounted] = createSignal(!sharedConfig.context)
onMount(() => setMounted(true))
return createMemo(
() => (
(Comp = comp()),
(m = mounted()),
untrack(() => (Comp && m ? Comp(rest) : props.fallback))
),
)
}
}
51 changes: 6 additions & 45 deletions packages/solid-query-devtools/src/devtools.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import {
createEffect,
createMemo,
createSignal,
onCleanup,
onMount,
sharedConfig,
splitProps,
untrack,
} from 'solid-js'
import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
import { onlineManager, useQueryClient } from '@tanstack/solid-query'
import { isServer } from 'solid-js/web'
import { TanstackQueryDevtools } from '@tanstack/query-devtools'
import type {
DevtoolsButtonPosition,
DevtoolsErrorType,
DevtoolsPosition,
} from '@tanstack/query-devtools'
import type { QueryClient } from '@tanstack/solid-query'
import type { Component, ComponentProps, JSX } from 'solid-js'
import type { JSX } from 'solid-js'

interface DevtoolsOptions {
/**
Expand All @@ -40,6 +30,10 @@ interface DevtoolsOptions {
* Custom instance of QueryClient
*/
client?: QueryClient
/**
* Fallback component to show while the devtools are loading.
*/
fallback?: JSX.Element
/**
* Use this so you can define custom errors that can be shown in the devtools.
*/
Expand Down Expand Up @@ -104,36 +98,3 @@ export default function SolidQueryDevtools(props: DevtoolsOptions) {

return <div class="tsqd-parent-container" ref={ref}></div>
}

/*
This function has been taken from solid-start's codebase
This allows the devtools to be loaded only on the client and bypasses any server side rendering
https://github.com/solidjs/solid-start/blob/2967fc2db3f0df826f061020231dbdafdfa0746b/packages/start/islands/clientOnly.tsx
*/
export function clientOnly<T extends Component<any>>(
fn: () => Promise<{
default: T
}>,
) {
if (isServer)
return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
props.fallback

const [comp, setComp] = createSignal<T>()
fn().then((m) => setComp(() => m.default))
return (props: ComponentProps<T>) => {
let Comp: T | undefined
let m: boolean
const [, rest] = splitProps(props, ['fallback'])
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
const [mounted, setMounted] = createSignal(!sharedConfig.context)
onMount(() => setMounted(true))
return createMemo(
() => (
(Comp = comp()),
(m = mounted()),
untrack(() => (Comp && m ? Comp(rest) : props.fallback))
),
)
}
}
2 changes: 1 addition & 1 deletion packages/solid-query-devtools/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isDev } from 'solid-js/web'
import { clientOnly } from './devtools'
import clientOnly from './clientOnly'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is here, importing from ./devtools prematurely before line 6 dynamically imports it later.

import type SolidQueryDevtoolsComp from './devtools'

export const SolidQueryDevtools: typeof SolidQueryDevtoolsComp = isDev
Expand Down