Nomenclature Processing Machine


    1.1.0 • Public • Published

    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.


    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.


    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.


    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() = window._isomorphicFetch_store_
      <Provider isomorphicFetch={isomorphicFetch}>
        <MyApplication />

    Preact: Browser Entry Rendering

    import {Provider, IsomorphicFetch} from 'jsx-isomorphic-fetch'
    let isomorphicFetch = new IsomorphicFetch() = window._isomorphicFetch_store_
      <Provider id="application" isomorphicFetch={isomorphicFetch}>
        <MyApplication />

    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) {
        this.state = {}
      componentWillMount() {
        const initialData = this.context.isomorphicFetch.initial('MyDataID')
        // Set initial states
        this.state.title = initialData ? initialData.title : null
          // 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.
      componentWillUnmount () {
    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 />
    // Trigger initial renderToString this will start resolving the data.
    // .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_ = ' + + '</script>'
      // Resolve your server request however your server is setup to do that.
      resolveMyServerRequest(myAppAsHTMLString, myAppsData)


    npm i jsx-isomorphic-fetch

    DownloadsWeekly Downloads






    Last publish


    • horrorandtropics