GithubHelp home page GithubHelp logo

next-props-composer's Introduction

next-props-composer

npm version

Compose multiple Next.js getStaticProps or getServerSideProps functions.

Install

npm install next-props-composer

next-props-composer has a peer dependency of next@^10.0.0.

Usage

next-props-composer allows you to compose multiple data-fetching methods for pre-rendering pages.

Export a getStaticProps or getServerSideProps function from your page that is mapped to composeProps.

import { composeProps } from "next-props-composer";

export const getStaticProps = composeProps(foo, bar, baz);

You supply any number of async functions that return props. These are the same function that you would normally return, and the page context is passed to each one.

// lib/api
export const getFoo: GetStaticProps = async ({ params }) => {
  const res = await fetch(`/api/foo/${params.id}`);
  const foo = await res.json();

  return {
    props: {
      foo,
    },
  };
};

export const getBar: GetStaticProps = async () => {
  const res = await fetch("/api/bar");
  const bar = await res.json();

  return {
    props: {
      bar,
    },
  };
};

And export an async getStaticProps or getServerSideProps function on your pages like you usually would. Each props object is merged into one (your props won't be transferable from one function to another. it works independently).

// pages/foo/[id].js
import { composeProps } from "next-props-composer";
import { getFoo, getBar } from "lib/api";

function Page({ foo, bar }) {
  return <main>{/* Use props like usual */}</main>;
}

export const getStaticProps = composeProps(getFoo, getBar);

export default Page;

While these examples show getStaticProps, the process is exactly the same for getServerSideProps.

redirect and notFound

If one of your composed functions returns a redirect or notFound, it will take precedence and immediately return.

export const getFoo: GetStaticProps = async ({ params }) => {
  const res = await fetch(`/api/foo/${params.id}`);
  const foo = await res.json();

  if (!foo) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
  }

  return {
    props: {
      foo,
    },
  };
};

revalidate

If one or more of your functions returns a revalidate property to opt into Incremental Static Regeneration, the last resolved value will be passed. This only applies to getStaticProps.

License

MIT

next-props-composer's People

Contributors

bitmap avatar sprabowo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.