Skip to content

Commit f4107e1

Browse files
committed
refactor: Drop hydrate export
1 parent 4f44cc0 commit f4107e1

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
@@ -50,6 +50,7 @@ const App = () => (
5050
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.
5151

5252
```js
53+
import { render, hydrate } from 'preact';
5354
import { LocationProvider, ErrorBoundary, Router, lazy, prerender as ssr } from 'preact-iso';
5455

5556
// Asynchronous (throws a promise)
@@ -65,7 +66,10 @@ const App = () => (
6566
</LocationProvider>
6667
);
6768

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

7074
export async function prerender(data) {
7175
return await ssr(<App />);
@@ -322,31 +326,6 @@ const App = () => (
322326
);
323327
```
324328

325-
### `hydrate`
326-
327-
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.
328-
329-
Pairs with the `prerender()` function.
330-
331-
Params:
332-
333-
- `jsx: ComponentChild` - The JSX element or component to render
334-
- `parent?: Element | Document | ShadowRoot | DocumentFragment` - The parent element to render into. Defaults to `document.body` if not provided.
335-
336-
```js
337-
import { hydrate } from 'preact-iso';
338-
339-
const App = () => (
340-
<div class="app">
341-
<h1>Hello World</h1>
342-
</div>
343-
);
344-
345-
hydrate(<App />);
346-
```
347-
348-
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.
349-
350329
### `prerender`
351330

352331
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)