Open History to see the last commit
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 )
16}
17β
18export function resetSsrQueryControllerCreationCount(
19 queryClient: QueryClient,
20): void {
21 ssrQueryControllerCreationCounts.set(queryClient, 0)
22}
23β
24export function getSsrQueryControllerCreationCount(
25 queryClient: QueryClient,
26): number {
27 return ssrQueryControllerCreationCounts.get(queryClient) ?? 0
28}
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 }
151}
152β
153if (!customElements.get('ssr-app')) {
154 customElements.define('ssr-app', SsrApp)
155}
156β
157declare global {
158 interface HTMLElementTagNameMap {
159 'ssr-app': SsrApp
160 }
161}