Skip to content

Commit 868ce03

Browse files
committed
separate concerns of clientOnly and SolidQueryDevtools
1 parent 065b5f4 commit 868ce03

File tree

3 files changed

+50
-46
lines changed

3 files changed

+50
-46
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
createMemo,
3+
createSignal,
4+
onMount,
5+
sharedConfig,
6+
splitProps,
7+
untrack,
8+
} from 'solid-js'
9+
import { isServer } from 'solid-js/web'
10+
import type { Component, ComponentProps, JSX } from 'solid-js'
11+
12+
/*
13+
This function has been taken from solid-start's codebase
14+
This allows the devtools to be loaded only on the client and bypasses any server side rendering
15+
https://github.com/solidjs/solid-start/blob/2967fc2db3f0df826f061020231dbdafdfa0746b/packages/start/islands/clientOnly.tsx
16+
*/
17+
export default function clientOnly<T extends Component<any>>(
18+
fn: () => Promise<{
19+
default: T
20+
}>,
21+
) {
22+
if (isServer)
23+
return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
24+
props.fallback
25+
26+
const [comp, setComp] = createSignal<T>()
27+
fn().then((m) => setComp(() => m.default))
28+
return (props: ComponentProps<T>) => {
29+
let Comp: T | undefined
30+
let m: boolean
31+
const [, rest] = splitProps(props, ['fallback'])
32+
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
33+
const [mounted, setMounted] = createSignal(!sharedConfig.context)
34+
onMount(() => setMounted(true))
35+
return createMemo(
36+
() => (
37+
(Comp = comp()),
38+
(m = mounted()),
39+
untrack(() => (Comp && m ? Comp(rest) : props.fallback))
40+
),
41+
)
42+
}
43+
}

packages/solid-query-devtools/src/devtools.tsx

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
import {
2-
createEffect,
3-
createMemo,
4-
createSignal,
5-
onCleanup,
6-
onMount,
7-
sharedConfig,
8-
splitProps,
9-
untrack,
10-
} from 'solid-js'
1+
import { createEffect, createMemo, onCleanup, onMount } from 'solid-js'
112
import { onlineManager, useQueryClient } from '@tanstack/solid-query'
12-
import { isServer } from 'solid-js/web'
133
import { TanstackQueryDevtools } from '@tanstack/query-devtools'
144
import type {
155
DevtoolsButtonPosition,
166
DevtoolsErrorType,
177
DevtoolsPosition,
188
} from '@tanstack/query-devtools'
199
import type { QueryClient } from '@tanstack/solid-query'
20-
import type { Component, ComponentProps, JSX } from 'solid-js'
10+
import type { JSX } from 'solid-js'
2111

2212
interface DevtoolsOptions {
2313
/**
@@ -40,6 +30,10 @@ interface DevtoolsOptions {
4030
* Custom instance of QueryClient
4131
*/
4232
client?: QueryClient
33+
/**
34+
* Fallback component to show while the devtools are loading.
35+
*/
36+
fallback?: JSX.Element
4337
/**
4438
* Use this so you can define custom errors that can be shown in the devtools.
4539
*/
@@ -104,36 +98,3 @@ export default function SolidQueryDevtools(props: DevtoolsOptions) {
10498

10599
return <div class="tsqd-parent-container" ref={ref}></div>
106100
}
107-
108-
/*
109-
This function has been taken from solid-start's codebase
110-
This allows the devtools to be loaded only on the client and bypasses any server side rendering
111-
https://github.com/solidjs/solid-start/blob/2967fc2db3f0df826f061020231dbdafdfa0746b/packages/start/islands/clientOnly.tsx
112-
*/
113-
export function clientOnly<T extends Component<any>>(
114-
fn: () => Promise<{
115-
default: T
116-
}>,
117-
) {
118-
if (isServer)
119-
return (props: ComponentProps<T> & { fallback?: JSX.Element }) =>
120-
props.fallback
121-
122-
const [comp, setComp] = createSignal<T>()
123-
fn().then((m) => setComp(() => m.default))
124-
return (props: ComponentProps<T>) => {
125-
let Comp: T | undefined
126-
let m: boolean
127-
const [, rest] = splitProps(props, ['fallback'])
128-
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest)
129-
const [mounted, setMounted] = createSignal(!sharedConfig.context)
130-
onMount(() => setMounted(true))
131-
return createMemo(
132-
() => (
133-
(Comp = comp()),
134-
(m = mounted()),
135-
untrack(() => (Comp && m ? Comp(rest) : props.fallback))
136-
),
137-
)
138-
}
139-
}

packages/solid-query-devtools/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isDev } from 'solid-js/web'
2-
import { clientOnly } from './devtools'
2+
import clientOnly from './clientOnly'
33
import type SolidQueryDevtoolsComp from './devtools'
44

55
export const SolidQueryDevtools: typeof SolidQueryDevtoolsComp = isDev

0 commit comments

Comments
 (0)