Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

app.ts

161 lines3.8 KB

1import { LitElement, css, html } from 'lit'
2import { createQueryController } from '@tanstack/lit-query'
3import { createDataQueryOptions } from './api.js'
4import type { QueryClient, QueryResultAccessor } from '@tanstack/lit-query'
5import type { DataResponse } from './api.js'
6​
7const ssrQueryControllerCreationCounts = new WeakMap<QueryClient, number>()
8​
9function incrementSsrQueryControllerCreationCount(
10 queryClient: QueryClient,
11): void {
12 ssrQueryControllerCreationCounts.set(
13 queryClient,
14 (ssrQueryControllerCreationCounts.get(queryClient) ?? 0) + 1,
15 )
17​
18export function resetSsrQueryControllerCreationCount(
19 queryClient: QueryClient,
20): void {
21 ssrQueryControllerCreationCounts.set(queryClient, 0)
23​
24export function getSsrQueryControllerCreationCount(
25 queryClient: QueryClient,
26): number {
27 return ssrQueryControllerCreationCounts.get(queryClient) ?? 0
29​
30export class SsrApp extends LitElement {
31 static properties = {
32 apiBaseUrl: { attribute: 'api-base-url' },
33 queryClient: { attribute: false },
34 }
35​
36 static styles = css`
37 :host {
38 color: #1f2937;
39 display: block;
40 font-family:
41 ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
42 sans-serif;
43 max-width: 32rem;
44 padding: 1.5rem;
45 }
46​
47 article {
48 background: #ffffff;
49 border: 1px solid #d1d5db;
50 border-radius: 1rem;
51 box-shadow: 0 12px 40px rgba(15, 23, 42, 0.08);
52 padding: 1.25rem;
53 }
54​
55 h1 {
56 font-size: 1.25rem;
57 margin: 0 0 1rem;
58 }
59​
60 p {
61 margin: 0.5rem 0;
62 }
63​
64 button {
65 background: #111827;
66 border: none;
67 border-radius: 999px;
68 color: #ffffff;
69 cursor: pointer;
70 font: inherit;
71 margin-top: 1rem;
72 padding: 0.65rem 1rem;
73 }
74​
75 button[disabled] {
76 cursor: wait;
77 opacity: 0.65;
78 }
79 `
80​
81 apiBaseUrl = ''
82 queryClient?: QueryClient
83​
84 private dataQuery?: QueryResultAccessor<DataResponse, Error>
85​
86 protected override willUpdate(): void {
87 if (!this.dataQuery && this.queryClient) {
88 incrementSsrQueryControllerCreationCount(this.queryClient)
89 this.dataQuery = createQueryController(
90 this,
91 createDataQueryOptions(this.apiBaseUrl),
92 this.queryClient,
93 )
94 }
95 }
96​
97 private readonly handleRefetch = (): void => {
98 void this.dataQuery?.refetch()
99 }
100​
101 protected override render() {
102 if (!this.dataQuery) {
103 return html`
104 <article data-testid="content">
105 <h1>Lit Query SSR</h1>
106 <p data-testid="status">Loading...</p>
107 </article>
108 `
109 }
110​
111 const query = this.dataQuery()
112​
113 if (query.isPending) {
114 return html`
115 <article data-testid="content">
116 <h1>Lit Query SSR</h1>
117 <p data-testid="status">Loading...</p>
118 </article>
119 `
120 }
121​
122 if (query.isError) {
123 return html`
124 <article data-testid="content">
125 <h1>Lit Query SSR</h1>
126 <p data-testid="status">Error</p>
127 <p data-testid="error-message">${query.error.message}</p>
128 </article>
129 `
130 }
131​
132 return html`
133 <article data-testid="content">
134 <h1>Lit Query SSR</h1>
135 <p data-testid="status">${query.isFetching ? 'Refreshing' : 'Ready'}</p>
136 <p data-testid="message">${query.data.message}</p>
137 <p data-testid="request-count">
138 Request count: ${query.data.requestCount}
139 </p>
140 <p data-testid="served-at">Served at: ${query.data.servedAt}</p>
141 <button
142 data-testid="refetch-button"
143 ?disabled=${query.isFetching}
144 @click=${this.handleRefetch}
145 >
146 Refetch
147 </button>
148 </article>
149 `
150 }
152​
153if (!customElements.get('ssr-app')) {
154 customElements.define('ssr-app', SsrApp)
156​
157declare global {
158 interface HTMLElementTagNameMap {
159 'ssr-app': SsrApp
160 }