0.2.0 • Published 3 years ago

next-proper v0.2.0

Weekly downloads
171
License
MIT
Repository
github
Last release
3 years ago

⛓ next-proper

npm NPM npm

Easily compile NextJS props via composed methods for getServerSideProps and getStaticProps.

Install

Via npm

npm install --save next-proper

Via Yarn

yarn add next-proper

How to use

The goal of this package is to make it easy to compose NextJS props logic for either getServerSideProps or getStaticProps, so that you don’t have to copy that same code through all your pages.

Depending on your needs, or your apps logic, as you compose your prop methods, you can internally either exit early with { notFound: true } or { redirect: ... }, or continue the chain by calling, next({ props: {...} });

Here’s a basic example using some simplified auth logic:

props/getAuthProps.js

export const getAuthProps = async (props, next, ctx) => {
  ...Do auth stuff...
  const user = getUser(...)

  if (!user) {
    return {
      redirect: {
        destination: '/login',
        permanent: false,
      }
    }
  }

  return next({
    ...props,
    props: {
      ...props.props,
      user,
    }
  })
}

pages/secure-page.js

import nextProps from 'next-proper'
import { getAuthProps } from 'props/getAuthProps'
import { getFetchPageProps } from 'props/getServerSideFetchPageProps'

export const getServerSideProps = (ctx) => nextProps([
  getAuthProps,
  getFetchPageProps,
])(ctx)

License

MIT © Ryan Hefner

0.2.0

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.2

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago