npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-react-redux-server-render

v1.0.4

Published

koa react redux server render middleware

Downloads

3

Readme

#koa-react-redux-server-render

react redux server render middleware for koa

  • react-router服务器端处理.
  • SEO的完美支持.
  • 服务端store的数据加载.

安装

Install using npm:

npm install koa-react-redux-server-render --save

使用

react-router

createRoutes.js


  import {Route, Router} from 'react-router'
  import {Home,About} from './containers'
  
  export default(history) => (
  <Router history={history}>
    <Route path='/home' component={Home}/>
    <Route path='/about' component={About}/>
  </Router>
)

在koa中使用 (目前只支持1.0)

app.use(reactReduxServerRender(routes,createStore,render)

| 参数 | 类型 | 说明 | | --- | --- | --- | | routes | Router | reacr-router的路由 | | createStore() | Function | 创建store的function | | render({__body,state,head}) | Function | 渲染react的处理方法, * _body:解析后的React Component的StaticMarkup state: 当前的store数据head: 当前的头处理数据,比如title,meta等(目前使用的是react-helmet)|

使用example


import Koa from 'koa'
import reactReduxServerRender from 'koa-react-redux-server-render'
import configureStore from '../src/store/configureStore'
import createMemoryHistory from 'history/lib/createMemoryHistory'
import createRoutes from '../src/routes'

const app=Koa()

// apply react server render
const history = createMemoryHistory()
app.use(reactReduxServerRender(createRoutes(history), () => {
  return configureStore({}, history)
}, function* (models) {
  yield this.render('index', models)
}))

SEO相关处理

1. React控件


import React, {Component, PropTypes} from 'react'
import Helmet from 'react-helmet'

class Home extends Component {
  render() {
    return (
      <div>
        <Helmet title='首页' meta={[
          {
            'name': 'description',
            'content': 'test application'
          }, {
            'property': 'og:type',
            'content': 'article'
          }
        ]}/>
      </div>
    )
  }
}

Home.propTypes = {
  blogs: PropTypes.object.isRequired,
  fetchBlogs: PropTypes.func.isRequired
}

export default Home

2. render 处理


<!DOCTYPE html>
<html>
  <head>
    {{head.title}}
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    {{head.meta}}
    <link rel="apple-touch-icon" sizes="57x57" href="/images/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/images/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/images/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/images/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/images/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/images/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/images/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/images/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192"  href="/images/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="/images/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
    <meta name="msapplication-TileImage" content="/images/ms-icon-144x144.png">
    {{head.link}}
    <script>
      window.___INITIAL_STATE__ = {{state|json|safe}}
    </script>
  </head>
  <body>
    <div id="root">
      {{__body|safe}}
    </div>
  </body>
</html>

服务端获取数据解决方案

在组建中实现static fetchData() -> Promise 方法


class HomeConatiner extends Component {

  static fetchData({store, params, location}) {
    return store.dispatch(actions.fetchAll())
  }

  render() {
    return (
      <div>
        Test
      </div>
    )
  }
}

具体使用可以参考 nicejs