# moon-ssr

> Server Side Rendering for Moon

Latest version **0.1.1** (published 2017-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install moon-ssr
pnpm add moon-ssr
yarn add moon-ssr
bun add moon-ssr
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2017-08-09 |
| First published | 2017-03-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Kabir Shah |
| Maintainers | kingkabir |
| Keywords | ssr, server, side, rendering, moon, moon-ssr |

## Links

- npm: https://www.npmjs.com/package/moon-ssr
- Repository: https://github.com/kbrsh/moon-ssr
- Homepage: https://github.com/kbrsh/moon-ssr#readme
- Issues: https://github.com/kbrsh/moon-ssr/issues
- npm.io page: https://npm.io/package/moon-ssr

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2017-08-09
- 0.1.0 — 2017-07-14
- 0.0.3 — 2017-04-02
- 0.0.2 — 2017-04-02
- 0.0.1 — 2017-03-03

## README

# Moon SSR

Server Side Rendering for Moon

#### What?

Since some clients (such as those with IE9 and below) don't have support for Moon, so you can send prerendered html over, and Moon will automatically rerender on the client if possible.

#### Installation

```sh
npm install moon-ssr
```

#### Usage

```js
const MoonSSR = require('moon-ssr');
MoonSSR.renderToString(instance);
```

#### Full Example

First, install some dependencies with:

```sh
npm install moonjs moon-ssr express
```

Next, let's create a simple Moon app.

Create an `index.html` file

```html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Moon SSR</title>
  </head>
  <body>
    <!-- Where our app is mounted -->
    <div id="app"></div>

    <!-- Require Moon -->
    <script src="/js/moon.js"></script>
    <!-- Require our Moon App -->
    <script src="./js/scripts.js"></script>
    <!-- We mount it only on the client -->
    <script>app.mount("#app");</script>
  </body>
</html>
```

Next, add the Moon application code in `js/scripts.js`

```js
(function(root) {
  'use strict'

  // Function that returns a Moon Instance
  var init = function() {
    return new Moon({
      template: '<div id="app">{{msg}}</div>',
      data: {
        msg: "Hello Moon!"
      }
    });
  }

  // If in node, export it,
  // if in the browser, create a global variable
  // with the instance
  if(typeof module !== 'undefined' && module.exports) {
    module.exports = init;
  } else {
    root.app = init();
  }
})(this);
```

Finally, lets create the server in `server.js`

```js
// Require dependencies for Moon
global.Moon = require('moonjs');
const MoonSSR = require('moon-ssr');

// Some dependencies for I/O
const fs = require('fs');
const path = require('path');

// Our HTML layout file
const layout = fs.readFileSync('./index.html', 'utf8');

// Here we are using express, but you can use whatever
// you'd like
const express = require('express');
const server = express();

// Start a static server serving our javascript assets
server.use('/js', express.static(path.join(__dirname, 'js')));

// Get the '/' route and send over the server rendered content
server.get('/', (req, res) => {
  // Create a new instance of our Moon App
  const app = require('./js/scripts.js')();

  // Render our App to HTML
  const html = MoonSSR.renderToString(app);

  // Send it over to the client
  res.send(layout.replace('<div id="app"></div>', html));
});

// Start the Server on http://localhost:3000
server.listen(3000);
```

#### License

Licensed under the [MIT License](https://kbrsh.github.io/license) by [Kabir Shah](https://kabir.ml)

---
_Source: https://npm.io/package/moon-ssr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
