@tenphi/tasty 0.0.0-snapshot.ce10eff → 0.0.0-snapshot.cfcf770
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 +290 -207
- package/dist/_virtual/_rolldown/runtime.js +1 -2
- package/dist/chunks/cacheKey.d.ts +1 -0
- package/dist/chunks/cacheKey.js +17 -10
- package/dist/chunks/cacheKey.js.map +1 -1
- package/dist/chunks/definitions.d.ts +1 -1
- package/dist/chunks/definitions.js +3 -5
- package/dist/chunks/definitions.js.map +1 -1
- package/dist/chunks/index.d.ts +1 -0
- package/dist/chunks/renderChunk.d.ts +1 -0
- package/dist/chunks/renderChunk.js +32 -34
- package/dist/chunks/renderChunk.js.map +1 -1
- package/dist/compute-styles.d.ts +31 -0
- package/dist/compute-styles.js +357 -0
- package/dist/compute-styles.js.map +1 -0
- package/dist/config.d.ts +109 -30
- package/dist/config.js +144 -45
- package/dist/config.js.map +1 -1
- package/dist/core/index.d.ts +12 -11
- package/dist/core/index.js +11 -10
- package/dist/counter-style/index.js +51 -0
- package/dist/counter-style/index.js.map +1 -0
- package/dist/debug.d.ts +26 -141
- package/dist/debug.js +357 -637
- package/dist/debug.js.map +1 -1
- package/dist/font-face/index.js +63 -0
- package/dist/font-face/index.js.map +1 -0
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/useCounterStyle.d.ts +50 -0
- package/dist/hooks/useCounterStyle.js +45 -0
- package/dist/hooks/useCounterStyle.js.map +1 -0
- package/dist/hooks/useFontFace.d.ts +43 -0
- package/dist/hooks/useFontFace.js +69 -0
- package/dist/hooks/useFontFace.js.map +1 -0
- package/dist/hooks/useGlobalStyles.d.ts +3 -0
- package/dist/hooks/useGlobalStyles.js +23 -2
- package/dist/hooks/useGlobalStyles.js.map +1 -1
- package/dist/hooks/useKeyframes.js +13 -4
- package/dist/hooks/useKeyframes.js.map +1 -1
- package/dist/hooks/useProperty.js +31 -14
- package/dist/hooks/useProperty.js.map +1 -1
- package/dist/hooks/useRawCSS.js +8 -2
- package/dist/hooks/useRawCSS.js.map +1 -1
- package/dist/hooks/useStyles.d.ts +4 -4
- package/dist/hooks/useStyles.js +7 -147
- package/dist/hooks/useStyles.js.map +1 -1
- package/dist/index.d.ts +15 -12
- package/dist/index.js +14 -11
- package/dist/injector/index.d.ts +36 -11
- package/dist/injector/index.js +42 -11
- package/dist/injector/index.js.map +1 -1
- package/dist/injector/injector.d.ts +60 -3
- package/dist/injector/injector.js +168 -29
- package/dist/injector/injector.js.map +1 -1
- package/dist/injector/sheet-manager.d.ts +10 -13
- package/dist/injector/sheet-manager.js +39 -70
- package/dist/injector/sheet-manager.js.map +1 -1
- package/dist/injector/types.d.ts +101 -19
- package/dist/keyframes/index.js +1 -1
- package/dist/parser/classify.js +6 -6
- package/dist/parser/classify.js.map +1 -1
- package/dist/parser/const.js +30 -3
- package/dist/parser/const.js.map +1 -1
- package/dist/parser/lru.js +1 -1
- package/dist/parser/lru.js.map +1 -1
- package/dist/parser/parser.js +2 -3
- package/dist/parser/parser.js.map +1 -1
- package/dist/parser/tokenizer.js +1 -1
- package/dist/parser/tokenizer.js.map +1 -1
- package/dist/parser/types.js +1 -1
- package/dist/parser/types.js.map +1 -1
- package/dist/pipeline/conditions.js +1 -1
- package/dist/pipeline/conditions.js.map +1 -1
- package/dist/pipeline/exclusive.js +1 -2
- package/dist/pipeline/exclusive.js.map +1 -1
- package/dist/pipeline/index.d.ts +3 -1
- package/dist/pipeline/index.js +80 -32
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/materialize.js +381 -122
- package/dist/pipeline/materialize.js.map +1 -1
- package/dist/pipeline/parseStateKey.d.ts +1 -1
- package/dist/pipeline/parseStateKey.js +21 -13
- package/dist/pipeline/parseStateKey.js.map +1 -1
- package/dist/pipeline/simplify.js +1 -2
- package/dist/pipeline/simplify.js.map +1 -1
- package/dist/pipeline/warnings.js +1 -1
- package/dist/plugins/index.d.ts +2 -0
- package/dist/plugins/okhsl-plugin.js +3 -277
- package/dist/plugins/okhsl-plugin.js.map +1 -1
- package/dist/plugins/types.d.ts +9 -2
- package/dist/properties/index.js +24 -60
- package/dist/properties/index.js.map +1 -1
- package/dist/properties/property-type-resolver.d.ts +0 -5
- package/dist/properties/property-type-resolver.js +19 -32
- package/dist/properties/property-type-resolver.js.map +1 -1
- package/dist/ssr/astro.d.ts +29 -0
- package/dist/ssr/astro.js +64 -0
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/async-storage.d.ts +17 -0
- package/dist/ssr/async-storage.js +34 -0
- package/dist/ssr/async-storage.js.map +1 -0
- package/dist/ssr/collect-auto-properties.js +39 -0
- package/dist/ssr/collect-auto-properties.js.map +1 -0
- package/dist/ssr/collector.d.ts +102 -0
- package/dist/ssr/collector.js +219 -0
- package/dist/ssr/collector.js.map +1 -0
- package/dist/ssr/format-global-rules.js +22 -0
- package/dist/ssr/format-global-rules.js.map +1 -0
- package/dist/ssr/format-keyframes.js +69 -0
- package/dist/ssr/format-keyframes.js.map +1 -0
- package/dist/ssr/format-property.js +49 -0
- package/dist/ssr/format-property.js.map +1 -0
- package/dist/ssr/format-rules.js +73 -0
- package/dist/ssr/format-rules.js.map +1 -0
- package/dist/ssr/hydrate.d.ts +22 -0
- package/dist/ssr/hydrate.js +49 -0
- package/dist/ssr/hydrate.js.map +1 -0
- package/dist/ssr/index.d.ts +4 -0
- package/dist/ssr/index.js +10 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/next.d.ts +43 -0
- package/dist/ssr/next.js +74 -0
- package/dist/ssr/next.js.map +1 -0
- package/dist/ssr/ssr-collector-ref.js +12 -0
- package/dist/ssr/ssr-collector-ref.js.map +1 -0
- package/dist/states/index.js +13 -259
- package/dist/states/index.js.map +1 -1
- package/dist/static/index.js +1 -2
- 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/static/tastyStatic.js +1 -2
- package/dist/static/tastyStatic.js.map +1 -1
- package/dist/static/types.js +1 -1
- package/dist/styles/border.d.ts +1 -1
- package/dist/styles/border.js +30 -24
- package/dist/styles/border.js.map +1 -1
- package/dist/styles/color.d.ts +1 -1
- package/dist/styles/color.js +11 -8
- package/dist/styles/color.js.map +1 -1
- package/dist/styles/const.js +17 -0
- package/dist/styles/const.js.map +1 -0
- package/dist/styles/createStyle.js +28 -26
- package/dist/styles/createStyle.js.map +1 -1
- package/dist/styles/dimension.js +15 -3
- package/dist/styles/dimension.js.map +1 -1
- package/dist/styles/directional.js +133 -0
- package/dist/styles/directional.js.map +1 -0
- package/dist/styles/display.d.ts +3 -10
- package/dist/styles/display.js +45 -39
- package/dist/styles/display.js.map +1 -1
- package/dist/styles/fade.d.ts +1 -1
- package/dist/styles/fade.js +9 -5
- package/dist/styles/fade.js.map +1 -1
- package/dist/styles/fill.d.ts +2 -2
- package/dist/styles/fill.js +3 -4
- package/dist/styles/fill.js.map +1 -1
- package/dist/styles/flow.js +1 -1
- package/dist/styles/gap.d.ts +1 -1
- package/dist/styles/gap.js +4 -3
- package/dist/styles/gap.js.map +1 -1
- package/dist/styles/height.d.ts +1 -1
- package/dist/styles/height.js +1 -2
- package/dist/styles/height.js.map +1 -1
- package/dist/styles/index.d.ts +0 -1
- package/dist/styles/index.js +3 -4
- package/dist/styles/index.js.map +1 -1
- package/dist/styles/inset.d.ts +1 -29
- package/dist/styles/inset.js +19 -135
- package/dist/styles/inset.js.map +1 -1
- package/dist/styles/list.d.ts +5 -5
- package/dist/styles/list.js +4 -2
- package/dist/styles/list.js.map +1 -1
- package/dist/styles/margin.d.ts +1 -1
- package/dist/styles/margin.js +17 -89
- package/dist/styles/margin.js.map +1 -1
- package/dist/styles/outline.d.ts +1 -1
- package/dist/styles/outline.js +6 -16
- package/dist/styles/outline.js.map +1 -1
- package/dist/styles/padding.d.ts +1 -1
- package/dist/styles/padding.js +17 -89
- package/dist/styles/padding.js.map +1 -1
- package/dist/styles/placement.d.ts +37 -0
- package/dist/styles/placement.js +74 -0
- package/dist/styles/placement.js.map +1 -0
- package/dist/styles/predefined.d.ts +4 -6
- package/dist/styles/predefined.js +9 -13
- package/dist/styles/predefined.js.map +1 -1
- package/dist/styles/preset.d.ts +6 -1
- package/dist/styles/preset.js +54 -53
- package/dist/styles/preset.js.map +1 -1
- package/dist/styles/radius.d.ts +1 -3
- package/dist/styles/radius.js +38 -6
- package/dist/styles/radius.js.map +1 -1
- package/dist/styles/scrollMargin.d.ts +24 -0
- package/dist/styles/scrollMargin.js +32 -0
- package/dist/styles/scrollMargin.js.map +1 -0
- package/dist/styles/scrollbar.d.ts +9 -5
- package/dist/styles/scrollbar.js +31 -92
- package/dist/styles/scrollbar.js.map +1 -1
- package/dist/styles/shadow.d.ts +1 -1
- package/dist/styles/shadow.js +4 -3
- package/dist/styles/shadow.js.map +1 -1
- package/dist/styles/shared.js +17 -0
- package/dist/styles/shared.js.map +1 -0
- package/dist/styles/transition.d.ts +1 -1
- package/dist/styles/transition.js +6 -5
- package/dist/styles/transition.js.map +1 -1
- package/dist/styles/types.d.ts +94 -28
- package/dist/styles/width.d.ts +1 -1
- package/dist/styles/width.js +1 -2
- package/dist/styles/width.js.map +1 -1
- package/dist/tasty.d.ts +46 -894
- package/dist/tasty.js +96 -59
- package/dist/tasty.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils/cache-wrapper.js +4 -9
- package/dist/utils/cache-wrapper.js.map +1 -1
- package/dist/utils/case-converter.js +1 -1
- package/dist/utils/color-math.d.ts +46 -0
- package/dist/utils/color-math.js +749 -0
- package/dist/utils/color-math.js.map +1 -0
- package/dist/utils/color-space.d.ts +5 -0
- package/dist/utils/color-space.js +228 -0
- package/dist/utils/color-space.js.map +1 -0
- package/dist/utils/colors.js +3 -2
- package/dist/utils/colors.js.map +1 -1
- package/dist/utils/dotize.js +1 -1
- package/dist/utils/dotize.js.map +1 -1
- package/dist/utils/filter-base-props.d.ts +1 -1
- package/dist/utils/filter-base-props.js +1 -1
- package/dist/utils/filter-base-props.js.map +1 -1
- package/dist/utils/get-display-name.js +1 -1
- package/dist/utils/has-keys.js +13 -0
- package/dist/utils/has-keys.js.map +1 -0
- package/dist/utils/is-dev-env.js +1 -1
- package/dist/utils/is-dev-env.js.map +1 -1
- package/dist/utils/is-valid-element-type.js +1 -1
- package/dist/utils/merge-styles.js +1 -2
- package/dist/utils/merge-styles.js.map +1 -1
- package/dist/utils/mod-attrs.d.ts +0 -2
- package/dist/utils/mod-attrs.js +3 -4
- package/dist/utils/mod-attrs.js.map +1 -1
- package/dist/utils/process-tokens.d.ts +3 -17
- package/dist/utils/process-tokens.js +19 -107
- package/dist/utils/process-tokens.js.map +1 -1
- package/dist/utils/resolve-recipes.js +1 -2
- package/dist/utils/resolve-recipes.js.map +1 -1
- package/dist/utils/selector-transform.js +32 -0
- package/dist/utils/selector-transform.js.map +1 -0
- package/dist/utils/string.js +1 -1
- package/dist/utils/styles.d.ts +3 -82
- package/dist/utils/styles.js +29 -560
- package/dist/utils/styles.js.map +1 -1
- package/dist/utils/typography.d.ts +24 -13
- package/dist/utils/typography.js +15 -17
- package/dist/utils/typography.js.map +1 -1
- package/dist/utils/warnings.js +1 -1
- package/dist/zero/babel.d.ts +79 -10
- package/dist/zero/babel.js +190 -40
- package/dist/zero/babel.js.map +1 -1
- package/dist/zero/css-writer.js +1 -2
- package/dist/zero/css-writer.js.map +1 -1
- package/dist/zero/extractor.js +54 -3
- package/dist/zero/extractor.js.map +1 -1
- package/dist/zero/index.js +1 -2
- package/dist/zero/next.d.ts +56 -30
- package/dist/zero/next.js +105 -40
- package/dist/zero/next.js.map +1 -1
- package/docs/PIPELINE.md +519 -0
- package/docs/README.md +31 -0
- package/docs/adoption.md +296 -0
- package/docs/comparison.md +420 -0
- package/docs/configuration.md +326 -0
- package/docs/debug.md +318 -0
- package/docs/design-system.md +424 -0
- package/docs/dsl.md +688 -0
- package/docs/getting-started.md +217 -0
- package/docs/injector.md +542 -0
- package/docs/methodology.md +616 -0
- package/docs/runtime.md +572 -0
- package/docs/ssr.md +354 -0
- package/docs/styles.md +596 -0
- package/docs/tasty-static.md +520 -0
- package/package.json +61 -35
- package/dist/styles/align.d.ts +0 -15
- package/dist/styles/align.js +0 -14
- package/dist/styles/align.js.map +0 -1
- package/dist/styles/justify.d.ts +0 -15
- package/dist/styles/justify.js +0 -14
- package/dist/styles/justify.js.map +0 -1
- package/dist/styles/styledScrollbar.d.ts +0 -47
- package/dist/styles/styledScrollbar.js +0 -38
- package/dist/styles/styledScrollbar.js.map +0 -1
- package/dist/tokens/typography.d.ts +0 -19
- package/dist/tokens/typography.js +0 -237
- package/dist/tokens/typography.js.map +0 -1
- package/dist/utils/hsl-to-rgb.js +0 -38
- package/dist/utils/hsl-to-rgb.js.map +0 -1
- package/dist/utils/okhsl-to-rgb.js +0 -296
- package/dist/utils/okhsl-to-rgb.js.map +0 -1
package/docs/ssr.md
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
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()` detects a `ServerStyleCollector` (via `AsyncLocalStorage` or an explicit option) 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 global getter. All style computation — whether from `tasty()` components, `computeStyles()`, `useStyles()`, or other hooks like `useGlobalStyles()` — discovers the collector through this single global getter. No React context is involved.
|
|
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 `<script>` tag transfers the `cacheKey → className` mapping to the client.
|
|
89
|
+
- When the module loads on the client, `hydrateTastyCache()` runs automatically 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
|
+
`tasty()` components are hook-free and do not require `'use client'`. They can be used directly in React Server Components. Dynamic `styleProps` like `<Grid flow="column">` work normally in server components. During SSR, `computeStyles()` discovers the collector via the same global getter registered by `TastyRegistry` — no React context or client boundary needed for this path.
|
|
94
|
+
|
|
95
|
+
### Options
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
// Skip cache state transfer (saves payload size at the cost of hydration perf)
|
|
99
|
+
<TastyRegistry transferCache={false}>{children}</TastyRegistry>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### CSP nonce
|
|
103
|
+
|
|
104
|
+
If your app uses Content Security Policy with nonces, configure it before rendering:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
// app/layout.tsx or a server-side init file
|
|
108
|
+
import { configure } from '@tenphi/tasty';
|
|
109
|
+
|
|
110
|
+
configure({ nonce: 'your-nonce-value' });
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The nonce is automatically applied to all `<style>` and `<script>` tags injected by `TastyRegistry`.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Astro
|
|
118
|
+
|
|
119
|
+
### 1. Add the middleware
|
|
120
|
+
|
|
121
|
+
Create or update your Astro middleware:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
// src/middleware.ts
|
|
125
|
+
import { tastyMiddleware } from '@tenphi/tasty/ssr/astro';
|
|
126
|
+
|
|
127
|
+
export const onRequest = tastyMiddleware();
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 2. Use tasty() components as normal
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
// src/components/Card.tsx
|
|
134
|
+
import { tasty } from '@tenphi/tasty';
|
|
135
|
+
|
|
136
|
+
const Card = tasty({
|
|
137
|
+
styles: {
|
|
138
|
+
padding: '4x',
|
|
139
|
+
fill: '#surface',
|
|
140
|
+
radius: '1r',
|
|
141
|
+
border: true,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
export default Card;
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```astro
|
|
149
|
+
---
|
|
150
|
+
// src/pages/index.astro
|
|
151
|
+
import Card from '../components/Card.tsx';
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
<html>
|
|
155
|
+
<body>
|
|
156
|
+
<Card>Static card — styles collected by middleware</Card>
|
|
157
|
+
<Card client:load>Island card — styles hydrated on client</Card>
|
|
158
|
+
</body>
|
|
159
|
+
</html>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### How it works
|
|
163
|
+
|
|
164
|
+
Astro's `@astrojs/react` renderer calls `renderToString()` for each React component without wrapping the tree in a provider. The middleware uses `AsyncLocalStorage` to make the collector available to all `computeStyles()` calls within the request.
|
|
165
|
+
|
|
166
|
+
- **Static components** (no `client:*`): Styles are collected during `renderToString` and injected into `</head>`. No JavaScript is shipped for these components.
|
|
167
|
+
- **Islands** (`client:load`, `client:visible`, etc.): Styles are collected during SSR the same way. On the client, importing `@tenphi/tasty/ssr/astro` auto-hydrates the cache from `<script data-tasty-cache>`. The island's `computeStyles()` calls hit the cache during hydration.
|
|
168
|
+
|
|
169
|
+
### Client-side hydration for islands
|
|
170
|
+
|
|
171
|
+
The `@tenphi/tasty/ssr/astro` module auto-hydrates when imported on the client. To ensure the cache is warm before any island renders, import it in a shared entry point or in each island component:
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
// src/components/MyIsland.tsx
|
|
175
|
+
import '@tenphi/tasty/ssr/astro'; // auto-hydrates cache on import
|
|
176
|
+
import { tasty } from '@tenphi/tasty';
|
|
177
|
+
|
|
178
|
+
const MyIsland = tasty({
|
|
179
|
+
styles: { padding: '2x', fill: '#blue' },
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
export default MyIsland;
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Options
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
// Skip cache state transfer
|
|
189
|
+
export const onRequest = tastyMiddleware({ transferCache: false });
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### CSP nonce
|
|
193
|
+
|
|
194
|
+
Same as Next.js -- call `configure({ nonce: '...' })` before any rendering happens. The middleware reads the nonce and applies it to injected tags.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## Generic Framework Integration
|
|
199
|
+
|
|
200
|
+
Any React-based framework can integrate using `runWithCollector`, which binds a `ServerStyleCollector` to the current async context via `AsyncLocalStorage`. All `computeStyles()` and hook calls within the render automatically discover the collector.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
import {
|
|
204
|
+
ServerStyleCollector,
|
|
205
|
+
runWithCollector,
|
|
206
|
+
hydrateTastyCache,
|
|
207
|
+
} from '@tenphi/tasty/ssr';
|
|
208
|
+
import { renderToString } from 'react-dom/server';
|
|
209
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
210
|
+
|
|
211
|
+
// ── Server ──────────────────────────────────────────────
|
|
212
|
+
|
|
213
|
+
const collector = new ServerStyleCollector();
|
|
214
|
+
|
|
215
|
+
const html = await runWithCollector(collector, () =>
|
|
216
|
+
renderToString(<App />)
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const css = collector.getCSS();
|
|
220
|
+
const cacheState = collector.getCacheState();
|
|
221
|
+
|
|
222
|
+
// Embed in your HTML template:
|
|
223
|
+
const fullHtml = `
|
|
224
|
+
<html>
|
|
225
|
+
<head>
|
|
226
|
+
<style data-tasty-ssr>${css}</style>
|
|
227
|
+
<script data-tasty-cache type="application/json">
|
|
228
|
+
${JSON.stringify(cacheState)}
|
|
229
|
+
</script>
|
|
230
|
+
</head>
|
|
231
|
+
<body>
|
|
232
|
+
<div id="root">${html}</div>
|
|
233
|
+
</body>
|
|
234
|
+
</html>
|
|
235
|
+
`;
|
|
236
|
+
|
|
237
|
+
// ── Client ──────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
// Before hydration:
|
|
240
|
+
hydrateTastyCache(); // reads from <script data-tasty-cache>
|
|
241
|
+
|
|
242
|
+
hydrateRoot(document.getElementById('root'), <App />);
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Streaming SSR
|
|
246
|
+
|
|
247
|
+
For streaming with `renderToPipeableStream`, use `flushCSS()` instead of `getCSS()`:
|
|
248
|
+
|
|
249
|
+
```tsx
|
|
250
|
+
const collector = new ServerStyleCollector();
|
|
251
|
+
|
|
252
|
+
const stream = await runWithCollector(collector, () =>
|
|
253
|
+
renderToPipeableStream(<App />, {
|
|
254
|
+
onShellReady() {
|
|
255
|
+
// Flush styles collected so far
|
|
256
|
+
const css = collector.flushCSS();
|
|
257
|
+
res.write(`<style data-tasty-ssr>${css}</style>`);
|
|
258
|
+
stream.pipe(res);
|
|
259
|
+
},
|
|
260
|
+
onAllReady() {
|
|
261
|
+
// Flush any remaining styles + cache state
|
|
262
|
+
const css = collector.flushCSS();
|
|
263
|
+
if (css) res.write(`<style data-tasty-ssr>${css}</style>`);
|
|
264
|
+
|
|
265
|
+
const state = collector.getCacheState();
|
|
266
|
+
res.write(`<script data-tasty-cache type="application/json">${JSON.stringify(state)}</script>`);
|
|
267
|
+
},
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
## API Reference
|
|
275
|
+
|
|
276
|
+
### Entry points
|
|
277
|
+
|
|
278
|
+
| Import path | Description |
|
|
279
|
+
|---|---|
|
|
280
|
+
| `@tenphi/tasty/ssr` | Core SSR API: `ServerStyleCollector`, `runWithCollector`, `hydrateTastyCache` |
|
|
281
|
+
| `@tenphi/tasty/ssr/next` | Next.js App Router: `TastyRegistry` component |
|
|
282
|
+
| `@tenphi/tasty/ssr/astro` | Astro: `tastyMiddleware`, auto-hydration on import |
|
|
283
|
+
|
|
284
|
+
### `ServerStyleCollector`
|
|
285
|
+
|
|
286
|
+
Server-safe style collector. One instance per request.
|
|
287
|
+
|
|
288
|
+
| Method | Description |
|
|
289
|
+
|---|---|
|
|
290
|
+
| `allocateClassName(cacheKey)` | Allocate a sequential class name (`t0`, `t1`, ...) for a cache key. Returns `{ className, isNewAllocation }`. |
|
|
291
|
+
| `collectChunk(cacheKey, className, rules)` | Record CSS rules for a chunk. Deduplicated by `cacheKey`. |
|
|
292
|
+
| `collectKeyframes(name, css)` | Record a `@keyframes` rule. Deduplicated by name. |
|
|
293
|
+
| `collectProperty(name, css)` | Record a `@property` rule. Deduplicated by name. |
|
|
294
|
+
| `collectFontFace(key, css)` | Record a `@font-face` rule. Deduplicated by content hash. |
|
|
295
|
+
| `collectCounterStyle(name, css)` | Record a `@counter-style` rule. Deduplicated by name. |
|
|
296
|
+
| `getCSS()` | Get all collected CSS as a single string. For non-streaming SSR. |
|
|
297
|
+
| `flushCSS()` | Get only CSS collected since the last flush. For streaming SSR. |
|
|
298
|
+
| `getCacheState()` | Serialize `{ entries: Record<cacheKey, className>, classCounter }` for client hydration. |
|
|
299
|
+
|
|
300
|
+
### `TastyRegistry`
|
|
301
|
+
|
|
302
|
+
Next.js App Router component. Props:
|
|
303
|
+
|
|
304
|
+
| Prop | Type | Default | Description |
|
|
305
|
+
|---|---|---|---|
|
|
306
|
+
| `children` | `ReactNode` | required | Application tree |
|
|
307
|
+
| `transferCache` | `boolean` | `true` | Embed cache state script for zero-cost hydration |
|
|
308
|
+
|
|
309
|
+
### `tastyMiddleware(options?)`
|
|
310
|
+
|
|
311
|
+
Astro middleware factory. Options:
|
|
312
|
+
|
|
313
|
+
| Option | Type | Default | Description |
|
|
314
|
+
|---|---|---|---|
|
|
315
|
+
| `transferCache` | `boolean` | `true` | Embed cache state script for island hydration |
|
|
316
|
+
|
|
317
|
+
### `hydrateTastyCache(state?)`
|
|
318
|
+
|
|
319
|
+
Pre-populate the client injector cache. When called without arguments, reads from `window.__TASTY_SSR_CACHE__` (streaming) or `<script data-tasty-cache>` (non-streaming).
|
|
320
|
+
|
|
321
|
+
### `runWithCollector(collector, fn)`
|
|
322
|
+
|
|
323
|
+
Run a function with a `ServerStyleCollector` bound to the current async context via `AsyncLocalStorage`. All `computeStyles()` and `useStyles()` calls within `fn` (and async continuations) will find this collector.
|
|
324
|
+
|
|
325
|
+
---
|
|
326
|
+
|
|
327
|
+
## Troubleshooting
|
|
328
|
+
|
|
329
|
+
### Styles flash on page load (FOUC)
|
|
330
|
+
|
|
331
|
+
The `TastyRegistry` or `tastyMiddleware` is missing. Ensure your layout wraps the app with `TastyRegistry` (Next.js) or the middleware is registered (Astro).
|
|
332
|
+
|
|
333
|
+
### Hydration mismatch warnings
|
|
334
|
+
|
|
335
|
+
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, import `@tenphi/tasty/ssr/astro` in your island components. For custom setups, call `hydrateTastyCache()` before `hydrateRoot()`.
|
|
336
|
+
|
|
337
|
+
### Styles duplicated after hydration
|
|
338
|
+
|
|
339
|
+
This is expected and harmless. 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:
|
|
340
|
+
|
|
341
|
+
```tsx
|
|
342
|
+
import { hydrateTastyCache } from '@tenphi/tasty/ssr';
|
|
343
|
+
|
|
344
|
+
hydrateTastyCache();
|
|
345
|
+
hydrateRoot(root, <App />);
|
|
346
|
+
|
|
347
|
+
// Optional: remove SSR style tags after hydration
|
|
348
|
+
document.querySelectorAll('style[data-tasty-ssr]').forEach(el => el.remove());
|
|
349
|
+
document.querySelectorAll('script[data-tasty-cache]').forEach(el => el.remove());
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### `AsyncLocalStorage` not available
|
|
353
|
+
|
|
354
|
+
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).
|