UNPKG

4.58 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[See the demo](https://microsite-demo.nmoo.vercel.app/)
14
15> Microsite is output as ESM, so it needs to run in a Node environment which supports it (node@12.19.0).
16>
17> Ensure that your project includes `"type": "module"` in `package.json`, which will allow you to use ESM in your project's `node` scripts.
18
19## Automatic Partial Hydration (APH)
20
21The 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.
22Microsite, 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.
23
24```tsx
25import { withHydrate } from "microsite/hydrate";
26
27const Counter = () => {
28 const [count, setCount] = useState(0);
29
30 return (
31 <>
32 <button onClick={() => setCount((v) => v - 1)}>-</button>
33 <span>{count}</span>
34 <button onClick={() => setCount((v) => v + 1)}>+</button>
35 </>
36 );
37};
38
39export default withHydrate(Counter, { method: "idle" });
40```
41
42There are a few rules to keep in mind when leveraging APH:
43
44- Hydrated components cannot contain any other hydrated component, as hydration is controlled by the top-level component.
45
46- Hydrated components should be placed as deep as possible in your app's tree for the most efficient bundles.
47
48- 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.
49
50#### `withHydrate` Options
51
52**method**
53
54As 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.
55
56- `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.
57
58- `visible` hydrates the component as soon as it enters the viewport, via [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver).
59
60- `interaction` hydrates the component as soon as the user interacts with it (via `focus` or `pointerenter` events.)
61
62## Pages
63
64Microsite uses the file-system to generate your output, meaning each component in `src/pages` outputs a corresponding HTML file.
65
66Page templates are written as `.tsx` files with [Preact](https://preactjs.com/).
67
68## Styles
69
70Styles are written using CSS Modules. `src/global.css` is, as you guessed, a global CSS file injected on every page.
71Per-page/per-component styles are also inject on the correct pages. They are modules and must be named `*.module.css`.
72
73## Project structure
74
75Microsite cares about the structure of your project. It should look like this:
76
77```
78project/
79├── src/
80│ ├── global.css
81│ ├── global.ts // shipped entirely to client, if present
82│ ├── pages/ // fs-based routing like Next.js
83│ │ └── index.tsx
84│ └── public/ // copied to dist/
85└── tsconfig.json
86```
87
88## Acknowledgments
89
90- [Markus Oberlehner](https://twitter.com/maoberlehner), [`vue-lazy-hydration`](https://github.com/maoberlehner/vue-lazy-hydration)
91- [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/)
92- [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)
93- [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)
94- [Poimandres](https://github.com/pmndrs), [`valtio`](https://github.com/pmndrs/valtio) for inspiring `microsite/global`