diff --git a/packages/solid-query-devtools/src/clientOnly.tsx b/packages/solid-query-devtools/src/clientOnly.tsx new file mode 100644 index 0000000000..379bd78ad7 --- /dev/null +++ b/packages/solid-query-devtools/src/clientOnly.tsx @@ -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>( + fn: () => Promise<{ + default: T + }>, +) { + if (isServer) + return (props: ComponentProps & { fallback?: JSX.Element }) => + props.fallback + + const [comp, setComp] = createSignal() + fn().then((m) => setComp(() => m.default)) + return (props: ComponentProps) => { + 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)) + ), + ) + } +} diff --git a/packages/solid-query-devtools/src/devtools.tsx b/packages/solid-query-devtools/src/devtools.tsx index 1b7bc4a698..1dc8807890 100644 --- a/packages/solid-query-devtools/src/devtools.tsx +++ b/packages/solid-query-devtools/src/devtools.tsx @@ -1,15 +1,5 @@ -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, @@ -17,7 +7,7 @@ import type { 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 { /** @@ -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. */ @@ -104,36 +98,3 @@ export default function SolidQueryDevtools(props: DevtoolsOptions) { return
} - -/* - 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>( - fn: () => Promise<{ - default: T - }>, -) { - if (isServer) - return (props: ComponentProps & { fallback?: JSX.Element }) => - props.fallback - - const [comp, setComp] = createSignal() - fn().then((m) => setComp(() => m.default)) - return (props: ComponentProps) => { - 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)) - ), - ) - } -} diff --git a/packages/solid-query-devtools/src/index.tsx b/packages/solid-query-devtools/src/index.tsx index 63f21620ec..a67e7cede1 100644 --- a/packages/solid-query-devtools/src/index.tsx +++ b/packages/solid-query-devtools/src/index.tsx @@ -1,5 +1,5 @@ import { isDev } from 'solid-js/web' -import { clientOnly } from './devtools' +import clientOnly from './clientOnly' import type SolidQueryDevtoolsComp from './devtools' export const SolidQueryDevtools: typeof SolidQueryDevtoolsComp = isDev