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-ssr-prefetch

v1.0.8

Published

js library to prefetch server side before ssr

Downloads

11

Readme

react-ssr-prefetch

This is a simple library to help prefetch data and then render the app alongside the data. The client side must be implemented. As for the server side, if it is not implemented, then it will just be a normal client side data fetching.

Installing

Using yarn

yarn add react-ssr-prefetch

Using npm

npm install react-ssr-prefetch

Hooks

usePrefetch

usage: usePrefetch(prefetchFunctions, { lazy, params, defaultValue, initialValue })

| Params | Type | Description | | ----------------- | ------------------- | ----------------------------------------------------------------------------------------------------- | | prefetchFunctions | Object of functions | this is a map of functions that you would like to execute. | | lazy | Boolean | if true, it will not fetch on server-side, defaults to false | | params | Object of arrays | this is a map of array which will be passed to the functions defined in the prefetchFunctions | | initialValue | Object | this is a map that defines the initial value of the data of the keys defined in the prefetchFunctions | | defaultValue | any | this is the absolute fallback initial value for every keys in the prefetchFunctions |

the response of this function is an object of object with keys that are defined in prefetchFunctions. Each key is structured in the following way: { [key]: { data, loading, error } }

| Key | Type | Description | | ------- | ------- | --------------------------------------------------------- | | data | any | the result of the prefetch function of the respective key | | loading | Boolean | loading state of the respective key | | error | Object | error object if prefetch function ends with error |

Example

Client side
import React from 'react'
import { usePrefetch, PrefetchProvider } from 'react-ssr-prefetch/client'

const prefetchFunctions = {
  news: (newsID) =>
    Promise.resolve({
      story: 'this is a story with id: ' + newsID,
    }), // do api call here
  user: () =>
    Promise.resolve({
      name: 'my-name',
    }),
}

const App = ({ newsID }) => {
  const {
    news: { data, loading, error, refetch },
    user,
  } = usePrefetch(prefetchFunctions, { params: { news: [newsID] } })

  if (loading) return 'Loading'
  if (error) return 'error'

  return <div>{JSON.stringify(data)}</div>
}

const ClientApp = () => {
  // this will depend on where you pass the prefetch context data during the server side rendering
  const { data } = window.__data
  return (
    <PrefetchProvider data={data}>
        <App />
    </PrefetchProvider>
  )
};
Server side
import React from 'react'
import { renderWithData } from 'react-ssr-prefetch/server'
import App from 'your_app_location'

const app = <App />
const prefetchContext = {}
const htmlBody = await renderWithData(app, prefetchContext)
const textHtml = `<!doctype html>
<html>
  <head></head><body>
  <div id="main">${htmlBody}</div>
  <script type="text/javascript">
    window.__data=${JSON.stringify(prefetchContext.data)}
  </script>
</body>
</html>`
res.send(textHtml)

Server Side Utils

renderWithData

Render component to string server side alongside prefetched data

Usage: renderWithData(ReactComponent, context, renderToStringFunction)

| Params | Type | Description | | ---------------------- | --------------- | ---------------------------------------------------------------------------------------- | | ReactComponent | React Component | your React App | | context | Object | all requests values will be stored here | | renderToStringFunction | Function | function to render to string, by default uses require('react-dom/server').renderToString |

import React from 'react'
import { renderWithData } from 'react-ssr-prefetch/server'

const prefetchContext = {}
const htmlBody = await renderWithData(App, prefetchContext)
const textHtml = `<!doctype html>
<html>
  <head></head><body>
  <div id="main">${htmlBody}</div>
  <script type="text/javascript">
    window.__data=${JSON.stringify(data)}
  </script>
</body>
</html>`
res.send(textHtml)

Client Side Utils

Prefetch Provider

Usage: <PrefetchProvider data={window.__data}><App/></PrefetchProvider>

| Params | Type | Description | | ------ | ------ | ------------------------------------------------------------------------------------------------------------ | | data | Object | this is the data that will be used client side, and will be used by any value that uses the prefetch context |