Skip to content

Commit f013cff

Browse files
fix: make open-source work with https servers (#99)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 59ecdb6 commit f013cff

File tree

17 files changed

+606
-8
lines changed

17 files changed

+606
-8
lines changed

examples/react/https/.eslintrc.cjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @ts-check
2+
3+
/** @type {import('eslint').Linter.Config} */
4+
const config = {
5+
settings: {
6+
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'],
7+
rules: {
8+
'react/no-children-prop': 'off',
9+
},
10+
},
11+
}
12+
13+
module.exports = config

examples/react/https/.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
pnpm-lock.yaml
15+
yarn.lock
16+
package-lock.json
17+
18+
# misc
19+
.DS_Store
20+
.env.local
21+
.env.development.local
22+
.env.test.local
23+
.env.production.local
24+
25+
npm-debug.log*
26+
yarn-debug.log*
27+
yarn-error.log*

examples/react/https/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Example
2+
3+
To run this example:
4+
5+
- `npm install`
6+
- `npm run dev`

examples/react/https/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" type="image/svg+xml" href="/emblem-light.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="og:image"
10+
content="https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800"
11+
/>
12+
<meta name="og:title" content="Basic Example - TanStack Devtools" />
13+
<meta
14+
name="og:description"
15+
content="A basic example of using TanStack Devtools with React and loading up the social previews"
16+
/>
17+
<meta name="og:url" content="https://example.com/basic" />
18+
19+
<meta
20+
name="twitter:image"
21+
content="https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=800"
22+
/>
23+
<meta name="twitter:title" content="Basic Example - TanStack Devtools" />
24+
<meta
25+
name="twitter:description"
26+
content="A basic example of using TanStack Devtools with React and loading up the social previews"
27+
/>
28+
<meta name="twitter:url" content="https://example.com/basic" />
29+
<script src="https://unpkg.com/react-scan/dist/auto.global.js"></script>
30+
<title>Basic Example - TanStack Devtools</title>
31+
<description
32+
>A basic example of using TanStack Devtools with React.</description
33+
>
34+
</head>
35+
<body>
36+
<noscript>You need to enable JavaScript to run this app.</noscript>
37+
<div id="root"></div>
38+
<script type="module" src="/src/index.tsx"></script>
39+
</body>
40+
</html>

examples/react/https/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@tanstack/devtools-example-react-https",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite --port=3005",
7+
"build": "vite build",
8+
"preview": "vite preview",
9+
"test:types": "tsc"
10+
},
11+
"dependencies": {
12+
"@tanstack/devtools-event-client": "^0.2.3",
13+
"@tanstack/react-devtools": "^0.5.5",
14+
"@tanstack/react-query": "^5.83.0",
15+
"@tanstack/react-query-devtools": "^5.83.0",
16+
"@tanstack/react-router": "^1.130.2",
17+
"@tanstack/react-router-devtools": "^1.130.2",
18+
"react": "^19.1.0",
19+
"react-dom": "^19.1.0",
20+
"zod": "^4.0.14"
21+
},
22+
"devDependencies": {
23+
"@tanstack/devtools-ui": "0.3.3",
24+
"@tanstack/devtools-vite": "0.2.10",
25+
"@types/react": "^19.1.2",
26+
"@types/react-dom": "^19.1.2",
27+
"@vitejs/plugin-react": "^4.5.2",
28+
"vite": "^7.0.6",
29+
"vite-plugin-inspect": "11.3.2",
30+
"vite-plugin-mkcert": "^1.17.8"
31+
},
32+
"browserslist": {
33+
"production": [
34+
">0.2%",
35+
"not dead",
36+
"not op_mini all"
37+
],
38+
"development": [
39+
"last 1 chrome version",
40+
"last 1 firefox version",
41+
"last 1 safari version"
42+
]
43+
}
44+
}
Lines changed: 13 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Button } from './button'
2+
3+
export const ButtonWithProps = (props: { children: React.ReactNode }) => {
4+
return <Button {...props} />
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function Button(props: any) {
2+
return <button {...props} />
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Button } from './button'
2+
import { ButtonWithProps } from './button-with-props-only'
3+
4+
export function Feature() {
5+
return (
6+
<div>
7+
<h2>Feature component</h2>
8+
<Button>Nested</Button>
9+
<ButtonWithProps>With props</ButtonWithProps>
10+
</div>
11+
)
12+
}

examples/react/https/src/index.tsx

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import { createRoot } from 'react-dom/client'
2+
import { createContext, useContext, useState } from 'react'
3+
import { createPortal } from 'react-dom'
4+
import {
5+
QueryClient,
6+
QueryClientProvider,
7+
useQuery,
8+
useQueryClient,
9+
} from '@tanstack/react-query'
10+
import Devtools from './setup'
11+
import { queryPlugin } from './plugin'
12+
import { Button } from './button'
13+
import { Feature } from './feature'
14+
15+
const queryClient = new QueryClient({
16+
defaultOptions: {
17+
queries: {
18+
gcTime: 1000 * 60 * 60 * 24, // 24 hours
19+
},
20+
},
21+
})
22+
23+
type Post = {
24+
id: number
25+
title: string
26+
body: string
27+
}
28+
29+
function Posts({
30+
setPostId,
31+
}: {
32+
setPostId: React.Dispatch<React.SetStateAction<number>>
33+
}) {
34+
const queryClient = useQueryClient()
35+
const { status, data, error, isFetching } = usePosts()
36+
37+
return (
38+
<div>
39+
<h1>Posts</h1>
40+
<div>
41+
{status === 'pending' ? (
42+
'Loading...'
43+
) : status === 'error' ? (
44+
<span>Error: {error.message}</span>
45+
) : (
46+
<>
47+
<div>
48+
{data.map((post) => (
49+
<p key={post.id}>
50+
<a
51+
onClick={() => setPostId(post.id)}
52+
href="#"
53+
style={
54+
// We can access the query data here to show bold links for
55+
// ones that are cached
56+
queryClient.getQueryData(['post', post.id])
57+
? {
58+
fontWeight: 'bold',
59+
color: 'green',
60+
}
61+
: {}
62+
}
63+
>
64+
{post.title}
65+
</a>
66+
</p>
67+
))}
68+
</div>
69+
<div>{isFetching ? 'Background Updating...' : ' '}</div>
70+
</>
71+
)}
72+
</div>
73+
</div>
74+
)
75+
}
76+
77+
const getPostById = async (id: number): Promise<Post> => {
78+
const response = await fetch(
79+
`https://jsonplaceholder.typicode.com/posts/${id}`,
80+
)
81+
return await response.json()
82+
}
83+
84+
function usePost(postId: number) {
85+
return useQuery({
86+
queryKey: ['post', postId],
87+
queryFn: () => getPostById(postId),
88+
enabled: !!postId,
89+
})
90+
}
91+
92+
function Post({
93+
postId,
94+
setPostId,
95+
}: {
96+
postId: number
97+
setPostId: React.Dispatch<React.SetStateAction<number>>
98+
}) {
99+
const { status, data, error, isFetching } = usePost(postId)
100+
101+
return (
102+
<div>
103+
<div>
104+
<a onClick={() => setPostId(-1)} href="#">
105+
Back
106+
</a>
107+
</div>
108+
{!postId || status === 'pending' ? (
109+
'Loading...'
110+
) : status === 'error' ? (
111+
<span>Error: {error.message}</span>
112+
) : (
113+
<>
114+
<h1>{data.title}</h1>
115+
<div>
116+
<p>{data.body}</p>
117+
</div>
118+
<div>{isFetching ? 'Background Updating...' : ' '}</div>
119+
</>
120+
)}
121+
</div>
122+
)
123+
}
124+
function usePosts() {
125+
return useQuery({
126+
queryKey: ['posts'],
127+
queryFn: async (): Promise<Array<Post>> => {
128+
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
129+
return await response.json()
130+
},
131+
})
132+
}
133+
134+
const Context = createContext<{
135+
count: number
136+
setCount: (count: number) => void
137+
}>({ count: 0, setCount: () => {} })
138+
139+
setTimeout(() => {
140+
queryPlugin.emit('test', {
141+
title: 'Test Event',
142+
description:
143+
'This is a test event from the TanStack Query Devtools plugin.',
144+
})
145+
}, 1000)
146+
147+
queryPlugin.on('test', (event) => {
148+
console.log('Received test event:', event)
149+
})
150+
151+
function Mounted() {
152+
const c = useContext(Context)
153+
console.log(c)
154+
return (
155+
<p
156+
onClick={() => {
157+
c.setCount(c.count + 1)
158+
}}
159+
>
160+
{c.count}
161+
<hr />
162+
</p>
163+
)
164+
}
165+
166+
function App() {
167+
const [state, setState] = useState(1)
168+
const [win, setWin] = useState<Window | null>(null)
169+
const [postId, setPostId] = useState(-1)
170+
return (
171+
<div>
172+
<Context.Provider value={{ count: state, setCount: setState }}>
173+
<QueryClientProvider client={queryClient}>
174+
<h1>TanStack Devtools React Basic Example</h1>
175+
current count: {state}
176+
<Button onClick={() => setState(state + 1)}>Click me</Button>
177+
<Button onClick={() => setWin(window.open('', '', 'popup'))}>
178+
Click me to open new window
179+
</Button>
180+
{win && createPortal(<Mounted />, win.document.body)}
181+
<Feature />
182+
<p>
183+
As you visit the posts below, you will notice them in a loading
184+
state the first time you load them. However, after you return to
185+
this list and click on any posts you have already visited again, you
186+
will see them load instantly and background refresh right before
187+
your eyes!{' '}
188+
<strong>
189+
(You may need to throttle your network speed to simulate longer
190+
loading sequences)
191+
</strong>
192+
</p>
193+
{postId > -1 ? (
194+
<Post postId={postId} setPostId={setPostId} />
195+
) : (
196+
<Posts setPostId={setPostId} />
197+
)}
198+
<Devtools />
199+
</QueryClientProvider>
200+
</Context.Provider>
201+
</div>
202+
)
203+
}
204+
205+
const root = createRoot(document.getElementById('root')!)
206+
root.render(<App />)

0 commit comments

Comments
 (0)