Open History to see the last commit
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 )
18}
19β
20export function resetSsrQueryControllerCreationCount(
21 queryClient: QueryClient,
22): void {
23 ssrQueryControllerCreationCounts.set(queryClient, 0)
24}
25β
26export function getSsrQueryControllerCreationCount(
27 queryClient: QueryClient,
28): number {
29 return ssrQueryControllerCreationCounts.get(queryClient) ?? 0
30}
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, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
44 sans-serif;
45 max-width: 32rem;
46 padding: 1.5rem;
47 }
48β
49 article {
50 background: #ffffff;
51 border: 1px solid #d1d5db;
52 border-radius: 1rem;
53 box-shadow: 0 12px 40px rgba(15, 23, 42, 0.08);
54 padding: 1.25rem;
55 }
56β
57 h1 {
58 font-size: 1.25rem;
59 margin: 0 0 1rem;
60 }
61β
62 p {
63 margin: 0.5rem 0;
64 }
65β
66 button {
67 background: #111827;
68 border: none;
69 border-radius: 999px;
70 color: #ffffff;
71 cursor: pointer;
72 font: inherit;
73 margin-top: 1rem;
74 padding: 0.65rem 1rem;
75 }
76β
77 button[disabled] {
78 cursor: wait;
79 opacity: 0.65;
80 }
81 `
82β
83 apiBaseUrl = ''
84 queryClient?: QueryClient
85β
86 private dataQuery?: QueryResultAccessor<DataResponse, Error>
87β
88 protected override willUpdate(): void {
89 if (!this.dataQuery && this.queryClient) {
90 incrementSsrQueryControllerCreationCount(this.queryClient)
91 this.dataQuery = createQueryController(
92 this,
93 createDataQueryOptions(this.apiBaseUrl),
94 this.queryClient,
95 )
96 }
97 }
98β
99 private readonly handleRefetch = (): void => {
100 void this.dataQuery?.refetch()
101 }
102β
103 protected override render() {
104 if (!this.dataQuery) {
105 return html`
106 <article data-testid="content">
107 <h1>Lit Query SSR</h1>
108 <p data-testid="status">Loading...</p>
109 </article>
110 `
111 }
112β
113 const query = this.dataQuery()
114β
115 if (query.isPending) {
116 return html`
117 <article data-testid="content">
118 <h1>Lit Query SSR</h1>
119 <p data-testid="status">Loading...</p>
120 </article>
121 `
122 }
123β
124 if (query.isError) {
125 return html`
126 <article data-testid="content">
127 <h1>Lit Query SSR</h1>
128 <p data-testid="status">Error</p>
129 <p data-testid="error-message">${query.error?.message}</p>
130 </article>
131 `
132 }
133β
134 return html`
135 <article data-testid="content">
136 <h1>Lit Query SSR</h1>
137 <p data-testid="status">${query.isFetching ? 'Refreshing' : 'Ready'}</p>
138 <p data-testid="message">${query.data.message}</p>
139 <p data-testid="request-count">
140 Request count: ${query.data.requestCount}
141 </p>
142 <p data-testid="served-at">Served at: ${query.data.servedAt}</p>
143 <button
144 data-testid="refetch-button"
145 ?disabled=${query.isFetching}
146 @click=${this.handleRefetch}
147 >
148 Refetch
149 </button>
150 </article>
151 `
152 }
153}
154β
155if (!customElements.get('ssr-app')) {
156 customElements.define('ssr-app', SsrApp)
157}
158β
159declare global {
160 interface HTMLElementTagNameMap {
161 'ssr-app': SsrApp
162 }
163}