<commit_before># spa-renderer
Prerender single page application for SEO, with out-of-box supprort for deploying to`AWS Lambda` or `Aliyun Function Compute`.

This project is inspired by [prerender](https://github.com/prerender/prerender) , but  `spa-renderer` is a more lightweight and cheap solution. 

## Develop & Test
#### Download phantomjs and config
Download [phantomjs](http://phantomjs.org/download.html)  for your develop platform,  and modify LOCAL_PHANTOM_PATH in ./src/index.js
#### Run spa-renderer in local shell
``` bash
$ node ./src/index.js --local-sr-mode https://www.foo.bar/test/_hashbang_/frontend/router/path
```
spa-render will replace `_hashbang_` with `#!` 


## Deploy

#### Step 1.  Build this project

 Install dev dependency
``` bash
$ npm install
```
build
``` bash
$ npm run build
```

#### Step 2.  Download phantomjs for server

Download **phantomjs for linux 64bit ** from [Here](http://phantomjs.org/download.html),  and extract it to ./dist 
Your dist folder should look like this

``` plain
dist
 ├── index.js
 ├── run_in_phantom.js
 ├── phantomjs
 ├   └── bin
 ├        └── phantomjs

```

#### Step 3.  Create service and deploy code

##### AWS Lambda
Compress the **content** of dist folder into `dist.zip`
Login to https://aws.amazon.com , find Service - Compute - Lambda , and create a new lambda function

 here are some key points:
 
* Trigger : API Gateway
* Name: `spa-renderer` or anything you like :)
* Runtime: Node.js 6.x 
* Code entry type: upload your  `dist.zip` as code file
* Handler: `index.handler_aws_lambda`
* Role:  create one if needed
* Advanced settings -> Memory:  128MB is enough for most cases
* Advanced settings -> Timeout: 5 - 20 seconds

Api url can be found in Triggers Tab
Usage: 
``` plain
https://XXXXX.execute-api.XXXXX.amazonaws.com/prod/spa-renderer?url=https://www.foo.bar/test/_hashbang_/frontend/router/path
```


##### Aliyun Function Compute (阿里云 函数计算 )
在阿里云上创建一个函数计算的实例
* 运行环境 Node.js 4.x
* 超时： 5-20 秒
* 内存： 128MB 
* 函数入口： `index.handler_aliyun_fc`
* 代码上传：使用 [fcli](https://help.aliyun.com/document_detail/52995.htm)，详见文档
* 需要关联一个 API网关到这个服务，才能通过网关访问

API的访问地址比较隐蔽，在API详情点击编辑， 在第二步的地方可以看到二级域名和请求Path
 
用法
``` plain
http://XXXXX.alicloudapi.com/spa_render?url=https://www.foo.bar/test/_hashbang_/frontend/router/path
```<commit_msg>fix doc<commit_after># spa-renderer

Prerender single page application for SEO, with out-of-box supprort for deploying to`AWS Lambda` or `Aliyun Function Compute`.

This project is inspired by [prerender](https://github.com/prerender/prerender) , but `spa-renderer` is a more lightweight and cheap solution. 

## Develop & Test

### Download phantomjs and config

Download [phantomjs](http://phantomjs.org/download.html)  for your develop platform,  and modify LOCAL_PHANTOM_PATH in ./src/index.js

### Run spa-renderer in local shell

``` bash
$ node ./src/index.js --local-sr-mode https://www.foo.bar/test/_hashbang_/frontend/router/path
```
spa-render will replace `_hashbang_` with `#!` 


## Deploy

### Step 1.  Build this project

Install dev dependency if needed

``` bash
$ npm install
```
build

``` bash
$ npm run build
```

### Step 2.  Download phantomjs for server

Download **phantomjs for linux 64bit** from [Here](http://phantomjs.org/download.html),  and extract it to ./dist 

Your dist folder should look like this

``` plain
dist
 ├── index.js
 ├── run_in_phantom.js
 ├── phantomjs
 ├   └── bin
 ├        └── phantomjs

```

### Step 3.  Create service and deploy code

#### AWS Lambda

Compress the **content** of dist folder into `dist.zip`

Login to https://aws.amazon.com , find Service - Compute - Lambda , and create a new lambda function

here are some key points:
 
* Trigger : API Gateway
* Name: `spa-renderer` or anything you like :)
* Runtime: Node.js 6.x 
* Code entry type: upload your  `dist.zip` as code file
* Handler: `index.handler_aws_lambda`
* Role:  create one if needed
* Advanced settings -> Memory:  128MB is enough for most cases
* Advanced settings -> Timeout: 5 - 20 seconds

API url can be found in Triggers Tab

Usage: 

``` plain
https://XXXXX.execute-api.XXXXX.amazonaws.com/prod/spa-renderer?url=https://www.foo.bar/test/_hashbang_/frontend/router/path
```


#### Aliyun Function Compute (阿里云 函数计算 )
在阿里云上创建一个函数计算的实例

* 运行环境 Node.js 4.x
* 超时： 5-20 秒
* 内存： 128MB 
* 函数入口： `index.handler_aliyun_fc`
* 代码上传：使用 [fcli](https://help.aliyun.com/document_detail/52995.htm)，详见文档
* 需要关联一个 API网关到这个服务，才能通过网关访问

API的访问地址比较隐蔽，在API详情点击编辑， 在第二步的地方可以看到二级域名和请求Path
 
用法

``` plain
http://XXXXX.alicloudapi.com/spa_render?url=https://www.foo.bar/test/_hashbang_/frontend/router/path
```
{'fraction_non_alphanumeric': 0.1317662281647617, 'fraction_numerical': 0.010351520379555747, 'mean_word_length': 4.074398249452954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 14, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1286660', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1756, 'n_words': 475}