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

app.ts

167 lines3.8 KB

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