Skip to content

Commit 337a431

Browse files
committed
preact iso
1 parent a621153 commit 337a431

File tree

5 files changed

+94
-25
lines changed

5 files changed

+94
-25
lines changed

demo/package-lock.json

Lines changed: 56 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"@preact/preset-vite": "^2.5.0",
2020
"express": "^4.18.2",
21-
"preact": "^10.12.1"
21+
"preact": "^10.13.1",
22+
"preact-iso": "^2.3.0"
2223
}
2324
}

demo/src/App.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h } from 'preact';
2-
import { Suspense, lazy } from 'preact/compat';
2+
import { lazy, Router, Route, ErrorBoundary } from 'preact-iso';
33

44
export function App({ head }) {
55
const Counter = lazy(
@@ -16,9 +16,11 @@ export function App({ head }) {
1616
<body>
1717
<main>
1818
<h1>Our Counter application</h1>
19-
<Suspense fallback={<p>Loading...</p>}>
20-
<Counter />
21-
</Suspense>
19+
<ErrorBoundary>
20+
<Router fallback={<p>Loading...</p>}>
21+
<Route component={Counter} path="/" />
22+
</Router>
23+
</ErrorBoundary>
2224
</main>
2325
{import.meta.env.DEV && (
2426
<script type="module" src="/src/entry-client.jsx" />

demo/src/entry-client.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { hydrate } from 'preact';
2+
import { LocationProvider } from 'preact-iso';
23
import { App } from './App';
34

45
const config = { attributes: true, childList: true, subtree: true };
@@ -16,4 +17,9 @@ const mut = new MutationObserver((mutationList, observer) => {
1617
});
1718
mut.observe(document, config);
1819

19-
hydrate(<App />, document);
20+
hydrate(
21+
<LocationProvider>
22+
<App />
23+
</LocationProvider>,
24+
document
25+
);

demo/src/entry-server.jsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1+
import { LocationProvider } from 'preact-iso';
12
import { renderToPipeableStream } from '../../src/stream-node';
23
import { App } from './App';
34

5+
// @ts-ignore
6+
global.location = {
7+
origin: 'http://localhost:3000'
8+
};
9+
410
export function render({ res, head }) {
511
res.socket.on('error', (error) => {
612
console.error('Fatal', error);
713
});
8-
const { pipe, abort } = renderToPipeableStream(<App head={head} />, {
9-
onShellReady() {
10-
res.statusCode = 200;
11-
res.setHeader('Content-type', 'text/html');
12-
pipe(res);
13-
},
14-
onErrorShell(error) {
15-
res.statusCode = 500;
16-
res.send(
17-
`<!doctype html><p>An error ocurred:</p><pre>${error.message}</pre>`
18-
);
14+
const { pipe, abort } = renderToPipeableStream(
15+
<LocationProvider url="/">
16+
<App head={head} />
17+
</LocationProvider>,
18+
{
19+
onShellReady() {
20+
res.statusCode = 200;
21+
res.setHeader('Content-type', 'text/html');
22+
pipe(res);
23+
},
24+
onErrorShell(error) {
25+
res.statusCode = 500;
26+
res.send(
27+
`<!doctype html><p>An error ocurred:</p><pre>${error.message}</pre>`
28+
);
29+
}
1930
}
20-
});
31+
);
2132

2233
// Abandon and switch to client rendering if enough time passes.
2334
// Try lowering this to see the client recover.

0 commit comments

Comments
 (0)