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

react-component-server

v1.0.0

Published

A lib that makes serverside rendering of components easier

Downloads

6

Readme

React Component Server

Build Status

A small lib that wraps express to allow for simple serving of react components.

Usage

Creating Server

To create a new React Component Server call the create method

const ReactComponentServer = require('react-component-server')
const componentServer = ReactComponentServer.create({
  // ... options
})

Component Server Options

{
  // pass an http server instead of creating a new one
  server: server,
  // relative path to components dir
  componentsDir: './components',
  // relative path to templates dir  
  templatesDir: './templates',
  // set of defaults to use
  defaults: {
    // flag to create a browserify bundle of component
    bundle: true
    // default component
    component: './App.js', // ./components/App.js
    // default props to pass to component
    props: {},
    // the template for the component
    template: './template.js' // ./templates/template.js
  }
}

Creating routes

componentServer.get('/', function(res, req, done) {
  const query = req.query || {}
  // pass options to done same as defaults
  done({
    component: './Explore.js',
    props: {query},
    template: './explore-template.js'
  })
})
// or you can just pass an Object
componentServer.get('/about', {component: './About.js'})
// or just use defaults
componentServer.get('/foo')

Listening on Port

// pretty much a proxy for express's listen method
componentServer.listen(3000)
// or you could just listen with the passed express app

Browserify bundles

To allow your component to be ran both client and server side you can create a bundle for it. There is a bit more configuration but is worth it.

Specify route needs bundle

componentServer.get('/bar', {
  bundle: true,
  template: './layout-bundle.js',
  component: './Bar.js'
})

next you will need to inject that bundle and start it in the layout. Here is a example of what a simple layout that includes a bundle would look like.

const React = require('react')
const {safeStringify} = require('react-component-server') // util to strip <scripts>
const template = (ComponentString, {props, meta}) => {
  return (
    <html>
      <body>
        <div id='app' dangerouslySetInnerHTML={{
          __html: ComponentString
        }} />
        <script src={meta.bundlePath} />
        <script dangerouslySetInnerHTML={{
          __html: `
            var React = require('react')
            var ReactDOM = require('react-dom')
            var App = require('${meta.bundleExpose}')
            ReactDOM.render(
              React.createElement(App, ${safeStringify(props)}),
              document.getElementById('app')
            );
          `
        }} />
      </body>
    </html>
  )
}

module.exports = template

What this project needs help with

  • external cacheing of html, and bundles
  • remove dependency on express
  • catching more error states
  • more test
  • feedback!