@tenphi/tasty 0.0.0-snapshot.8dc3efa → 0.0.0-snapshot.8efdc31
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/README.md +440 -372
- package/dist/babel-types-docs8f4c.d.ts +18 -0
- package/dist/chunks/async-storage-f9j9JRtq.js +37 -0
- package/dist/chunks/async-storage-f9j9JRtq.js.map +1 -0
- package/dist/chunks/build-config-DmSCYjYl.js +60 -0
- package/dist/chunks/build-config-DmSCYjYl.js.map +1 -0
- package/dist/{collector-CBddDz-s.js → chunks/collector-8w5r-xFl.js} +68 -101
- package/dist/chunks/collector-8w5r-xFl.js.map +1 -0
- package/dist/chunks/config-engine-DjRv6Axa.js +272 -0
- package/dist/chunks/config-engine-DjRv6Axa.js.map +1 -0
- package/dist/chunks/config-file-DlZXJMj8.js +25 -0
- package/dist/chunks/config-file-DlZXJMj8.js.map +1 -0
- package/dist/chunks/css-definitions-_UidGa56.js +910 -0
- package/dist/chunks/css-definitions-_UidGa56.js.map +1 -0
- package/dist/chunks/css-resources-P7IBUUZs.js +142 -0
- package/dist/chunks/css-resources-P7IBUUZs.js.map +1 -0
- package/dist/chunks/debug-B6dZq3uH.js +502 -0
- package/dist/chunks/debug-B6dZq3uH.js.map +1 -0
- package/dist/chunks/dsl-rgu6XQcD.js +1386 -0
- package/dist/chunks/dsl-rgu6XQcD.js.map +1 -0
- package/dist/chunks/hydration-TRcz5vnk.js +41 -0
- package/dist/chunks/hydration-TRcz5vnk.js.map +1 -0
- package/dist/chunks/react-runtime-DS1cEAMK.js +689 -0
- package/dist/chunks/react-runtime-DS1cEAMK.js.map +1 -0
- package/dist/chunks/runtime-engine-DFCZN1nF.js +2432 -0
- package/dist/chunks/runtime-engine-DFCZN1nF.js.map +1 -0
- package/dist/chunks/runtime-state-C8MzPjBR.js +152 -0
- package/dist/chunks/runtime-state-C8MzPjBR.js.map +1 -0
- package/dist/{merge-styles-CWRl0mKF.js → chunks/shared-utils-y-HjvPN4.js} +60 -55
- package/dist/chunks/shared-utils-y-HjvPN4.js.map +1 -0
- package/dist/chunks/style-engine-DoGrZPBB.js +5576 -0
- package/dist/chunks/style-engine-DoGrZPBB.js.map +1 -0
- package/dist/{css-writer-DPR4cAyK.js → chunks/zero-engine-CiVidroC.js} +95 -136
- package/dist/chunks/zero-engine-CiVidroC.js.map +1 -0
- package/dist/{collector-osfWTeRd.d.ts → collector-CTU-nl0g.d.ts} +64 -8
- package/dist/{config-BoZDUHW5.d.ts → config-DDY7NlZ1.d.ts} +514 -92
- package/dist/core/index.d.ts +5 -5
- package/dist/core/index.js +9 -6
- package/dist/hydrate-CRyjjAyl.d.ts +22 -0
- package/dist/{index-B_k47mc_.d.ts → index-DOxyS72p.d.ts} +714 -407
- package/dist/index.d.ts +5 -5
- package/dist/index.js +11 -732
- package/dist/{merge-styles-BMWcH6MF.d.ts → merge-styles-DSkVc6CD.d.ts} +2 -3
- package/dist/ssr/astro-client.d.ts +1 -1
- package/dist/ssr/astro-client.js +1 -14
- package/dist/ssr/astro-client.js.map +1 -1
- package/dist/ssr/astro-middleware-extract-static.d.ts +10 -0
- package/dist/ssr/astro-middleware-extract-static.js +9 -0
- package/dist/ssr/astro-middleware-extract-static.js.map +1 -0
- package/dist/ssr/astro-middleware-extract.d.ts +10 -0
- package/dist/ssr/astro-middleware-extract.js +9 -0
- package/dist/ssr/astro-middleware-extract.js.map +1 -0
- package/dist/ssr/astro-middleware-static.d.ts +17 -0
- package/dist/ssr/astro-middleware-static.js +7 -0
- package/dist/ssr/astro-middleware-static.js.map +1 -0
- package/dist/ssr/astro-middleware.d.ts +11 -8
- package/dist/ssr/astro-middleware.js +2 -14
- package/dist/ssr/astro-middleware.js.map +1 -1
- package/dist/ssr/astro.d.ts +40 -10
- package/dist/ssr/astro.js +163 -91
- package/dist/ssr/astro.js.map +1 -1
- package/dist/ssr/client.d.ts +2 -0
- package/dist/ssr/client.js +2 -0
- package/dist/ssr/index.d.ts +5 -32
- package/dist/ssr/index.js +6 -6
- package/dist/ssr/index.js.map +1 -1
- package/dist/ssr/next-config.d.ts +64 -0
- package/dist/ssr/next-config.js +123 -0
- package/dist/ssr/next-config.js.map +1 -0
- package/dist/ssr/next.d.ts +9 -9
- package/dist/ssr/next.js +29 -44
- package/dist/ssr/next.js.map +1 -1
- package/dist/static/index.d.ts +8 -9
- package/dist/static/index.js +4 -22
- package/dist/static/index.js.map +1 -1
- package/dist/static/inject.d.ts +1 -2
- package/dist/static/inject.js.map +1 -1
- package/dist/{index-tcHuMPFt.d.ts → types-DJEM_BOJ.d.ts} +879 -598
- package/dist/zero/babel.d.ts +12 -20
- package/dist/zero/babel.js +97 -156
- package/dist/zero/babel.js.map +1 -1
- package/dist/zero/index.d.ts +76 -7
- package/dist/zero/index.js +25 -2
- package/dist/zero/index.js.map +1 -0
- package/dist/zero/next.d.ts +4 -5
- package/dist/zero/next.js +3 -38
- package/dist/zero/next.js.map +1 -1
- package/docs/README.md +20 -9
- package/docs/adoption.md +69 -36
- package/docs/ai-agents.md +258 -0
- package/docs/comparison.md +77 -47
- package/docs/configuration.md +617 -98
- package/docs/debug.md +28 -15
- package/docs/design-system.md +154 -33
- package/docs/dsl.md +663 -194
- package/docs/getting-started.md +130 -50
- package/docs/injector.md +160 -69
- package/docs/methodology.md +82 -51
- package/docs/migration-v3.md +285 -0
- package/docs/pipeline.md +102 -33
- package/docs/plugins.md +333 -0
- package/docs/react-api.md +213 -48
- package/docs/runtime-benchmarks.md +388 -0
- package/docs/ssr.md +310 -118
- package/docs/styles.md +177 -40
- package/docs/tasty-static.md +289 -136
- package/package.json +146 -41
- package/tasty.config.ts +5 -0
- package/dist/async-storage-B7_o6FKt.js +0 -44
- package/dist/async-storage-B7_o6FKt.js.map +0 -1
- package/dist/collector-CBddDz-s.js.map +0 -1
- package/dist/config-dkNtxY1j.js +0 -10462
- package/dist/config-dkNtxY1j.js.map +0 -1
- package/dist/context-CkSg-kDT.js +0 -24
- package/dist/context-CkSg-kDT.js.map +0 -1
- package/dist/core-CWJ4JX5P.js +0 -1598
- package/dist/core-CWJ4JX5P.js.map +0 -1
- package/dist/css-writer-DPR4cAyK.js.map +0 -1
- package/dist/format-global-rules-Dbc_1tc3.js +0 -22
- package/dist/format-global-rules-Dbc_1tc3.js.map +0 -1
- package/dist/format-rules-BnfFVBQE.js +0 -143
- package/dist/format-rules-BnfFVBQE.js.map +0 -1
- package/dist/hydrate-Chagc_CN.js +0 -45
- package/dist/hydrate-Chagc_CN.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/keyframes-Dbe0HQAg.js +0 -588
- package/dist/keyframes-Dbe0HQAg.js.map +0 -1
- package/dist/merge-styles-CWRl0mKF.js.map +0 -1
- package/dist/resolve-recipes-DZBMeDib.js +0 -144
- package/dist/resolve-recipes-DZBMeDib.js.map +0 -1
package/docs/ssr.md
CHANGED
|
@@ -1,35 +1,41 @@
|
|
|
1
1
|
# Server-Side Rendering (SSR)
|
|
2
2
|
|
|
3
|
-
Tasty supports server-side rendering with zero-cost client hydration. This does **not** introduce a separate styling engine:
|
|
3
|
+
Tasty supports server-side rendering with zero-cost client hydration. This does **not** introduce a separate styling engine: `tasty()` uses the same rendering pipeline on the server and in the browser, while the SSR integrations add 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
|
+
## Zero-runtime terminology
|
|
6
|
+
|
|
7
|
+
Zero-runtime delivery is an outcome, not an alias for `tastyStatic()`. When `tasty()` components render only on the server, their CSS is delivered with the HTML and no Tasty styling runtime is shipped to the browser. Astro's `tastyIntegration({ islands: false })` is the explicit integration for this setup. Server-only Next.js React Server Components follow the same architecture, although you should verify the generated output for your deployment.
|
|
8
|
+
|
|
9
|
+
`tastyStatic()` reaches the same client-side outcome by extracting CSS during the build instead of during React rendering. Use it when extraction must happen before rendering or when the consumer is not React; see [Build-Time Extraction](tasty-static.md).
|
|
4
10
|
|
|
5
11
|
---
|
|
6
12
|
|
|
7
13
|
## Requirements
|
|
8
14
|
|
|
9
|
-
| Dependency | Version | Required for
|
|
10
|
-
|
|
11
|
-
| `react`
|
|
12
|
-
| `next`
|
|
13
|
-
| Node.js
|
|
15
|
+
| Dependency | Version | Required for |
|
|
16
|
+
| ---------- | ------- | ----------------------------------------------------------------------------------------------- |
|
|
17
|
+
| `react` | >= 18 | All SSR entry points (matches the current peer dependency of `@tenphi/tasty`) |
|
|
18
|
+
| `next` | >= 14 | Next.js integration (`@tenphi/tasty/ssr/next`) — App Router with `useServerInsertedHTML` |
|
|
19
|
+
| Node.js | >= 20 | Generic / streaming SSR (`@tenphi/tasty/ssr`) — uses `node:async_hooks` for `AsyncLocalStorage` |
|
|
14
20
|
|
|
15
|
-
The Astro integration (`@tenphi/tasty/ssr/astro`)
|
|
21
|
+
The Astro integration (`@tenphi/tasty/ssr/astro`) needs `@astrojs/react`. Like `@tenphi/tasty/ssr`, it imports `node:async_hooks`, so the server runtime must provide it. `css: { mode: 'extract' }` requires Astro 5 or newer.
|
|
16
22
|
|
|
17
23
|
---
|
|
18
24
|
|
|
19
25
|
## How It Works
|
|
20
26
|
|
|
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
|
|
27
|
+
`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 and serializes them as `<style>` tags plus a class-list script in the HTML. On the client, `hydrateTastyClasses()` pre-populates the injector's rules map with the rendered class names so that `computeStyles()` skips the rendering pipeline entirely during hydration.
|
|
22
28
|
|
|
23
29
|
```
|
|
24
30
|
Server Client
|
|
25
31
|
────── ──────
|
|
26
|
-
tasty() renders
|
|
27
|
-
└─ computeStyles() └─
|
|
32
|
+
tasty() renders hydrateTastyClasses() reads window.__TASTY__
|
|
33
|
+
└─ computeStyles() └─ marks rendered class names as already-in-DOM
|
|
28
34
|
└─ collector.collect()
|
|
29
35
|
tasty() renders
|
|
30
36
|
After render: └─ computeStyles()
|
|
31
|
-
<style data-tasty-ssr>
|
|
32
|
-
<script
|
|
37
|
+
<style data-tasty-ssr> └─ class name known → skip pipeline
|
|
38
|
+
<script> (pushes to __TASTY__) └─ no CSS re-injection
|
|
33
39
|
```
|
|
34
40
|
|
|
35
41
|
---
|
|
@@ -80,23 +86,115 @@ export default function RootLayout({
|
|
|
80
86
|
|
|
81
87
|
That's it. All `tasty()` components inside the tree automatically get SSR support. No per-component changes needed.
|
|
82
88
|
|
|
89
|
+
### Optional shared globals stylesheet
|
|
90
|
+
|
|
91
|
+
By default, configured global CSS is included in the streamed Tasty style tag
|
|
92
|
+
for every route. `withTastyNext()` can move that stable CSS into one
|
|
93
|
+
content-hashed stylesheet shared by all routes while leaving component and hook
|
|
94
|
+
styles in the normal streaming path.
|
|
95
|
+
|
|
96
|
+
Keep the Tasty config in a side-effect-free module:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
// app/tasty.config.ts
|
|
100
|
+
import type { TastyConfig } from '@tenphi/tasty';
|
|
101
|
+
|
|
102
|
+
const config: TastyConfig = {
|
|
103
|
+
tokens: { $gap: '8px', '#brand': 'rebeccapurple' },
|
|
104
|
+
globalStyles: { body: { margin: '0', color: '#brand' } },
|
|
105
|
+
fontFaces: {
|
|
106
|
+
Brand: { src: 'url("/fonts/brand.woff2") format("woff2")' },
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export default config;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Use it from the Next config:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
// next.config.ts
|
|
117
|
+
import { withTastyNext } from '@tenphi/tasty/ssr/next-config';
|
|
118
|
+
import config from './app/tasty.config';
|
|
119
|
+
|
|
120
|
+
export default withTastyNext({
|
|
121
|
+
config,
|
|
122
|
+
})({
|
|
123
|
+
// your Next.js config
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The runtime must receive the same config. Import and configure it before the
|
|
128
|
+
registry renders:
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
// app/tasty-registry.tsx
|
|
132
|
+
'use client';
|
|
133
|
+
|
|
134
|
+
import { configure } from '@tenphi/tasty';
|
|
135
|
+
import { TastyRegistry } from '@tenphi/tasty/ssr/next';
|
|
136
|
+
import config from './tasty.config';
|
|
137
|
+
|
|
138
|
+
configure(config);
|
|
139
|
+
|
|
140
|
+
export default function TastyStyleRegistry({
|
|
141
|
+
children,
|
|
142
|
+
}: {
|
|
143
|
+
children: React.ReactNode;
|
|
144
|
+
}) {
|
|
145
|
+
return <TastyRegistry>{children}</TastyRegistry>;
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The generated file contains eager configuration artifacts: built-in and custom
|
|
150
|
+
`@property` rules, tokens and presets, `@font-face`, `@counter-style`, native
|
|
151
|
+
CSS `@function` definitions, and `globalStyles`. Route-dependent component
|
|
152
|
+
rules and calls to `useGlobalStyles`, `useRawCSS`, `useKeyframes`,
|
|
153
|
+
`useProperty`, `useFontFace`, `useCounterStyle`, and `useFunction` remain
|
|
154
|
+
route-specific. Configured keyframes referenced by `globalStyles` are included in the file; all
|
|
155
|
+
other configured keyframes stay lazy and stream only when a route references
|
|
156
|
+
them.
|
|
157
|
+
|
|
158
|
+
The default output directory is `public/_tasty`. The wrapper adds the generated
|
|
159
|
+
URL to `TastyRegistry`, respects `basePath`, preserves existing `env` and
|
|
160
|
+
`headers` config, and serves the content-hashed file with an immutable one-year
|
|
161
|
+
cache header on Next server deployments. Static exports keep the content-hashed
|
|
162
|
+
URL and leave cache headers to the hosting provider. Older hashes are not
|
|
163
|
+
deleted automatically, so rolling deployments cannot break pages from the
|
|
164
|
+
previous build; clean the generated directory as part of a clean deployment if
|
|
165
|
+
needed. Page-relative CSS resources such as
|
|
166
|
+
`url(../fonts/brand.woff2)` are rejected because moving them would change their
|
|
167
|
+
meaning; use root-relative, absolute, or data URLs.
|
|
168
|
+
|
|
169
|
+
`withTastyNext()` options:
|
|
170
|
+
|
|
171
|
+
| Option | Type | Default | Description |
|
|
172
|
+
| ------------ | ------------- | ------------------------- | ---------------------------------------------------------------------- |
|
|
173
|
+
| `config` | `TastyConfig` | — | Config object; takes precedence over `configFile` |
|
|
174
|
+
| `configFile` | `string` | — | Project-relative config module path; requires the optional `jiti` peer |
|
|
175
|
+
| `rootDir` | `string` | current directory | Next app root when the build runs from a monorepo root |
|
|
176
|
+
| `outputDir` | `string` | `public/_tasty` | Filesystem output directory |
|
|
177
|
+
| `publicPath` | `string` | inferred from `outputDir` | Root-relative URL; required when output is outside `public` |
|
|
178
|
+
| `enabled` | `boolean` | `true` | Disable generation without changing wrapper composition |
|
|
179
|
+
|
|
83
180
|
### How it works
|
|
84
181
|
|
|
85
182
|
- `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 `
|
|
183
|
+
- 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()`, `useCounterStyle()`, and `useFunction()` — discover the collector through the module-level getter or context provider.
|
|
87
184
|
- `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
|
|
89
|
-
- When the `@tenphi/tasty/ssr/next` module loads on the client, `
|
|
185
|
+
- A companion inline `<script>` tag pushes the rendered class names into `window.__TASTY__` for each flush. This streaming-friendly approach accumulates the class list incrementally as Suspense boundaries resolve.
|
|
186
|
+
- When the `@tenphi/tasty/ssr/next` module loads on the client, `hydrateTastyClasses()` runs automatically from `window.__TASTY__` and pre-populates the injector's rules map. During hydration, `computeStyles()` sees the class name is already registered and skips the entire pipeline.
|
|
90
187
|
|
|
91
188
|
### Using Tasty in Server Components
|
|
92
189
|
|
|
93
|
-
|
|
190
|
+
Every Tasty style function needs no `'use client'`, so it can be used directly in React Server Components:
|
|
94
191
|
|
|
95
192
|
- `tasty()` components — dynamic `styleProps` like `<Grid flow="column">` work normally
|
|
96
|
-
- `useStyles()
|
|
97
|
-
- `
|
|
193
|
+
- `useStyles()` and its hook-free equivalent `computeStyles()` — a class name for any element
|
|
194
|
+
- `useGlobalStyles()`, `useRawCSS()` — inject styles by selector
|
|
195
|
+
- `useKeyframes()`, `useProperty()`, `useFontFace()`, `useCounterStyle()`, `useFunction()` — inject ancillary CSS rules
|
|
98
196
|
|
|
99
|
-
|
|
197
|
+
Next.js renders Server Components in a separate module graph that cannot see the collector `TastyRegistry` registers, so their CSS takes the RSC inline path: it is accumulated in a per-request `React.cache` and emitted as an inline `<style>` tag by the next `tasty()` component in the tree. Standalone style functions, `useStyles()` included, cannot emit CSS on their own, so make sure a `tasty()` component renders after them in every RSC tree. The RSC path does not emit configured globals (tokens, `@property`, `globalStyles`); those come from `TastyRegistry`'s collector or the `withTastyNext()` shared stylesheet. For the same reason, import the module that calls `configure()` from `app/layout.tsx` as well as from the registry module, so the RSC graph sees the same states, recipes, and units.
|
|
100
198
|
|
|
101
199
|
### Options
|
|
102
200
|
|
|
@@ -110,25 +208,27 @@ During SSR, all functions discover the collector via the same global getter regi
|
|
|
110
208
|
If your app uses Content Security Policy with nonces, configure it before rendering:
|
|
111
209
|
|
|
112
210
|
```tsx
|
|
113
|
-
// app/
|
|
211
|
+
// app/tasty-registry.tsx: the 'use client' module that renders TastyRegistry
|
|
114
212
|
import { configure } from '@tenphi/tasty';
|
|
115
213
|
|
|
116
214
|
configure({ nonce: 'your-nonce-value' });
|
|
117
215
|
```
|
|
118
216
|
|
|
119
|
-
The nonce is automatically applied to all `<style>` and `<script>` tags injected by `TastyRegistry`.
|
|
217
|
+
The nonce is automatically applied to all `<style>` and `<script>` tags injected by `TastyRegistry`. Call it in the module that renders `TastyRegistry`; `app/layout.tsx` is a Server Component and configures a separate module instance.
|
|
120
218
|
|
|
121
219
|
---
|
|
122
220
|
|
|
123
221
|
## Astro
|
|
124
222
|
|
|
125
|
-
Tasty offers
|
|
223
|
+
Tasty offers several levels of Astro integration. Choose the one that matches your needs:
|
|
126
224
|
|
|
127
|
-
| Setup
|
|
128
|
-
|
|
129
|
-
| Zero setup | None
|
|
130
|
-
| `tastyIntegration({ islands: false })`
|
|
131
|
-
| `tastyIntegration()`
|
|
225
|
+
| Setup | Config needed | Deduplication | Hooks work | Client JS |
|
|
226
|
+
| ---------------------------------------------------------------- | ------------- | ----------------------------------- | --------------------------------- | -------------- |
|
|
227
|
+
| Zero setup | None | None (each element inlines its CSS) | No (style functions emit nothing) | None |
|
|
228
|
+
| `tastyIntegration({ islands: false })` | One line | Cross-tree | Yes | None |
|
|
229
|
+
| `tastyIntegration()` | One line | Cross-tree | Yes | Auto-hydration |
|
|
230
|
+
| `tastyIntegration({ css: { mode: 'extract' } })` | One line | Cross-tree and cross-page | Yes | Auto-hydration |
|
|
231
|
+
| `tastyIntegration({ islands: false, css: { mode: 'extract' } })` | One line | Cross-tree and cross-page | Yes | None |
|
|
132
232
|
|
|
133
233
|
### Zero setup (static pages)
|
|
134
234
|
|
|
@@ -141,9 +241,9 @@ import { tasty } from '@tenphi/tasty';
|
|
|
141
241
|
const Card = tasty({
|
|
142
242
|
styles: {
|
|
143
243
|
padding: '4x',
|
|
144
|
-
fill: '#
|
|
244
|
+
fill: '#fff',
|
|
145
245
|
radius: '1r',
|
|
146
|
-
border:
|
|
246
|
+
border: '1bw solid #ddd',
|
|
147
247
|
},
|
|
148
248
|
});
|
|
149
249
|
|
|
@@ -163,9 +263,9 @@ import Card from '../components/Card.tsx';
|
|
|
163
263
|
</html>
|
|
164
264
|
```
|
|
165
265
|
|
|
166
|
-
**Trade-offs**:
|
|
266
|
+
**Trade-offs**: Astro renders React with the standard `react` build, where `React.cache` does nothing, so there is no per-request style cache. Every `tasty()` element inlines the full CSS for its classes, even when the same component renders twice. Configured globals (`:root` tokens, `@property` defaults, `globalStyles`, `@font-face`) are never emitted, because only the SSR collector emits them, and CSS from `useGlobalStyles`, `useRawCSS`, `useKeyframes`, `useProperty`, `useFontFace`, `useCounterStyle`, and `useFunction` is dropped. Without the [default properties](dsl.md#default-properties), styles that read them resolve to nothing: `border: true` and `outline: true` draw nothing, and `transition` with no duration does not animate. In development, Tasty warns once that it is rendering without a style collector.
|
|
167
267
|
|
|
168
|
-
Best for
|
|
268
|
+
Best for trying Tasty out with plain values, as above. Use `tastyIntegration()` for anything that relies on `configure()` or on Tasty's default tokens.
|
|
169
269
|
|
|
170
270
|
### Astro Integration (recommended)
|
|
171
271
|
|
|
@@ -188,7 +288,7 @@ This gives you:
|
|
|
188
288
|
|
|
189
289
|
- A `ServerStyleCollector` per request via `AsyncLocalStorage`, deduplicating CSS across all React trees on the page
|
|
190
290
|
- A single consolidated `<style data-tasty-ssr>` injected into `</head>`
|
|
191
|
-
- A `<script
|
|
291
|
+
- A `<script>` tag pushing the rendered class names into `window.__TASTY__` for client hydration
|
|
192
292
|
- 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
293
|
|
|
194
294
|
All style functions (`useGlobalStyles`, `useRawCSS`, `useKeyframes`, `useProperty`, `useFontFace`, `useCounterStyle`) work on the server.
|
|
@@ -202,7 +302,7 @@ import Interactive from '../components/Interactive.tsx';
|
|
|
202
302
|
|
|
203
303
|
<html>
|
|
204
304
|
<body>
|
|
205
|
-
<Card>Static -- styles
|
|
305
|
+
<Card>Static -- styles are inlined into the page head</Card>
|
|
206
306
|
<Interactive client:load>Island -- cache hydrated automatically</Interactive>
|
|
207
307
|
</body>
|
|
208
308
|
</html>
|
|
@@ -223,7 +323,64 @@ export default defineConfig({
|
|
|
223
323
|
});
|
|
224
324
|
```
|
|
225
325
|
|
|
226
|
-
This gives the same middleware deduplication and hook support, but ships zero client-side JavaScript. No `<script
|
|
326
|
+
This gives the same middleware deduplication and hook support, but ships zero client-side JavaScript. No class-list `<script>` is emitted.
|
|
327
|
+
|
|
328
|
+
#### Build-wide CSS extraction
|
|
329
|
+
|
|
330
|
+
Static Astro builds can move Tasty CSS into content-hashed, browser-cacheable
|
|
331
|
+
shared and page assets:
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
export default defineConfig({
|
|
335
|
+
integrations: [
|
|
336
|
+
react(),
|
|
337
|
+
tastyIntegration({
|
|
338
|
+
islands: false,
|
|
339
|
+
css: {
|
|
340
|
+
mode: 'extract',
|
|
341
|
+
},
|
|
342
|
+
}),
|
|
343
|
+
],
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
`css.mode` defaults to `'inline'`, so existing projects keep their current
|
|
348
|
+
output. Extraction requires Astro 5 or newer and only applies to prerendered
|
|
349
|
+
production pages. Development, preview-time SSR, and on-demand routes continue
|
|
350
|
+
to receive the normal inline `<style data-tasty-ssr>` output.
|
|
351
|
+
|
|
352
|
+
Extraction writes every artifact emitted by all styled pages to a shared
|
|
353
|
+
stylesheet. Each page's strict set difference is written to a separate page
|
|
354
|
+
stylesheet. The shared link comes first and the page link follows, so shared
|
|
355
|
+
styles form the base cascade and page-only styles can override them. A fully
|
|
356
|
+
shared page omits the empty page stylesheet. If generated pages have no common
|
|
357
|
+
artifacts, each page receives only its page stylesheet.
|
|
358
|
+
|
|
359
|
+
The shared-base/page-override order is the extraction-mode cascade contract.
|
|
360
|
+
It does not preserve an inline artifact order where a page-only rule originally
|
|
361
|
+
appeared before a shared rule. Use shared styles for defaults and page-only
|
|
362
|
+
styles for overrides.
|
|
363
|
+
|
|
364
|
+
Extracted CSS preserves resource URLs verbatim. Relative URLs in an inline
|
|
365
|
+
style resolve from the page, but in an extracted stylesheet they resolve from
|
|
366
|
+
the asset directory. Use absolute URLs or data URLs when extraction is enabled.
|
|
367
|
+
Root-relative URLs such as `url(/fonts/brand.woff2)` are also safe while the
|
|
368
|
+
stylesheet stays on the page's origin. The build fails with a clear error if an
|
|
369
|
+
artifact contains a page-relative or fragment-only URL, including URL strings
|
|
370
|
+
in `image-set()`, `image()`, `src()`, and `@import`.
|
|
371
|
+
|
|
372
|
+
Assets are written under Astro's configured `build.assets` directory (for
|
|
373
|
+
example, `/_astro/tasty.shared.a1b2c3.css` and
|
|
374
|
+
`/_astro/tasty.page.d4e5f6.css`). Links include the configured Astro `base`, so
|
|
375
|
+
nested routes do not need relative-path handling. Content hashes and output are
|
|
376
|
+
deterministic for identical builds. If `build.assetsPrefix` is configured,
|
|
377
|
+
Tasty uses its CSS-specific prefix (or `fallback`) just like Astro-generated
|
|
378
|
+
stylesheets. When that prefix points to a different origin, root-relative
|
|
379
|
+
resources would resolve against the asset origin rather than the page's origin,
|
|
380
|
+
so the build rejects them as well. Tasty compares the prefix with Astro's `site`
|
|
381
|
+
when it is configured; without `site`, an absolute or protocol-relative prefix
|
|
382
|
+
is treated conservatively as cross-origin. Use a fully absolute resource URL in
|
|
383
|
+
that configuration.
|
|
227
384
|
|
|
228
385
|
### Manual middleware (advanced)
|
|
229
386
|
|
|
@@ -234,10 +391,7 @@ If you need to compose Tasty's middleware with other middleware (e.g., via `sequ
|
|
|
234
391
|
import { sequence } from 'astro:middleware';
|
|
235
392
|
import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
236
393
|
|
|
237
|
-
export const onRequest = sequence(
|
|
238
|
-
tastyMiddleware(),
|
|
239
|
-
myOtherMiddleware,
|
|
240
|
-
);
|
|
394
|
+
export const onRequest = sequence(tastyMiddleware(), myOtherMiddleware);
|
|
241
395
|
```
|
|
242
396
|
|
|
243
397
|
For island hydration with manual middleware, import the client module in a shared entry point or in each island:
|
|
@@ -253,17 +407,24 @@ import '@tenphi/tasty/ssr/astro-client';
|
|
|
253
407
|
export const onRequest = tastyMiddleware({ transferCache: false });
|
|
254
408
|
```
|
|
255
409
|
|
|
410
|
+
Build-wide extraction (`css: { mode: 'extract' }`) is available only through `tastyIntegration()`; `tastyMiddleware()` always inlines CSS.
|
|
411
|
+
|
|
256
412
|
### How it works
|
|
257
413
|
|
|
258
414
|
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
415
|
|
|
260
416
|
- **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
|
|
417
|
+
- **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 class list from `window.__TASTY__` and pre-populates the injector's rules map. The island's `computeStyles()` calls see the class names as already registered and skip the pipeline during hydration.
|
|
262
418
|
- The middleware reads the full response body, then injects the collected CSS into `</head>` before sending the final HTML.
|
|
419
|
+
- In extraction mode, prerendered responses also carry temporary structured
|
|
420
|
+
artifact metadata. `astro:build:done` uses those collector-provided
|
|
421
|
+
boundaries to write shared and page assets and rewrite generated HTML, then
|
|
422
|
+
removes the metadata. Artifact boundaries are never inferred by splitting or
|
|
423
|
+
reparsing the generated CSS.
|
|
263
424
|
|
|
264
425
|
### CSP nonce
|
|
265
426
|
|
|
266
|
-
Call `configure({ nonce: '...' })` before any rendering happens. The middleware reads the nonce and applies it to injected `<style>` and `<script>` tags.
|
|
427
|
+
Call `configure({ nonce: '...' })` before any rendering happens. The middleware reads the nonce and applies it to injected `<style>` and `<script>` tags. In extraction mode, the external stylesheet links retain the nonce.
|
|
267
428
|
|
|
268
429
|
---
|
|
269
430
|
|
|
@@ -272,73 +433,82 @@ Call `configure({ nonce: '...' })` before any rendering happens. The middleware
|
|
|
272
433
|
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
434
|
|
|
274
435
|
```tsx
|
|
436
|
+
// ── Server ──────────────────────────────────────────────
|
|
275
437
|
import {
|
|
276
|
-
|
|
438
|
+
createServerStyleCollector,
|
|
277
439
|
runWithCollector,
|
|
278
|
-
hydrateTastyCache,
|
|
279
440
|
} from '@tenphi/tasty/ssr';
|
|
280
441
|
import { renderToString } from 'react-dom/server';
|
|
281
|
-
import { hydrateRoot } from 'react-dom/client';
|
|
282
|
-
|
|
283
|
-
// ── Server ──────────────────────────────────────────────
|
|
284
442
|
|
|
285
|
-
const collector =
|
|
443
|
+
const collector = createServerStyleCollector();
|
|
286
444
|
|
|
287
|
-
const html =
|
|
288
|
-
renderToString(<App />)
|
|
289
|
-
);
|
|
445
|
+
const html = runWithCollector(collector, () => renderToString(<App />));
|
|
290
446
|
|
|
291
447
|
const css = collector.getCSS();
|
|
292
|
-
const
|
|
448
|
+
const classNames = collector.getRenderedClassNames();
|
|
293
449
|
|
|
294
450
|
// Embed in your HTML template:
|
|
295
451
|
const fullHtml = `
|
|
296
452
|
<html>
|
|
297
453
|
<head>
|
|
298
454
|
<style data-tasty-ssr>${css}</style>
|
|
299
|
-
<script
|
|
300
|
-
${JSON.stringify(cacheState)}
|
|
301
|
-
</script>
|
|
455
|
+
<script>(window.__TASTY__=window.__TASTY__||[]).push(${JSON.stringify(classNames)})</script>
|
|
302
456
|
</head>
|
|
303
457
|
<body>
|
|
304
458
|
<div id="root">${html}</div>
|
|
305
459
|
</body>
|
|
306
460
|
</html>
|
|
307
461
|
`;
|
|
462
|
+
```
|
|
308
463
|
|
|
464
|
+
```tsx
|
|
309
465
|
// ── Client ──────────────────────────────────────────────
|
|
466
|
+
import { hydrateTastyClasses } from '@tenphi/tasty/ssr/client';
|
|
467
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
310
468
|
|
|
311
|
-
//
|
|
312
|
-
hydrateTastyCache(); // reads from <script data-tasty-cache>
|
|
313
|
-
|
|
469
|
+
hydrateTastyClasses(); // reads window.__TASTY__
|
|
314
470
|
hydrateRoot(document.getElementById('root'), <App />);
|
|
315
471
|
```
|
|
316
472
|
|
|
473
|
+
`@tenphi/tasty/ssr/client` is the browser half: it exports `hydrateTastyClasses()` and has no import side effects. A module shared by the server and the client can import `@tenphi/tasty/ssr` too. It is safe in a browser bundle, where there is no server collector to find, and only `runWithCollector()` throws there.
|
|
474
|
+
|
|
317
475
|
### Streaming SSR
|
|
318
476
|
|
|
319
477
|
For streaming with `renderToPipeableStream`, use `flushCSS()` instead of `getCSS()`:
|
|
320
478
|
|
|
321
479
|
```tsx
|
|
322
|
-
|
|
480
|
+
import { PassThrough } from 'node:stream';
|
|
481
|
+
import { renderToPipeableStream } from 'react-dom/server';
|
|
482
|
+
|
|
483
|
+
const collector = createServerStyleCollector();
|
|
484
|
+
|
|
485
|
+
// React ends the stream it pipes into, so pipe into a PassThrough and
|
|
486
|
+
// append the remaining styles and the class list after it ends.
|
|
487
|
+
const body = new PassThrough();
|
|
488
|
+
body.pipe(res, { end: false });
|
|
489
|
+
body.on('end', () => {
|
|
490
|
+
// Styles collected after the shell (Suspense boundaries) + class list
|
|
491
|
+
const css = collector.flushCSS();
|
|
492
|
+
if (css) res.write(`<style data-tasty-ssr>${css}</style>`);
|
|
493
|
+
|
|
494
|
+
const classNames = collector.getRenderedClassNames();
|
|
495
|
+
res.end(
|
|
496
|
+
`</div><script>(window.__TASTY__=window.__TASTY__||[]).push(${JSON.stringify(classNames)})</script></body></html>`,
|
|
497
|
+
);
|
|
498
|
+
});
|
|
323
499
|
|
|
324
|
-
|
|
325
|
-
renderToPipeableStream(<App />, {
|
|
500
|
+
runWithCollector(collector, () => {
|
|
501
|
+
const { pipe } = renderToPipeableStream(<App />, {
|
|
326
502
|
onShellReady() {
|
|
327
|
-
//
|
|
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
|
|
503
|
+
// Styles collected for the shell
|
|
334
504
|
const css = collector.flushCSS();
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
505
|
+
res.write(
|
|
506
|
+
`<!DOCTYPE html><html><head><style data-tasty-ssr>${css}</style></head><body><div id="root">`,
|
|
507
|
+
);
|
|
508
|
+
pipe(body);
|
|
339
509
|
},
|
|
340
|
-
})
|
|
341
|
-
);
|
|
510
|
+
});
|
|
511
|
+
});
|
|
342
512
|
```
|
|
343
513
|
|
|
344
514
|
---
|
|
@@ -347,68 +517,82 @@ const stream = await runWithCollector(collector, () =>
|
|
|
347
517
|
|
|
348
518
|
### Entry points
|
|
349
519
|
|
|
350
|
-
| Import path
|
|
351
|
-
|
|
352
|
-
| `@tenphi/tasty/ssr`
|
|
353
|
-
| `@tenphi/tasty/ssr/
|
|
354
|
-
| `@tenphi/tasty/ssr/
|
|
355
|
-
| `@tenphi/tasty/ssr/
|
|
520
|
+
| Import path | Description |
|
|
521
|
+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
522
|
+
| `@tenphi/tasty/ssr` | Core SSR API: `ServerStyleCollector`, `createServerStyleCollector`, `runWithCollector`, `getSSRCollector`, `hydrateTastyClasses`; types `ServerStyleArtifact`, `ServerStyleArtifactKind`. Safe to import in a browser bundle; `runWithCollector()` needs the server |
|
|
523
|
+
| `@tenphi/tasty/ssr/client` | Browser-safe client hydration for any framework: `hydrateTastyClasses`, no import side effects |
|
|
524
|
+
| `@tenphi/tasty/ssr/next` | Next.js App Router: `TastyRegistry` component |
|
|
525
|
+
| `@tenphi/tasty/ssr/next-config` | Next.js config wrapper: shared, content-hashed global stylesheet |
|
|
526
|
+
| `@tenphi/tasty/ssr/astro` | Astro: `tastyIntegration`, `tastyMiddleware` |
|
|
527
|
+
| `@tenphi/tasty/ssr/astro-client` | Astro: client-side cache hydration (auto-injected by integration, or import manually) |
|
|
528
|
+
| `@tenphi/tasty/ssr/astro-middleware`<br />`@tenphi/tasty/ssr/astro-middleware-static`<br />`@tenphi/tasty/ssr/astro-middleware-extract`<br />`@tenphi/tasty/ssr/astro-middleware-extract-static` | Astro: the middleware entrypoints `tastyIntegration()` registers via `addMiddleware()`. Exported so Astro can resolve them by specifier; you should not import them. For manual setups use `tastyMiddleware()`. |
|
|
356
529
|
|
|
357
530
|
### `ServerStyleCollector`
|
|
358
531
|
|
|
359
532
|
Server-safe style collector. One instance per request.
|
|
360
533
|
|
|
361
|
-
Constructor: `new ServerStyleCollector(namePrefix?)
|
|
362
|
-
|
|
363
|
-
| Method
|
|
364
|
-
|
|
365
|
-
| `allocateClassName(cacheKey)`
|
|
366
|
-
| `collectChunk(cacheKey, className, rules)` | Record CSS rules for a chunk. Deduplicated by `cacheKey`.
|
|
367
|
-
| `collectKeyframes(name, css)`
|
|
368
|
-
| `allocateKeyframeName(providedName?)`
|
|
369
|
-
| `collectProperty(name, css)`
|
|
370
|
-
| `collectFontFace(key, css)`
|
|
371
|
-
| `collectCounterStyle(name, css)` | Record a `@counter-style` rule
|
|
372
|
-
| `
|
|
373
|
-
| `
|
|
374
|
-
| `
|
|
375
|
-
| `
|
|
376
|
-
| `
|
|
377
|
-
| `
|
|
378
|
-
| `
|
|
534
|
+
Constructor: `new ServerStyleCollector(namePrefix?)`, or use the `createServerStyleCollector(namePrefix?)` factory. The optional `namePrefix` overrides the value from `configure({ namePrefix })`; in normal usage you pass nothing and let the global config drive it. See [Configuration: Name prefix](configuration.md#name-prefix).
|
|
535
|
+
|
|
536
|
+
| Method | Description |
|
|
537
|
+
| ------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
538
|
+
| `allocateClassName(cacheKey)` | Allocate a deterministic, content-hashed class name for a cache key (e.g. `t1a2b3` with the default prefix). The same `cacheKey` always produces the same class name on server and client when both share the same `namePrefix`. Returns `{ className, isNewAllocation }`. |
|
|
539
|
+
| `collectChunk(cacheKey, className, rules)` | Record CSS rules for a chunk. Deduplicated by `cacheKey`. |
|
|
540
|
+
| `collectKeyframes(name, css)` | Record a `@keyframes` rule. Deduplicated by name. |
|
|
541
|
+
| `allocateKeyframeName(providedName?)` | Allocate a keyframe name. Returns `providedName` if given, otherwise generates one using `${namePrefix}k${counter}` (e.g. `tk0`, `tk1`, ...). |
|
|
542
|
+
| `collectProperty(name, css)` | Record a `@property` rule. Deduplicated by name. |
|
|
543
|
+
| `collectFontFace(key, css)` | Record a `@font-face` rule. Deduplicated by content hash. |
|
|
544
|
+
| `collectCounterStyle(name, css, options?)` | Record a `@counter-style` rule, keyed by name. A different rule under the same name replaces the earlier one (and is re-flushed when streaming); `{ weak: true }`, used for `configure()` definitions, never replaces. |
|
|
545
|
+
| `collectFunction(name, css, options?)` | Record a CSS `@function` rule (from `useFunction` or `@function` styles). Same replace/`weak` semantics as `collectCounterStyle`. |
|
|
546
|
+
| `allocateCounterStyleName(providedName?)` | Allocate a counter-style name. Returns `providedName` if given, otherwise generates one using `${namePrefix}c${counter}` (e.g. `tc0`, `tc1`, ...). |
|
|
547
|
+
| `collectGlobalStyles(key, css)` | Record global styles (from `useGlobalStyles`). Deduplicated by key. |
|
|
548
|
+
| `collectRawCSS(key, css)` | Record raw CSS text (from `useRawCSS`). Deduplicated by key. |
|
|
549
|
+
| `collectInternals()` | Collect eager configured globals: `@property`, `:root` tokens and presets, `@font-face`, `@counter-style`, `@function`, and `globalStyles`. Called automatically on first chunk collection; idempotent. |
|
|
550
|
+
| `getCSS()` | Get all collected CSS as a single string. For non-streaming SSR. |
|
|
551
|
+
| `flushCSS()` | Get only CSS collected since the last flush. For streaming SSR. |
|
|
552
|
+
| `getRenderedClassNames()` | Get the class names allocated since the previous call (the first call returns all so far). Push each batch to `window.__TASTY__` for hydration via `hydrateTastyClasses()`. |
|
|
553
|
+
| `getArtifacts()` | Return the collected CSS as ordered, typed artifacts (`property`, `font-face`, `counter-style`, `function`, `raw`, `global`, `chunk`, `keyframes`). |
|
|
379
554
|
|
|
380
555
|
### `TastyRegistry`
|
|
381
556
|
|
|
382
557
|
Next.js App Router component. Props:
|
|
383
558
|
|
|
384
|
-
| Prop
|
|
385
|
-
|
|
386
|
-
| `children`
|
|
387
|
-
| `transferCache`
|
|
559
|
+
| Prop | Type | Default | Description |
|
|
560
|
+
| ------------------ | ----------------- | --------- | ------------------------------------------------------------------------- |
|
|
561
|
+
| `children` | `ReactNode` | required | Application tree |
|
|
562
|
+
| `transferCache` | `boolean` | `true` | Embed cache state script for zero-cost hydration |
|
|
563
|
+
| `sharedStylesheet` | `string \| false` | generated | Override the shared URL, or disable generated shared CSS for the registry |
|
|
564
|
+
|
|
565
|
+
### `withTastyNext(options)`
|
|
566
|
+
|
|
567
|
+
Next.js configuration wrapper exported from
|
|
568
|
+
`@tenphi/tasty/ssr/next-config`. It generates a shared stylesheet from eager
|
|
569
|
+
Tasty configuration artifacts and wires its URL and immutable cache header
|
|
570
|
+
into the Next config. Route-specific CSS continues through `TastyRegistry`.
|
|
388
571
|
|
|
389
572
|
### `tastyIntegration(options?)`
|
|
390
573
|
|
|
391
574
|
Astro integration factory. Registers middleware and optionally injects client hydration.
|
|
392
575
|
|
|
393
|
-
| Option
|
|
394
|
-
|
|
395
|
-
| `islands` | `boolean`
|
|
576
|
+
| Option | Type | Default | Description |
|
|
577
|
+
| --------- | ---------------------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
578
|
+
| `islands` | `boolean` | `true` | When `true`, injects client hydration script and enables `transferCache`. When `false`, no client JS is shipped. |
|
|
579
|
+
| `css` | `{ mode?: 'inline' \| 'extract' }` | `{ mode: 'inline' }` | `'extract'` writes content-hashed shared and per-page stylesheets for prerendered pages (Astro 5+). See [Build-wide CSS extraction](#build-wide-css-extraction). |
|
|
396
580
|
|
|
397
581
|
### `tastyMiddleware(options?)`
|
|
398
582
|
|
|
399
583
|
Astro middleware factory. Use for manual middleware composition.
|
|
400
584
|
|
|
401
|
-
| Option
|
|
402
|
-
|
|
403
|
-
| `transferCache` | `boolean` | `true`
|
|
585
|
+
| Option | Type | Default | Description |
|
|
586
|
+
| --------------- | --------- | ------- | --------------------------------------------- |
|
|
587
|
+
| `transferCache` | `boolean` | `true` | Embed cache state script for island hydration |
|
|
404
588
|
|
|
405
|
-
### `
|
|
589
|
+
### `hydrateTastyClasses(classes?)`
|
|
406
590
|
|
|
407
|
-
Pre-populate the client injector
|
|
591
|
+
Exported from `@tenphi/tasty/ssr/client` and `@tenphi/tasty/ssr`. Pre-populate the client injector's rules map with class names rendered on the server, marking them as already present in the DOM so `computeStyles()` skips re-injection during hydration. When called without arguments, reads the class list from `window.__TASTY__` (populated by the streaming `<script>` tags emitted during SSR).
|
|
408
592
|
|
|
409
593
|
### `runWithCollector(collector, fn)`
|
|
410
594
|
|
|
411
|
-
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 `
|
|
595
|
+
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()`, `useCounterStyle()`, and `useFunction()` — will find this collector. Returns `fn`'s return value, synchronously unless `fn` returns a promise. Throws where `AsyncLocalStorage` is unavailable, such as in a browser.
|
|
412
596
|
|
|
413
597
|
---
|
|
414
598
|
|
|
@@ -420,25 +604,33 @@ The `TastyRegistry` or `tastyIntegration` is missing. Ensure your layout wraps t
|
|
|
420
604
|
|
|
421
605
|
### Hydration mismatch warnings
|
|
422
606
|
|
|
423
|
-
Class names are
|
|
607
|
+
Class names are content hashes of the resolved styles, so render order does not affect them. Unnamed `useKeyframes()` and `useCounterStyle()` names are counters (`tk0`, `tc0`) on both server and client; pass `name` if the two sides can call them in a different order. If you see mismatches, ensure `hydrateTastyClasses()` 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 `hydrateTastyClasses()` from `@tenphi/tasty/ssr/client` before `hydrateRoot()`.
|
|
608
|
+
|
|
609
|
+
Class names are also derived from the _resolved_ styles, so the server and the client must configure Tasty identically. Anything that changes what a component's styles resolve to will produce a mismatch if it is registered on only one side — `namePrefix`, `recipes`, `handlers`, and the `propHandlers` / `baseStyleProps` extension points described in [Plugins](plugins.md). Call the same `configure()` on both; global CSS is deduplicated automatically, so no `typeof window` guard is needed.
|
|
424
610
|
|
|
425
611
|
### Styles duplicated after hydration
|
|
426
612
|
|
|
427
|
-
**Global CSS** (`:root` tokens, `@property`, `globalStyles`, `@font-face`, `@counter-style`) configured via `configure()` is automatically deduplicated. When Tasty detects
|
|
613
|
+
**Global CSS** (`:root` tokens, `@property`, `globalStyles`, `@font-face`, `@counter-style`, `@function`) configured via `configure()` is automatically deduplicated. When Tasty detects an inline or extracted `[data-tasty-ssr]` stylesheet 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.
|
|
428
614
|
|
|
429
|
-
**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,
|
|
615
|
+
**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, you can remove the SSR style tags and hydration scripts manually after hydration:
|
|
430
616
|
|
|
431
617
|
```tsx
|
|
432
|
-
import {
|
|
618
|
+
import { hydrateTastyClasses } from '@tenphi/tasty/ssr/client';
|
|
433
619
|
|
|
434
|
-
|
|
620
|
+
hydrateTastyClasses();
|
|
435
621
|
hydrateRoot(root, <App />);
|
|
436
622
|
|
|
437
|
-
// Optional: remove SSR style tags after hydration
|
|
438
|
-
document.querySelectorAll('style[data-tasty-ssr]').forEach(el => el.remove());
|
|
439
|
-
document.querySelectorAll('script
|
|
623
|
+
// Optional: remove SSR style tags and class-list scripts after hydration
|
|
624
|
+
document.querySelectorAll('style[data-tasty-ssr]').forEach((el) => el.remove());
|
|
625
|
+
document.querySelectorAll('script').forEach((el) => {
|
|
626
|
+
if (el.textContent?.includes('__TASTY__')) el.remove();
|
|
627
|
+
});
|
|
440
628
|
```
|
|
441
629
|
|
|
442
630
|
### `AsyncLocalStorage` not available
|
|
443
631
|
|
|
444
|
-
|
|
632
|
+
`runWithCollector()` throws `runWithCollector() needs AsyncLocalStorage from node:async_hooks` where the runtime has no `AsyncLocalStorage`, such as in a browser bundle. Call it on the server. The rest of `@tenphi/tasty/ssr` is safe to import in a browser bundle; on the client, call `hydrateTastyClasses()` from `@tenphi/tasty/ssr/client`, or use `@tenphi/tasty/ssr/next`, which hydrates automatically.
|
|
633
|
+
|
|
634
|
+
### "Server rendering without a style collector" warning
|
|
635
|
+
|
|
636
|
+
Tasty rendered on the server with no collector and no React Server Components request scope: Astro without `tastyIntegration()`, a client component server-rendered without `TastyRegistry`, or a custom setup that skips `runWithCollector()`. Each `tasty()` element then inlines all of its CSS, and configured globals (tokens, the default `@property` set, `globalStyles`, `@font-face`) and the CSS of the standalone style functions are not emitted. Add the integration for your framework. The warning is shown once, in development only.
|