Skip to content

Commit 73259ef

Browse files
committed
feat: change how hydration sheet is located
1 parent ed6f4ce commit 73259ef

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

addon/hydrate.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
'use strict';
22

3-
exports.addon = function (renderer, id) {
4-
id = id || 'nano-css';
5-
3+
exports.addon = function (renderer) {
64
if (process.env.NODE_ENV !== 'production') {
75
require('./__dev__/warnOnMissingDependencies')('hydrate', renderer, ['put']);
86
}
97

108
if (renderer.client) {
119
var hydrated = {};
12-
var stylesheet = document.getElementById(id);
10+
var stylesheet = renderer.sh;
1311

1412
if (!stylesheet) {
1513
if (process.env.NODE_ENV !== 'production') {
16-
console.error('Hydration stylesheet with id "' + id + '" was not found.');
14+
console.error('Hydration style sheet was not found.');
1715
}
1816

1917
return;

docs/SSR.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ html += `<style>${nano.raw}</style>`;
1212
## Re-hydrating
1313

1414
`nano-css` can re-hydrate server-side generated CSS. To do that, you need to install [`hydrate` addon](hydrate.md);
15-
and set `nano-css` id on your `<style>` element.
15+
and provide style sheet you want to hydrate to the `nano-css` instance when creating it.
1616

1717
```js
18+
const nano = create({
19+
sh: typeof document === 'object' ? document.getElementById('nano-css') : null
20+
});
21+
1822
html += `<style id="nano-css">${nano.raw}</style>`;
1923
```

docs/hydrate.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ Re-hydrates CSS styles generated on the server. On the server add `nano-css` id
66
html += `<style id="nano-css">${nano.raw}</style>`;
77
```
88

9-
That's it! `hydrate` addon will find this stylesheet in the browser and will not emit CSS
10-
rules that were already generated by the server.
11-
12-
13-
## Configuration
14-
15-
You can set a custom id for you style sheet.
9+
And when creating `nano-css` instance provide that style sheet in configuration.
1610

1711
```js
18-
import {addon as addonHydrate} from 'nano-css/addon/hydrate';
19-
20-
addonHydrate(nano, 'custom-id');
12+
const nano = create({
13+
sh: typeof document === 'object' ? document.getElementById('nano-css') : null
14+
});
2115
```
2216

17+
That's it!
18+
2319

2420
## Installation
2521

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ exports.create = function (config) {
3939
}, config);
4040

4141
if (renderer.client) {
42-
document.head.appendChild(renderer.sh = document.createElement('style'));
42+
if (!renderer.sh)
43+
document.head.appendChild(renderer.sh = document.createElement('style'));
4344

4445
renderer.putRaw = function (rawCssRule) {
4546
if (process.env.NODE_ENV === 'production') {

0 commit comments

Comments
 (0)