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

preswr

v1.0.0

Published

Preload your SWR hooks on the server

Downloads

12

Readme

Introduction

PreSWR is a wrapper over swr that keeps track of your calls and lets you preload its results. It's oriented as a tool to help with Server-Side Rendering, and quite suited for use with Next.js, although its design is environment-agnostic.

$ yarn add preswr
import usePreSWR, { preloaded } from "preswr"
import React from "react"

const FavouritePokemon = preloaded(({ name }) => {
  const { data } = usePreSWR(`https://pokeapi.co/api/v2/pokemon/${name}/`)
  return (
    <p>
      My favourite Pokémon is ${data.name}, of type ${data.types[0].type.name}
    </p>
  )
})

export default FavouritePokemon.Component

export const getStaticProps = async ({ params }) =>
  await FavouritePokemon.preloadData({ name: params.name })

Usage

usePreSWR

The default export is the hook function usePreSWR, which wraps useSWR with the preloading smarts. It has the same arguments.

It is allowed to mix usePreSWR calls and useSWR calls freely, but only the usePreSWR will be preloaded.

PreSWRConfig

usePreSWR can't read the config from the <SWRConfig> provider. We export a <PreSWRConfig> that works for both.

preloaded

It's a function which takes a React component, and an optional options object, and returns an object with two properties.

preloaded(
  MyComponent,
  /* this object, and all its properties, are optional */
  {
    /**
     * PreSWR analyzes the tree multiple times to make sure that all
     * conditional rendering is taken care of. You can configure how many times
     * this can happen, or set `false` to disable deep analyzing.
     */
    maxDepth: 20,
  }
)
  • Component is your original component, but it takes an extra prop with the preloaded data. You need to use this component in place of the original one to take advantage of the preloading.

  • preloadData() takes the props you'd call your component with, preloads the data which it would request, and returns the props with an extra one which holds the preloaded data.

Limitations

  • It only works with ReactDOM, so no support for React Native (yet?)
  • Make sure you follow the Rules of Hooks
  • When preloadData is called, your React component is rendered in the background (maybe multiple times!), which might invoke side-effects if your code uses them.
  • Your fetcher functions must work in the context you're using preloadData(). You can provide an initialData config key in the call to bypass calling them.
  • Your keys (or the value returned from key functions) must be able to be serialized to JSON (that means numbers, strings, null, arrays, and plain objects).

TypeScript

This package is written in TypeScript, and it exports a number of helpful types.

An especially useful type is PreloaderProps<...>, which returns the type of the props of the wrapped component, including the extra internal prop which holds the preloaded data.

import { GetStaticProps } from "next"
import { preloaded, PreloaderProps } from "preswr"
import FooComponent from "./foo"

const PreloadedFoo = preloaded(FooComponent)

export default PreloadedFoo.Component

export const getStaticProps: GetStaticProps<PreloaderProps<
  typeof PreloadedFoo
>> = async ({ params }) =>
  await FavouritePokemon.preloadData({ name: params.name })