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

next-relay

v1.0.9

Published

Relay support for NextJS

Downloads

12

Readme

This package is for using Relay with NextJS. This is an Alpha Library, use with caution.

This package has three modes, these modes can be mixed, with each page using a different mode:

  • Client Side Rendering (CSR) [Default]
  • Server Side Rendering (SSR)
  • Static Site Generation (SSG)

Client Side Rendering

This is the most basic as you don't need to consider how get the data for your build or during server side rendering. Here you can also just use a standard relay enviroment like the one here https://relay.dev/docs/en/quick-start-guide#relay-environment

import createPageContainer from 'next-relay';
import enviroment from '../standard-relay-enviroment';

function Home({ hello, loading }) {
    if(loading) {
        return <div>Loading</div>;
    }
    return <div>{hello}</div>;
}

const { Page } = createPageContainer(Home, graphql`
  query homeQuery {
    hello
  }
`, {
  initEnviroment: () => enviroment,
});
export default Page;

Mixed modes

Once you have setup SSR/SSG for your initEnviroment, it is backwards compible with CSR, it is recommended to have one initEnivoment in a util file. This library is design with the intent that you will use a mixature of techiques depending on the pages need. https://twitter.com/dan_abramov/status/1193004715044491267?lang=en

Server Side Rendering

Here we will no longer be able to use a basic enviroment as we dump and move the cache between the server render and the client. The key thing here is it will give you an instance of RelaySSR, more documentation on that library can be found here: https://github.com/relay-tools/react-relay-network-modern-ssr

import createPageContainer from 'next-relay';

function clientEnviroment(relayClientSSR) {
  return new Environment({
    store: new Store(new RecordSource()),
    network: new RelayNetworkLayer([
      cacheMiddleware({
        size: 100,
        ttl: 60 * 1000,
      }),
      relayClientSSR.getMiddleware({
        lookup: false,
      }),
      urlMiddleware({
        url: () => process.env.GRAPHQL_BASE_URL_CSR,
      }),
    ]),
  });
}

function serverEnviroment(relayServerSSR) {
  return new Environment({
    store: new Store(new RecordSource()),
    network: new RelayNetworkLayer([
      relayServerSSR.getMiddleware(),
      urlMiddleware({
        url: () => process.env.GRAPHQL_BASE_URL_SSR,
      }),
    ]),
  });
}


function Home({ hello, loading }) {
    if(loading) {
        return <div>Loading</div>;
    }
    return <div>{hello}</div>;
}

const { Page } = createPageContainer(Home, graphql`
  query homeQuery {
    hello
  }
`, {
    optimisation: 'SSR',
  initEnviroment: process.browser
  ? clientEnviroment
  : serverEnviroment;
,
});
export default Page;

Static Site Generation

Here we move the rendering to happen during the build phase instead of during runtime, the setup is the exactly the same as SSR except for the fact that you have to export getStaticProps


// Same code as SSR

const { Page, getStaticProps } = createPageContainer(Home, graphql`
  query homeQuery {
    hello
  }
`, {
    optimisation: 'SSG',
  initEnviroment: process.browser
  ? clientEnviroment
  : serverEnviroment;
,
});

export {  Page as default, getStaticProps };