Skip to content

Commit 9d31f42

Browse files
committed
refactor: Drop hydrate export
1 parent 5c6a6af commit 9d31f42

File tree

7 files changed

+8
-58
lines changed

7 files changed

+8
-58
lines changed

README.md

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const App = () => (
5252
Primarily meant for use with prerendering via [`@preact/preset-vite`](https://github.com/preactjs/preset-vite#prerendering-configuration) or other prerendering systems that share the API. If you're server-side rendering your app via any other method, you can use `preact-render-to-string` (specifically `renderToStringAsync()`) directly.
5353

5454
```js
55+
import { render, hydrate } from 'preact';
5556
import { LocationProvider, ErrorBoundary, Router, lazy, prerender as ssr } from 'preact-iso';
5657

5758
// Asynchronous (throws a promise)
@@ -67,7 +68,10 @@ const App = () => (
6768
</LocationProvider>
6869
);
6970

70-
hydrate(<App />);
71+
if (typeof window !== 'undefined') {
72+
const target = document.getElementById('app');
73+
import.meta.env.DEV ? render(<App />, target) : hydrate(<App />, target);
74+
}
7175

7276
export async function prerender(data) {
7377
return await ssr(<App />);
@@ -270,31 +274,6 @@ const App = () => (
270274
);
271275
```
272276

273-
### `hydrate`
274-
275-
A thin wrapper around Preact's `hydrate` export, it switches between hydrating and rendering the provided element, depending on whether the current page has been prerendered. Additionally, it checks to ensure it's running in a browser context before attempting any rendering, making it a no-op during SSR.
276-
277-
Pairs with the `prerender()` function.
278-
279-
Params:
280-
281-
- `jsx: ComponentChild` - The JSX element or component to render
282-
- `parent?: Element | Document | ShadowRoot | DocumentFragment` - The parent element to render into. Defaults to `document.body` if not provided.
283-
284-
```js
285-
import { hydrate } from 'preact-iso';
286-
287-
const App = () => (
288-
<div class="app">
289-
<h1>Hello World</h1>
290-
</div>
291-
);
292-
293-
hydrate(<App />);
294-
```
295-
296-
However, it is just a simple utility method. By no means is it essential to use, you can always use Preact's `hydrate` export directly.
297-
298277
### `prerender`
299278

300279
Renders a Virtual DOM tree to an HTML string using `preact-render-to-string`. The Promise returned from `prerender()` resolves to an Object with `html` and `links[]` properties. The `html` property contains your pre-rendered static HTML markup, and `links` is an Array of any non-external URL strings found in links on the generated page.

src/hydrate.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/hydrate.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './router.js';
21
export * from './lazy.js';
3-
export * from './hydrate.js';
2+
export * from './router.js';

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './hydrate.js';
21
export * from './lazy.js';
32
export * from './router.js';

src/prerender.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ export async function prerender(vnode, options) {
2525
vnode = cloneElement(vnode, props);
2626
}
2727

28-
let links = new Set();
28+
const links = new Set();
2929
vnodeHook = ({ type, props }) => {
3030
if (type === 'a' && props && props.href && (!props.target || props.target === '_self')) {
3131
links.add(props.href);
3232
}
3333
};
3434

3535
try {
36-
let html = await renderToStringAsync(vnode);
37-
html += `<script id="isodata"></script>`;
36+
const html = await renderToStringAsync(vnode);
3837
return { html, links };
3938
} finally {
4039
vnodeHook = null;

test/node/prerender.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,4 @@ test('extracts links', async () => {
2020
assert.ok(links.has('/baz'), `missing: /baz`);
2121
});
2222

23-
test('appends iso data script', async () => {
24-
const { html: h } = await prerender(html`<div />`);
25-
// Empty for now, but used for hydration vs render detection
26-
assert.match(h, /<script id="isodata"><\/script>/, 'missing iso data script tag');
27-
});
28-
2923
test.run();

0 commit comments

Comments
 (0)