11 | ,
12 | document.querySelector('#client-root')
13 | )
14 |
15 | // next, we'll render the static and client component
16 | const el = document.querySelector('.static-and-client')
17 |
18 | // the hydrateInitialState function allows us to render the same initial state
19 | // for the component as we had placed in the html, to reduce repetition.
20 | // this gives us back jsx representing the markup we used to initialize the
21 | // component in the html
22 | const vdom = hydrateInitialState(el.dataset.state, {
23 | 'static-and-client': StaticAndClient
24 | })
25 |
26 | // finally, we take the hydrated element and render it as usual!
27 | // note how we don't need to rewrite the jsx like we did with the client render
28 | render(vdom, el.parentElement, el)
29 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "spike-static-components-example",
3 | "description": "example of how to use static and client rendered preact components with spike",
4 | "version": "0.0.1",
5 | "author": "static-dev",
6 | "ava": {
7 | "verbose": "true"
8 | },
9 | "bugs": "https://github.com/static-dev/spike-static-components-example/issues",
10 | "dependencies": {
11 | "babel-preset-preact": "^1.1.0",
12 | "babel-register": "^6.24.1",
13 | "reshape-preact-components": "^0.5.1",
14 | "reshape-standard": "^2.1.1",
15 | "spike": "^2.0.2",
16 | "spike-css-standards": "^2.0.1",
17 | "spike-js-standards": "^2.0.2"
18 | },
19 | "devDependencies": {
20 | "snazzy": "^7.0.0",
21 | "standard": "^10.0.0"
22 | },
23 | "homepage": "https://github.com/static-dev/spike-static-components-example",
24 | "main": "app.js",
25 | "private": true,
26 | "repository": "https://github.com/static-dev/spike-static-components-example",
27 | "scripts": {
28 | "lint": "standard | snazzy",
29 | "precommit": "npm run lint -s",
30 | "pretest": "npm run lint -s",
31 | "test": "NODE_ENV=test ava"
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # spike-static-components-example
2 |
3 | An example of how to use static and client rendered preact components with spike
4 |
5 | ## Setup
6 |
7 | - make sure [node.js](http://nodejs.org) is at version >= `6`
8 | - `npm i spike -g`
9 | - clone this repo down and `cd` into the folder
10 | - run `npm install`
11 | - run `spike watch` or `spike compile`
12 |
13 | ## Architecture
14 |
15 | This is an entirely unique architecture pattern, to the best of our knowledge nothing else similar to this exists right now. So take a moment to let it sink in. As a simple guide:
16 |
17 | - Preact components are in `assets/js/components`
18 | - Client render is in `assets/js/index.js`
19 | - Static implementation is in `views/index.sgr`
20 | - "Server render" configuration is in `app.js`
21 |
22 | Happy to field any questions about this on twitter at [@jescalan](https://twitter.com/jescalan), or better yet in [the static-dev slack](https://static-dev-slack.herokuapp.com/) :grin:
23 |
--------------------------------------------------------------------------------
/views/index.sgr:
--------------------------------------------------------------------------------
1 | doctype html
2 | html
3 | head
4 | title Spike Static Components Example
5 | link(rel='stylesheet' href='/css/index.css')
6 | body
7 | p React devtools work on this page, feel free to open them up and look around!
8 |
9 | static-only(foo='bar')
10 | p child element
11 | static-and-client(foo='bar')
12 | p child element
13 | #client-root
14 |
15 | script(src='/js/main.js' defer)
16 |
--------------------------------------------------------------------------------