@tenphi/tasty 0.0.0-snapshot.002b1b3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +637 -0
- package/dist/async-storage-B7_o6FKt.js +44 -0
- package/dist/async-storage-B7_o6FKt.js.map +1 -0
- package/dist/collector-LuU1vZ68.d.ts +98 -0
- package/dist/collector-MOYY2SOr.js +230 -0
- package/dist/collector-MOYY2SOr.js.map +1 -0
- package/dist/config-A237aY9H.js +10235 -0
- package/dist/config-A237aY9H.js.map +1 -0
- package/dist/config-vuCRkBWX.d.ts +884 -0
- package/dist/context-CkSg-kDT.js +24 -0
- package/dist/context-CkSg-kDT.js.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +6 -0
- package/dist/core-BkKav78f.js +1592 -0
- package/dist/core-BkKav78f.js.map +1 -0
- package/dist/css-writer-Cos9tQRM.js +329 -0
- package/dist/css-writer-Cos9tQRM.js.map +1 -0
- package/dist/format-global-rules-Dbc_1tc3.js +22 -0
- package/dist/format-global-rules-Dbc_1tc3.js.map +1 -0
- package/dist/format-rules-C2oiTsEO.js +143 -0
- package/dist/format-rules-C2oiTsEO.js.map +1 -0
- package/dist/hydrate-miFzWIKR.js +45 -0
- package/dist/hydrate-miFzWIKR.js.map +1 -0
- package/dist/index-CJMXAAO5.d.ts +1602 -0
- package/dist/index-dUtwpOux.d.ts +1266 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +732 -0
- package/dist/index.js.map +1 -0
- package/dist/keyframes-DDtNo_hl.js +587 -0
- package/dist/keyframes-DDtNo_hl.js.map +1 -0
- package/dist/merge-styles-CtDJMhpJ.d.ts +7 -0
- package/dist/merge-styles-D_HbBOlq.js +144 -0
- package/dist/merge-styles-D_HbBOlq.js.map +1 -0
- package/dist/resolve-recipes-B7-823LL.js +144 -0
- package/dist/resolve-recipes-B7-823LL.js.map +1 -0
- package/dist/ssr/astro-client.d.ts +1 -0
- package/dist/ssr/astro-client.js +19 -0
- package/dist/ssr/astro-client.js.map +1 -0
- package/dist/ssr/astro-middleware.d.ts +15 -0
- package/dist/ssr/astro-middleware.js +19 -0
- package/dist/ssr/astro-middleware.js.map +1 -0
- package/dist/ssr/astro.d.ts +97 -0
- package/dist/ssr/astro.js +149 -0
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/index.d.ts +44 -0
- package/dist/ssr/index.js +10 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/next.d.ts +46 -0
- package/dist/ssr/next.js +75 -0
- package/dist/ssr/next.js.map +1 -0
- package/dist/static/index.d.ts +91 -0
- package/dist/static/index.js +50 -0
- package/dist/static/index.js.map +1 -0
- package/dist/static/inject.d.ts +5 -0
- package/dist/static/inject.js +17 -0
- package/dist/static/inject.js.map +1 -0
- package/dist/zero/babel.d.ts +81 -0
- package/dist/zero/babel.js +466 -0
- package/dist/zero/babel.js.map +1 -0
- package/dist/zero/index.d.ts +67 -0
- package/dist/zero/index.js +2 -0
- package/dist/zero/next.d.ts +86 -0
- package/dist/zero/next.js +143 -0
- package/dist/zero/next.js.map +1 -0
- package/docs/README.md +31 -0
- package/docs/adoption.md +298 -0
- package/docs/comparison.md +419 -0
- package/docs/configuration.md +394 -0
- package/docs/debug.md +320 -0
- package/docs/design-system.md +436 -0
- package/docs/dsl.md +688 -0
- package/docs/getting-started.md +217 -0
- package/docs/injector.md +544 -0
- package/docs/methodology.md +616 -0
- package/docs/pipeline.md +673 -0
- package/docs/react-api.md +557 -0
- package/docs/ssr.md +442 -0
- package/docs/styles.md +596 -0
- package/docs/tasty-static.md +532 -0
- package/package.json +222 -0
- package/tasty.config.ts +15 -0
package/docs/ssr.md
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
# Server-Side Rendering (SSR)
|
|
2
|
+
|
|
3
|
+
Tasty supports server-side rendering with zero-cost client hydration. This does **not** introduce a separate styling engine: SSR uses the same runtime `tasty()` pipeline you already use on the client, then adds server-side CSS collection and client-side cache hydration. Your existing `tasty()` components work unchanged, and SSR remains opt-in with no per-component modifications. For the broader docs map, see the [Docs Hub](README.md).
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
| Dependency | Version | Required for |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `react` | >= 18 | All SSR entry points (matches the current peer dependency of `@tenphi/tasty`) |
|
|
12
|
+
| `next` | >= 13 | Next.js integration (`@tenphi/tasty/ssr/next`) — App Router with `useServerInsertedHTML` |
|
|
13
|
+
| Node.js | >= 20 | Generic / streaming SSR (`@tenphi/tasty/ssr`) — uses `node:async_hooks` for `AsyncLocalStorage` |
|
|
14
|
+
|
|
15
|
+
The Astro integration (`@tenphi/tasty/ssr/astro`) has no additional dependencies beyond `react`.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## How It Works
|
|
20
|
+
|
|
21
|
+
`tasty()` components are hook-free and use `computeStyles()` internally — a synchronous, framework-agnostic function. On the server, `computeStyles()` discovers a `ServerStyleCollector` via a registered getter (module-level for Next.js, `globalThis` for Astro/generic frameworks using `AsyncLocalStorage`) and collects CSS into it instead of trying to access the DOM. On the client, CSS is injected synchronously into the DOM during render; the injector's content-based cache makes this idempotent. The collector accumulates all styles, serializes them as `<style>` tags and a cache state script in the HTML. On the client, `hydrateTastyCache()` pre-populates the injector cache so that `computeStyles()` skips the rendering pipeline entirely during hydration.
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
Server Client
|
|
25
|
+
────── ──────
|
|
26
|
+
tasty() renders hydrateTastyCache() pre-populates cache
|
|
27
|
+
└─ computeStyles() └─ cacheKey → className map ready
|
|
28
|
+
└─ collector.collect()
|
|
29
|
+
tasty() renders
|
|
30
|
+
After render: └─ computeStyles()
|
|
31
|
+
<style data-tasty-ssr> └─ cache hit → skip pipeline
|
|
32
|
+
<script data-tasty-cache> └─ no CSS re-injection
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Next.js (App Router)
|
|
38
|
+
|
|
39
|
+
### 1. Create the registry
|
|
40
|
+
|
|
41
|
+
Create a client component that wraps your tree with `TastyRegistry`:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
// app/tasty-registry.tsx
|
|
45
|
+
'use client';
|
|
46
|
+
|
|
47
|
+
import { TastyRegistry } from '@tenphi/tasty/ssr/next';
|
|
48
|
+
|
|
49
|
+
export default function TastyStyleRegistry({
|
|
50
|
+
children,
|
|
51
|
+
}: {
|
|
52
|
+
children: React.ReactNode;
|
|
53
|
+
}) {
|
|
54
|
+
return <TastyRegistry>{children}</TastyRegistry>;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 2. Add to root layout
|
|
59
|
+
|
|
60
|
+
Wrap your application in the registry:
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
// app/layout.tsx
|
|
64
|
+
import TastyStyleRegistry from './tasty-registry';
|
|
65
|
+
|
|
66
|
+
export default function RootLayout({
|
|
67
|
+
children,
|
|
68
|
+
}: {
|
|
69
|
+
children: React.ReactNode;
|
|
70
|
+
}) {
|
|
71
|
+
return (
|
|
72
|
+
<html>
|
|
73
|
+
<body>
|
|
74
|
+
<TastyStyleRegistry>{children}</TastyStyleRegistry>
|
|
75
|
+
</body>
|
|
76
|
+
</html>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
That's it. All `tasty()` components inside the tree automatically get SSR support. No per-component changes needed.
|
|
82
|
+
|
|
83
|
+
### How it works
|
|
84
|
+
|
|
85
|
+
- `TastyRegistry` is a `'use client'` component, but Next.js still server-renders it on initial page load. The `'use client'` boundary is required solely to access `useServerInsertedHTML` — **not** because `tasty()` components need the client.
|
|
86
|
+
- During SSR, `TastyRegistry` creates a `ServerStyleCollector` and registers it via a module-level getter (not `globalThis` — this avoids leaking between Next.js's separate RSC and SSR module graphs). It also wraps children in a React context provider so that hooks inside the SSR tree can discover the collector. All style functions — `tasty()` components, `computeStyles()`, `useStyles()`, `useGlobalStyles()`, `useRawCSS()`, `useKeyframes()`, `useProperty()`, `useFontFace()`, and `useCounterStyle()` — discover the collector through the module-level getter or context provider.
|
|
87
|
+
- `TastyRegistry` uses `useServerInsertedHTML` to flush collected CSS into the HTML stream as `<style data-tasty-ssr>` tags. This is fully streaming-compatible — styles are injected alongside each Suspense boundary as it resolves.
|
|
88
|
+
- A companion inline `<script>` tag merges the `cacheKey → className` mapping into `window.__TASTY_SSR_CACHE__` for each flush. This streaming-friendly approach accumulates cache entries incrementally as Suspense boundaries resolve.
|
|
89
|
+
- When the `@tenphi/tasty/ssr/next` module loads on the client, `hydrateTastyCache()` runs automatically from `window.__TASTY_SSR_CACHE__` and pre-populates the injector cache. During hydration, `computeStyles()` hits the cache and skips the entire pipeline.
|
|
90
|
+
|
|
91
|
+
### Using Tasty in Server Components
|
|
92
|
+
|
|
93
|
+
All Tasty style functions are hook-free and do not require `'use client'`. They can be used directly in React Server Components:
|
|
94
|
+
|
|
95
|
+
- `tasty()` components — dynamic `styleProps` like `<Grid flow="column">` work normally
|
|
96
|
+
- `useStyles()`, `useGlobalStyles()`, `useRawCSS()` — inject styles by class or selector
|
|
97
|
+
- `useKeyframes()`, `useProperty()`, `useFontFace()`, `useCounterStyle()` — inject ancillary CSS rules
|
|
98
|
+
|
|
99
|
+
During SSR, all functions discover the collector via the same global getter registered by `TastyRegistry` — no React context or client boundary needed. In RSC mode without a collector (e.g., Astro zero-setup), CSS is accumulated in a per-request cache and flushed into an inline `<style>` tag by the next `tasty()` component in the tree. Ensure at least one `tasty()` component is present in every RSC render tree — standalone style functions alone cannot emit their CSS without a `tasty()` component to trigger the flush.
|
|
100
|
+
|
|
101
|
+
### Options
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
// Skip cache state transfer (saves payload size at the cost of hydration perf)
|
|
105
|
+
<TastyRegistry transferCache={false}>{children}</TastyRegistry>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### CSP nonce
|
|
109
|
+
|
|
110
|
+
If your app uses Content Security Policy with nonces, configure it before rendering:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
// app/layout.tsx or a server-side init file
|
|
114
|
+
import { configure } from '@tenphi/tasty';
|
|
115
|
+
|
|
116
|
+
configure({ nonce: 'your-nonce-value' });
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The nonce is automatically applied to all `<style>` and `<script>` tags injected by `TastyRegistry`.
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## Astro
|
|
124
|
+
|
|
125
|
+
Tasty offers three levels of Astro integration. Choose the one that matches your needs:
|
|
126
|
+
|
|
127
|
+
| Setup | Config needed | Deduplication | Hooks work | Client JS |
|
|
128
|
+
|---|---|---|---|---|
|
|
129
|
+
| Zero setup | None | Per render tree | Yes (within each tree) | None |
|
|
130
|
+
| `tastyIntegration({ islands: false })` | One line | Cross-tree | Yes | None |
|
|
131
|
+
| `tastyIntegration()` | One line | Cross-tree | Yes | Auto-hydration |
|
|
132
|
+
|
|
133
|
+
### Zero setup (static pages)
|
|
134
|
+
|
|
135
|
+
`tasty()` components work in Astro with **no configuration**. Each component emits its own inline `<style>` tag during server rendering via the RSC inline path. Just import and use:
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
// src/components/Card.tsx
|
|
139
|
+
import { tasty } from '@tenphi/tasty';
|
|
140
|
+
|
|
141
|
+
const Card = tasty({
|
|
142
|
+
styles: {
|
|
143
|
+
padding: '4x',
|
|
144
|
+
fill: '#surface',
|
|
145
|
+
radius: '1r',
|
|
146
|
+
border: true,
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
export default Card;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
```astro
|
|
154
|
+
---
|
|
155
|
+
// src/pages/index.astro
|
|
156
|
+
import Card from '../components/Card.tsx';
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
<html>
|
|
160
|
+
<body>
|
|
161
|
+
<Card>Styled with zero setup</Card>
|
|
162
|
+
</body>
|
|
163
|
+
</html>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**Trade-offs**: Styles are deduplicated within each React render tree, but Astro renders separate component trees independently, so shared CSS (tokens, `@property` rules) may appear more than once. All style functions (`useGlobalStyles`, `useRawCSS`, `useKeyframes`, `useProperty`, `useFontFace`, `useCounterStyle`) work in zero-setup mode — their CSS is accumulated in the RSC cache and flushed by the next `tasty()` component in the tree.
|
|
167
|
+
|
|
168
|
+
Best for quick prototyping, small static sites, or trying Tasty out in Astro.
|
|
169
|
+
|
|
170
|
+
### Astro Integration (recommended)
|
|
171
|
+
|
|
172
|
+
For production use, add `tastyIntegration()` to your Astro config. This registers middleware automatically and, by default, injects client-side hydration for islands.
|
|
173
|
+
|
|
174
|
+
#### With islands (default)
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
// astro.config.mjs
|
|
178
|
+
import { defineConfig } from 'astro/config';
|
|
179
|
+
import react from '@astrojs/react';
|
|
180
|
+
import { tastyIntegration } from '@tenphi/tasty/ssr/astro';
|
|
181
|
+
|
|
182
|
+
export default defineConfig({
|
|
183
|
+
integrations: [react(), tastyIntegration()],
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
This gives you:
|
|
188
|
+
|
|
189
|
+
- A `ServerStyleCollector` per request via `AsyncLocalStorage`, deduplicating CSS across all React trees on the page
|
|
190
|
+
- A single consolidated `<style data-tasty-ssr>` injected into `</head>`
|
|
191
|
+
- A `<script data-tasty-cache>` tag with the `cacheKey -> className` map for client hydration
|
|
192
|
+
- Auto-injected client hydration script (via `injectScript('before-hydration')`) so islands skip the style pipeline during hydration -- no need to import anything manually in each island component
|
|
193
|
+
|
|
194
|
+
All style functions (`useGlobalStyles`, `useRawCSS`, `useKeyframes`, `useProperty`, `useFontFace`, `useCounterStyle`) work on the server.
|
|
195
|
+
|
|
196
|
+
```astro
|
|
197
|
+
---
|
|
198
|
+
// src/pages/index.astro
|
|
199
|
+
import Card from '../components/Card.tsx';
|
|
200
|
+
import Interactive from '../components/Interactive.tsx';
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
<html>
|
|
204
|
+
<body>
|
|
205
|
+
<Card>Static -- styles in <style data-tasty-ssr></Card>
|
|
206
|
+
<Interactive client:load>Island -- cache hydrated automatically</Interactive>
|
|
207
|
+
</body>
|
|
208
|
+
</html>
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Static only (no client JS)
|
|
212
|
+
|
|
213
|
+
If your site has no `client:*` islands, skip the hydration script and cache transfer:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
// astro.config.mjs
|
|
217
|
+
import { defineConfig } from 'astro/config';
|
|
218
|
+
import react from '@astrojs/react';
|
|
219
|
+
import { tastyIntegration } from '@tenphi/tasty/ssr/astro';
|
|
220
|
+
|
|
221
|
+
export default defineConfig({
|
|
222
|
+
integrations: [react(), tastyIntegration({ islands: false })],
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
This gives the same middleware deduplication and hook support, but ships zero client-side JavaScript. No `<script data-tasty-cache>` is emitted.
|
|
227
|
+
|
|
228
|
+
### Manual middleware (advanced)
|
|
229
|
+
|
|
230
|
+
If you need to compose Tasty's middleware with other middleware (e.g., via `sequence()`), use `tastyMiddleware()` directly:
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
// src/middleware.ts
|
|
234
|
+
import { sequence } from 'astro:middleware';
|
|
235
|
+
import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
236
|
+
|
|
237
|
+
export const onRequest = sequence(
|
|
238
|
+
tastyMiddleware(),
|
|
239
|
+
myOtherMiddleware,
|
|
240
|
+
);
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
For island hydration with manual middleware, import the client module in a shared entry point or in each island:
|
|
244
|
+
|
|
245
|
+
```tsx
|
|
246
|
+
import '@tenphi/tasty/ssr/astro-client';
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
#### Options
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
// Skip cache state transfer (static-only, no islands)
|
|
253
|
+
export const onRequest = tastyMiddleware({ transferCache: false });
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### How it works
|
|
257
|
+
|
|
258
|
+
Astro's `@astrojs/react` renderer calls `renderToString()` for each React component without wrapping the tree in a provider. The middleware creates a `ServerStyleCollector` and binds it via `AsyncLocalStorage`. All `computeStyles()` calls within the request discover this collector automatically.
|
|
259
|
+
|
|
260
|
+
- **Static components** (no `client:*`): Styles are collected during `renderToString` and injected into `</head>` as a single `<style>` tag. No JavaScript is shipped.
|
|
261
|
+
- **Islands** (`client:load`, `client:visible`, etc.): Styles are collected during SSR the same way. On the client, the hydration script (auto-injected by `tastyIntegration()` or manually via `@tenphi/tasty/ssr/astro-client`) reads the cache state from `<script data-tasty-cache>` and pre-populates the injector. The island's `computeStyles()` calls hit the cache during hydration.
|
|
262
|
+
- The middleware reads the full response body, then injects the collected CSS into `</head>` before sending the final HTML.
|
|
263
|
+
|
|
264
|
+
### CSP nonce
|
|
265
|
+
|
|
266
|
+
Call `configure({ nonce: '...' })` before any rendering happens. The middleware reads the nonce and applies it to injected `<style>` and `<script>` tags.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Generic Framework Integration
|
|
271
|
+
|
|
272
|
+
Any React-based framework can integrate using `runWithCollector`, which binds a `ServerStyleCollector` to the current async context via `AsyncLocalStorage`. All style function calls within the render automatically discover the collector.
|
|
273
|
+
|
|
274
|
+
```tsx
|
|
275
|
+
import {
|
|
276
|
+
ServerStyleCollector,
|
|
277
|
+
runWithCollector,
|
|
278
|
+
hydrateTastyCache,
|
|
279
|
+
} from '@tenphi/tasty/ssr';
|
|
280
|
+
import { renderToString } from 'react-dom/server';
|
|
281
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
282
|
+
|
|
283
|
+
// ── Server ──────────────────────────────────────────────
|
|
284
|
+
|
|
285
|
+
const collector = new ServerStyleCollector();
|
|
286
|
+
|
|
287
|
+
const html = await runWithCollector(collector, () =>
|
|
288
|
+
renderToString(<App />)
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const css = collector.getCSS();
|
|
292
|
+
const cacheState = collector.getCacheState();
|
|
293
|
+
|
|
294
|
+
// Embed in your HTML template:
|
|
295
|
+
const fullHtml = `
|
|
296
|
+
<html>
|
|
297
|
+
<head>
|
|
298
|
+
<style data-tasty-ssr>${css}</style>
|
|
299
|
+
<script data-tasty-cache type="application/json">
|
|
300
|
+
${JSON.stringify(cacheState)}
|
|
301
|
+
</script>
|
|
302
|
+
</head>
|
|
303
|
+
<body>
|
|
304
|
+
<div id="root">${html}</div>
|
|
305
|
+
</body>
|
|
306
|
+
</html>
|
|
307
|
+
`;
|
|
308
|
+
|
|
309
|
+
// ── Client ──────────────────────────────────────────────
|
|
310
|
+
|
|
311
|
+
// Before hydration:
|
|
312
|
+
hydrateTastyCache(); // reads from <script data-tasty-cache>
|
|
313
|
+
|
|
314
|
+
hydrateRoot(document.getElementById('root'), <App />);
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### Streaming SSR
|
|
318
|
+
|
|
319
|
+
For streaming with `renderToPipeableStream`, use `flushCSS()` instead of `getCSS()`:
|
|
320
|
+
|
|
321
|
+
```tsx
|
|
322
|
+
const collector = new ServerStyleCollector();
|
|
323
|
+
|
|
324
|
+
const stream = await runWithCollector(collector, () =>
|
|
325
|
+
renderToPipeableStream(<App />, {
|
|
326
|
+
onShellReady() {
|
|
327
|
+
// Flush styles collected so far
|
|
328
|
+
const css = collector.flushCSS();
|
|
329
|
+
res.write(`<style data-tasty-ssr>${css}</style>`);
|
|
330
|
+
stream.pipe(res);
|
|
331
|
+
},
|
|
332
|
+
onAllReady() {
|
|
333
|
+
// Flush any remaining styles + cache state
|
|
334
|
+
const css = collector.flushCSS();
|
|
335
|
+
if (css) res.write(`<style data-tasty-ssr>${css}</style>`);
|
|
336
|
+
|
|
337
|
+
const state = collector.getCacheState();
|
|
338
|
+
res.write(`<script data-tasty-cache type="application/json">${JSON.stringify(state)}</script>`);
|
|
339
|
+
},
|
|
340
|
+
})
|
|
341
|
+
);
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## API Reference
|
|
347
|
+
|
|
348
|
+
### Entry points
|
|
349
|
+
|
|
350
|
+
| Import path | Description |
|
|
351
|
+
|---|---|
|
|
352
|
+
| `@tenphi/tasty/ssr` | Core SSR API: `ServerStyleCollector`, `runWithCollector`, `hydrateTastyCache` |
|
|
353
|
+
| `@tenphi/tasty/ssr/next` | Next.js App Router: `TastyRegistry` component |
|
|
354
|
+
| `@tenphi/tasty/ssr/astro` | Astro: `tastyIntegration`, `tastyMiddleware` |
|
|
355
|
+
| `@tenphi/tasty/ssr/astro-client` | Astro: client-side cache hydration (auto-injected by integration, or import manually) |
|
|
356
|
+
|
|
357
|
+
### `ServerStyleCollector`
|
|
358
|
+
|
|
359
|
+
Server-safe style collector. One instance per request.
|
|
360
|
+
|
|
361
|
+
| Method | Description |
|
|
362
|
+
|---|---|
|
|
363
|
+
| `allocateClassName(cacheKey)` | Allocate a sequential class name (`t0`, `t1`, ...) for a cache key. Returns `{ className, isNewAllocation }`. |
|
|
364
|
+
| `collectChunk(cacheKey, className, rules)` | Record CSS rules for a chunk. Deduplicated by `cacheKey`. |
|
|
365
|
+
| `collectKeyframes(name, css)` | Record a `@keyframes` rule. Deduplicated by name. |
|
|
366
|
+
| `allocateKeyframeName(providedName?)` | Allocate a keyframe name. Returns `providedName` if given, otherwise generates one (`k0`, `k1`, ...). |
|
|
367
|
+
| `collectProperty(name, css)` | Record a `@property` rule. Deduplicated by name. |
|
|
368
|
+
| `collectFontFace(key, css)` | Record a `@font-face` rule. Deduplicated by content hash. |
|
|
369
|
+
| `collectCounterStyle(name, css)` | Record a `@counter-style` rule. Deduplicated by name. |
|
|
370
|
+
| `allocateCounterStyleName(providedName?)` | Allocate a counter-style name. Returns `providedName` if given, otherwise generates one (`cs0`, `cs1`, ...). |
|
|
371
|
+
| `collectGlobalStyles(key, css)` | Record global styles (from `useGlobalStyles`). Deduplicated by key. |
|
|
372
|
+
| `collectRawCSS(key, css)` | Record raw CSS text (from `useRawCSS`). Deduplicated by key. |
|
|
373
|
+
| `collectInternals()` | Collect internal `@property` rules, `:root` token defaults, `@font-face`, and `@counter-style` rules from the global config. Called automatically on first chunk collection; idempotent. |
|
|
374
|
+
| `getCSS()` | Get all collected CSS as a single string. For non-streaming SSR. |
|
|
375
|
+
| `flushCSS()` | Get only CSS collected since the last flush. For streaming SSR. |
|
|
376
|
+
| `getCacheState()` | Serialize `{ entries: Record<cacheKey, className>, classCounter }` for client hydration. |
|
|
377
|
+
|
|
378
|
+
### `TastyRegistry`
|
|
379
|
+
|
|
380
|
+
Next.js App Router component. Props:
|
|
381
|
+
|
|
382
|
+
| Prop | Type | Default | Description |
|
|
383
|
+
|---|---|---|---|
|
|
384
|
+
| `children` | `ReactNode` | required | Application tree |
|
|
385
|
+
| `transferCache` | `boolean` | `true` | Embed cache state script for zero-cost hydration |
|
|
386
|
+
|
|
387
|
+
### `tastyIntegration(options?)`
|
|
388
|
+
|
|
389
|
+
Astro integration factory. Registers middleware and optionally injects client hydration.
|
|
390
|
+
|
|
391
|
+
| Option | Type | Default | Description |
|
|
392
|
+
|---|---|---|---|
|
|
393
|
+
| `islands` | `boolean` | `true` | When `true`, injects client hydration script and enables `transferCache`. When `false`, no client JS is shipped. |
|
|
394
|
+
|
|
395
|
+
### `tastyMiddleware(options?)`
|
|
396
|
+
|
|
397
|
+
Astro middleware factory. Use for manual middleware composition.
|
|
398
|
+
|
|
399
|
+
| Option | Type | Default | Description |
|
|
400
|
+
|---|---|---|---|
|
|
401
|
+
| `transferCache` | `boolean` | `true` | Embed cache state script for island hydration |
|
|
402
|
+
|
|
403
|
+
### `hydrateTastyCache(state?)`
|
|
404
|
+
|
|
405
|
+
Pre-populate the client injector cache. When called without arguments, reads from `window.__TASTY_SSR_CACHE__` (streaming) or `<script data-tasty-cache>` (non-streaming).
|
|
406
|
+
|
|
407
|
+
### `runWithCollector(collector, fn)`
|
|
408
|
+
|
|
409
|
+
Run a function with a `ServerStyleCollector` bound to the current async context via `AsyncLocalStorage`. All style function calls within `fn` (and async continuations) — including `computeStyles()`, `useStyles()`, `useGlobalStyles()`, `useRawCSS()`, `useKeyframes()`, `useProperty()`, `useFontFace()`, and `useCounterStyle()` — will find this collector.
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
|
|
413
|
+
## Troubleshooting
|
|
414
|
+
|
|
415
|
+
### Styles flash on page load (FOUC)
|
|
416
|
+
|
|
417
|
+
The `TastyRegistry` or `tastyIntegration` is missing. Ensure your layout wraps the app with `TastyRegistry` (Next.js) or that `tastyIntegration()` is in your Astro config (or `tastyMiddleware()` is registered manually).
|
|
418
|
+
|
|
419
|
+
### Hydration mismatch warnings
|
|
420
|
+
|
|
421
|
+
Class names are deterministic for the same render order. If you see mismatches, ensure `hydrateTastyCache()` runs before React hydration. For Next.js, this is automatic. For Astro with `tastyIntegration()`, this is also automatic. For manual Astro middleware setups, import `@tenphi/tasty/ssr/astro-client` in your island components. For custom setups, call `hydrateTastyCache()` before `hydrateRoot()`.
|
|
422
|
+
|
|
423
|
+
### Styles duplicated after hydration
|
|
424
|
+
|
|
425
|
+
**Global CSS** (`:root` tokens, `@property`, `globalStyles`, `@font-face`, `@counter-style`) configured via `configure()` is automatically deduplicated. When Tasty detects `<style data-tasty-ssr>` in the document, it skips client-side injection of globals that were already rendered by the SSR collector. This means `configure()` can be called with the full config on both server and client — no `typeof window === 'undefined'` guard is needed.
|
|
426
|
+
|
|
427
|
+
**Component CSS**: SSR `<style data-tasty-ssr>` tags remain in the DOM. The client injector creates separate `<style>` elements for any new styles. SSR styles are never modified or removed by the client. If this is a concern for very large apps, call `cleanupSSRStyles()` after hydration:
|
|
428
|
+
|
|
429
|
+
```tsx
|
|
430
|
+
import { hydrateTastyCache } from '@tenphi/tasty/ssr';
|
|
431
|
+
|
|
432
|
+
hydrateTastyCache();
|
|
433
|
+
hydrateRoot(root, <App />);
|
|
434
|
+
|
|
435
|
+
// Optional: remove SSR style tags after hydration
|
|
436
|
+
document.querySelectorAll('style[data-tasty-ssr]').forEach(el => el.remove());
|
|
437
|
+
document.querySelectorAll('script[data-tasty-cache]').forEach(el => el.remove());
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### `AsyncLocalStorage` not available
|
|
441
|
+
|
|
442
|
+
The `@tenphi/tasty/ssr` entry point imports from `node:async_hooks`. This is excluded from client bundles by the build configuration. If you see import errors on the client, ensure your bundler treats `node:async_hooks` as external or use the `@tenphi/tasty/ssr/next` entry point (which does not use ALS).
|