Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/wmr/demo/public/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h, render } from 'preact';
import { LocationProvider, Router } from './lib/loc.js';
import lazy, { ErrorBoundary } from './lib/lazy.js';
import hydrate from './lib/hydrate';
import Home from './pages/home.js';
// import About from './pages/about/index.js';
import NotFound from './pages/_404.js';
Expand Down Expand Up @@ -44,7 +44,7 @@ export function App() {
}

if (typeof window !== 'undefined') {
render(<App />, document.body);
hydrate(<App />, document.body);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typeof window !== 'undefined' browser check can be removed.

References:

if (typeof window === 'undefined') return;

https://github.com/preactjs/wmr/blob/main/packages/preact-iso/README.md#hydratejs

This method also checks to make sure its running in a browser context before attempting any rendering - if not, it does nothing.

}

export async function prerender(data) {
Expand Down
17 changes: 17 additions & 0 deletions packages/wmr/demo/public/lib/hydrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { render, hydrate as hydrativeRender } from 'preact';

let initialized;

/** @type {typeof render} */
export default function hydrate(jsx, parent) {
if (typeof window === 'undefined') return;
let isodata = document.querySelector('script[type=isodata]');
// @ts-ignore-next
parent = parent || (isodata && isodata.parentNode) || document.body;
if (!initialized && isodata) {
hydrativeRender(jsx, parent);
} else {
render(jsx, parent);
}
initialized = true;
}