Skip to content

Commit

Permalink
Add SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Oct 18, 2019
1 parent efb5fb0 commit cdd7589
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

This library is a demonstration of the raw performance of S.js when used with the DOM Expressions runtime. This is an experimental approach used mostly for benchmarking and I'd recommend you checkout the official renderer for S.js, [Surplus](https://github.com/adamhaile/surplus) or [Solid](https://github.com/ryansolid/solid) which are better tested and provide a more comprehensive set of features.

It accomplishes this with using [Babel Plugin JSX DOM Expressions](https://github.com/ryansolid/babel-plugin-jsx-dom-expressions). It compiles JSX to DOM statements and by using inner parenthesis syntax ```{( )}``` wraps expressions in functions that can be called by the library of choice. In this case autorun wrap these expressions ensuring the view stays up to date. Unlike Virtual DOM only the changed nodes are affected and the whole tree is not re-rendered over and over.
It accomplishes this with using [Babel Plugin JSX DOM Expressions](https://github.com/ryansolid/babel-plugin-jsx-dom-expressions). It compiles JSX to DOM statements and wraps expressions in functions that can be called by the library of choice. In this case autorun wrap these expressions ensuring the view stays up to date. Unlike Virtual DOM only the changed nodes are affected and the whole tree is not re-rendered over and over.

To use simply wrap your code in a root:
To use simply wrap your code in render:

```js
import S from 's-js';
import { render } from 's-jsx';

S.root(() => document.body.appendChild(<App />))
render(App, document.body);
```

And include 'babel-plugin-jsx-dom-expressions' in your babelrc, webpack babel loader, or rollup babel plugin.
Expand All @@ -24,18 +24,6 @@ And include 'babel-plugin-jsx-dom-expressions' in your babelrc, webpack babel lo
> npm install s-jsx babel-plugin-jsx-dom-expressions
```

## API

Control flow is handled through a special $ JSX element that compiles down to optimized reconciled code that supports conditionals `when`, loops `each`, separate render trees `portal`, and async offscreen rendering `suspend`. Example:

```jsx
const list = S.data(["Alpha", "Beta", "Gamma"]);

<ul>
<$ each={state.list}>{item => <li>{item}</li>}</$>
</ul>
```

Alternatively this library supports Tagged Template Literals or HyperScript for non-precompiled environments by installing the companion library and including variants:
```js
import { html } from 's-jsx/html'; // or
Expand Down
29 changes: 28 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
export * from './runtime'
import S from 's-js';
export * from './runtime'

import { insert, hydration, startSSR } from "./runtime";

type MountableElement = Element | Document | ShadowRoot | DocumentFragment;

export function render(code: () => any, mount: MountableElement): () => void {
let dispose: () => void;
S.root(disposer => {
dispose = disposer;
insert(mount, code());
});
return dispose!;
}

export function renderSSR(code: () => any, element: MountableElement): () => void {
startSSR();
return render(code, element);
}

export function hydrate(code: () => any, element: MountableElement): () => void {
let disposer: () => void;
hydration(() => {
disposer = render(code, element);
}, element);
return disposer!;
}

0 comments on commit cdd7589

Please sign in to comment.