项目作者: ciao-chung

项目描述 :
A server side render service based on puppeteer
高级语言: JavaScript
项目地址: git://github.com/ciao-chung/ciao-ssr.git
创建时间: 2018-04-06T04:54:06Z
项目社区:https://github.com/ciao-chung/ciao-ssr

开源协议:MIT License

下载


Ciao SSR

A server side render service based on puppeteer

This is a puppeteer(chrome headless) server side render service.

Feature

  • Can limit render origin
  • Cache

Required

  • Node.js 8.x up

Dependencies

How server side render work?

Before use this service, you must know how server side render work.

Step Role File path Do
1 Proxy(.htaccess) dist/.htaccess Detect origin is crawler or not by checking user agent.
2 Middleware(ssr.php) dist/ssr.php Send the request with page’s url to this service’s http server.
3 Puppeteer :x: If origin is valid, it will trigger server side render crawler(puppeteer) start.
4 Response :x: The http server of this service will return response with render result.
5 Middleware(ssr.php) dist/ssr.php Render the result to crawler.

Icon Credit

Install Google Chrome

Skip this step if you has install chrome browser

  1. curl -sL https://raw.githubusercontent.com/ciao-chung/ciao-ssr/develop/Meta/install-chrome.sh | bash

Setup/Start server

Installation

  1. yarn global add ciao-ssr

Start server

  1. ciao-ssr --config=/file-to-your/config.json

Clean cache

  1. ciao-ssr --clean

Configuration

config json

Example

  1. {
  2. "allowOrigin": [
  3. "http://localhost:8081", "https://foo.bar"
  4. ],
  5. "cache": {
  6. "ttl": 60,
  7. "maxsize": 1000
  8. },
  9. "debug": true
  10. }
  • port(optional): Number, port of Node.js express app, default is 3000.
  • host(optional): String, host of Node.js express app, default is ‘localhost’.
  • allowOrigin(required): String/Array, allow origin, you can set it as * if you don’t want to limit any origin.
  • timeout(optional): Number, if client don’t trigger server side render service in this timeout, crawler will auto get page result and response, default is 5000ms, at most 15000ms.
  • cache(optional): Object, configure cache feature.
    • ttl(optional): Number, time to life of cache(minutes), default is 1 minute.
    • maxsize(optional): Number, maxsize of cache file on disk(Kilobyte), default is 1MB.
    • path(optional): String, cache file store path, default is ‘cache’.
      • driver(optional): String, default is file, here are available drivers.
        • file
        • redis
    • redisHost(optional): String redis host(default is “localhost”), it only work when redis driver.
    • redisPort(optional): String redis port(default is “6379”), only work when redis driver.
    • redisPass(optional): String redis password(default is null), only work when redis driver.
  • debug(optional): Boolean, debug mode, it will open chrome without headless mode.
  • launchOptions(optional): Object, you can setup any custom puppeteer launch option by this property

Client side(web)

Installation

  1. yarn add ciao-ssr-client

Copy proxy(.htaccess) and middleware(ssr.php) to web root

You can find them in node_modules/ciao-ssr-client

Or copy here

.htaccess

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteCond %{HTTP_USER_AGENT} !(firefox|chrome|safari|msie|edge|opera) [NC]
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteRule ^(.*)$ ssr.php [L]
  7. RewriteRule ^index\.html$ - [L]
  8. RewriteCond %{REQUEST_FILENAME} !-f
  9. RewriteCond %{REQUEST_FILENAME} !-d
  10. RewriteRule . /index.html [L]
  11. </IfModule>

ssr.php

  1. <?php
  2. $ssrHost = 'https://ssr.server';
  3. $host = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"];
  4. $user_agent = urlencode($_SERVER['HTTP_USER_AGENT']);
  5. $port = '';
  6. if($_SERVER["REQUEST_SCHEME"] == 'http' && $_SERVER["SERVER_PORT"] != 80){
  7. $port = ':'.$_SERVER["SERVER_PORT"];
  8. }
  9. if($_SERVER["REQUEST_SCHEME"] == 'https' && $_SERVER["SERVER_PORT"] != 443){
  10. $port = ':'.$_SERVER["SERVER_PORT"];
  11. }
  12. $requestUrl = $host.$port.$_SERVER['REQUEST_URI'];
  13. $result = json_decode(file_get_contents($ssrHost.'/render?url='.$requestUrl), true);
  14. if(!$result || !$result['statusCode']) {
  15. header("HTTP/1.0 404 Not Found");
  16. die;
  17. }
  18. http_response_code($result['statusCode']);
  19. echo $result['content'];

Use client library in web

We provide a client side library to trigger server side render service

  1. import ServerSideRenderClient from 'ciao-ssr-client'
  2. ServerSideRenderClient()
  3. // when your all async data are ready and render
  4. SSR.done()
  5. // when your page is in error type
  6. SSR.error()
  7. // when you want to custom error status code in error page
  8. SSR.error(403)

Apache configuration

Enable apache rewrite/proxy/proxy_http modules

  1. sudo a2enmod rewrite
  2. sudo a2enmod proxy
  3. sudo a2enmod proxy_http
  4. sudo service apache2 restart

Setup domain

  1. <VirtualHost *:80>
  2. ServerName example.com
  3. ServerAlias www.example.com
  4. ProxyRequests Off
  5. ProxyPreserveHost On
  6. ProxyVia Full
  7. <Proxy *>
  8. Require all granted
  9. </Proxy>
  10. <Location ></Location>
  11. ProxyPass http://localhost:3000/
  12. ProxyPassReverse http://127.0.0.1:3000
  13. </Location>
  14. </VirtualHost>

Enable domain and restart apache

  1. sudo a2ensite example.com.conf
  2. sudo service apache2 restart

Manage service with PM2

PM2 is an advanced Node.js process manager.

You can manage server side render service easily by using PM2.

Installation

  1. sudo yarn global add pm2

Management

  1. # start service
  2. pm2 start ciao-ssr --name="ssr" -- --config=/file-to-your/config.json
  3. # stop service
  4. pm2 stop ssr
  5. # delete service
  6. pm2 delete ssr
  7. # show status
  8. pm2 status ssr
  9. # show log
  10. pm2 log ssr

Start With Docker

This service will be started by pm2 when your container start

Just setup port and config json file of your local host

  1. # build image
  2. docker-compose build --no-cahce
  3. # run
  4. docker-compose up -d
  5. # exec
  6. docker exec ssr /bin/bash
  7. # start service with pm2 in container
  8. pm2 start ciao-ssr --name='ssr' -- --config=/ssr-config/config.json --watch && pm2 save

Volume

docker folder will mount as docker volume.

you should setup docker/config.json to configure your SSR service