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

jsx-isomorphic-fetch

v1.1.0

Published

Isomorphic Fetch and Provider for Server Side Rendering JSX applications.

Downloads

7

Readme

What does JSX Isomorphic Fetch do?

JSX Isomorphic Fetch is a provider that allows your Isomorphic applications to resolve fetch requests before responding to the user. This enables you to send api data or anything else down in your initial request/html. This package does not interfere with vanilla fetch functionality, instead it provides a new function that is supplied in the context passed down from the provider.

Performance

Having your server wait for api requests to complete before it responds to the users request is bad for performance. To avoid the performance degradation all requests that use this should be cached on your server.

Security

Because best practices with "JSX Isomorphic Fetch" require you to cache the request, all secure api requests should done through a regular fetch call and not with "JSX Isomorphic Fetch"

How to use it

There are three main parts to using "JSX Isomorphic Fetch" setting up the provider, getting the fetch call from context, and triggering two renderToString calls.

Provider

The Provider should be the highest level component in your application and it should be used as the wrapper in the render or renderToString call.

React: Browser Entry Rendering

import {Provider, IsomorphicFetch} from 'jsx-isomorphic-fetch'
let isomorphicFetch = new IsomorphicFetch()
isomorphicFetch.store = window._isomorphicFetch_store_

render(
  <Provider isomorphicFetch={isomorphicFetch}>
    <MyApplication />
  </Provider>,
  document.querySelector('#mount')
)

Preact: Browser Entry Rendering

import {Provider, IsomorphicFetch} from 'jsx-isomorphic-fetch'
let isomorphicFetch = new IsomorphicFetch()
isomorphicFetch.store = window._isomorphicFetch_store_

render(
  <Provider id="application" isomorphicFetch={isomorphicFetch}>
    <MyApplication />
  </Provider>,
  document.querySelector('#mount'),
  document.querySelector('#application')
)

Fetching Data

The Provider passes down isomorphicFetch as context, you need to get it out of context before you can use it. IsomorphicFetch has two functions you will want to use here: fetch and clear.

Initial state cant be set in constructor as context is not avalible yet, initial state is what allows the server to render the data.

import PropTypes from 'prop-types'

class MyComponent extends Component {
  constructor(props) {
    super(props)
    this.state = {}
  }

  componentWillMount() {
    const initialData = this.context.isomorphicFetch.initial('MyDataID')

    // Set initial states
    this.state.title = initialData ? initialData.title : null

    this.context.isomorphicFetch.fetch(
      'MyDataID',
      // Array of vanilla js fetch arguments
      ['/my_data.json', {method: 'get'}],

      // This is a middleware function that allows you to process the fetch response.
      function(response) {
        return response.json()
      }
    ).then(function(data) {
      // do stuff with data
    })

    // every call to the same url will be cached, to clear the cache use .clear
    // the cache cannot be cleared on the server with this.
    this.context.isomorphicFetch.clear('MyDataID')
  }

  componentWillUnmount () {
    this.context.isomorphicFetch.clear('MyDataID')
  }
}

MyComponent.childContextTypes = {
  isomorphicFetch: PropTypes.func
}

Rendering on the server

To render on the server it requires you to run render to string twice. The second render waits for all promises to complete.

import render from 'preact-render-to-string' // or react alternative
import {Provider, IsomorphicFetch} from 'jsx-isomorphic-fetch'

let isomorphicFetch = new IsomorphicFetch()
isomorphicFetch.isServer = true

let application = (  
  <Provider id="application" isomorphicFetch={isomorphicFetch}>
    <MyApplication />
  </Provider>
)

// Trigger initial renderToString this will start resolving the data.
render(application)

// .resolve returns a promise
isomorphicFetch.resolve().then(function() {
  // .store will already be processed with  JSON.stringify
  let myAppAsHTMLString = render(application)
  let myAppsData = '<script> window._isomorphicFetch_store_ = ' + isomorphicFetch.store + '</script>'

  // Resolve your server request however your server is setup to do that.
  resolveMyServerRequest(myAppAsHTMLString, myAppsData)
})