UNPKG

4.62 kBMarkdownView Raw
1<br />
2<br />
3
4<div align="center">
5 <img src="https://raw.githubusercontent.com/natemoo-re/microsite/master/.github/assets/microsite.svg?sanitize=true" alt="microsite" width="375" height="101" />
6</div>
7
8<br />
9<br />
10
11`microsite` is a fast, opinionated static-site generator (SSG) that outputs extremely minimal clientside code using **automatic partial hydration**. Pages are written with Preact, Typescript, and CSS Modules and compiled with `esbuild`.
12
13```bash
14npm init microsite <project>
15```
16
17[See the demo](https://microsite-demo.nmoo.vercel.app/)
18
19> Microsite is output as ESM, so it needs to run in a Node environment which supports it (node@12.19.0).
20>
21> Ensure that your project includes `"type": "module"` in `package.json`, which will allow you to use ESM in your project's `node` scripts.
22
23## Automatic Partial Hydration (APH)
24
25The most exciting feature of Microsite is automatic partial hydration. Current solutions send the entire component tree, which has already been rendered server-side, to the client for hydration.
26Microsite, on the other hand, uses a hint from the author (the `withHydrate` HOC) to strip away any unnecessary code and ship highly optimized code to the client.
27
28```tsx
29import { withHydrate } from "microsite/hydrate";
30
31const Counter = () => {
32 const [count, setCount] = useState(0);
33
34 return (
35 <>
36 <button onClick={() => setCount((v) => v - 1)}>-</button>
37 <span>{count}</span>
38 <button onClick={() => setCount((v) => v + 1)}>+</button>
39 </>
40 );
41};
42
43export default withHydrate(Counter, { method: "idle" });
44```
45
46There are a few rules to keep in mind when leveraging APH:
47
48- Hydrated components cannot contain any other hydrated component, as hydration is controlled by the top-level component.
49
50- Hydrated components should be placed as deep as possible in your app's tree for the most efficient bundles.
51
52- Hydrated components can't accept _rich_ children, because it's non-trivial to serialize them, though I have some ideas to address this. For now, strings and numbers as children are fine.
53
54#### `withHydrate` Options
55
56**method**
57
58As a developer, you know exactly how your site is structured, so Microsite allows you to tweak how hydration occurs, optimizing for your specific use cases.
59
60- `idle` (default) hydrates the component as soon as possible, when the browser executes [`requestIdleCallback`](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) code.
61
62- `visible` hydrates the component as soon as it enters the viewport, via [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver).
63
64- `interaction` hydrates the component as soon as the user interacts with it (via `focus` or `pointerenter` events.)
65
66## Pages
67
68Microsite uses the file-system to generate your output, meaning each component in `src/pages` outputs a corresponding HTML file.
69
70Page templates are written as `.tsx` files with [Preact](https://preactjs.com/).
71
72## Styles
73
74Styles are written using CSS Modules. `src/global.css` is, as you guessed, a global CSS file injected on every page.
75Per-page/per-component styles are also inject on the correct pages. They are modules and must be named `*.module.css`.
76
77## Project structure
78
79Microsite cares about the structure of your project. It should look like this:
80
81```
82project/
83├── src/
84│ ├── global.css
85│ ├── global.ts // shipped entirely to client, if present
86│ ├── pages/ // fs-based routing like Next.js
87│ │ └── index.tsx
88│ └── public/ // copied to dist/
89└── tsconfig.json
90```
91
92## Acknowledgments
93
94- [Markus Oberlehner](https://twitter.com/maoberlehner), [`vue-lazy-hydration`](https://github.com/maoberlehner/vue-lazy-hydration)
95- [Markus Oberlehner](https://twitter.com/maoberlehner), [Building Partially Hydrated, Progressively Enhanced Static Websites with Isomorphic Preact and Eleventy](https://markus.oberlehner.net/blog/building-partially-hydrated-progressively-enhanced-static-websites-with-isomorphic-preact-and-eleventy/)
96- [Lukas Bombach](https://twitter.com/luke_schmuke), [The case of partial hydration (with Next and Preact)](https://medium.com/@luke_schmuke/how-we-achieved-the-best-web-performance-with-partial-hydration-20fab9c808d5)
97- [Jason Miller](https://twitter.com/_developit) and [Addy Osmani](https://twitter.com/addyosmani), [Rendering on the Web](https://developers.google.com/web/updates/2019/02/rendering-on-the-web)
98- [Poimandres](https://github.com/pmndrs), [`valtio`](https://github.com/pmndrs/valtio) for inspiring `microsite/global`