@monteway/next
v0.4.2
Published
Utilities for Monterail projects based on Next.js
Downloads
1
Maintainers
Readme
@monteway/next
Useful utilities for Next.js apps.
Also a production-ready Dockerfile for Next.js projects.
Install
npm i @moneway/next
Usage
errorPropsResponse
Return an object that is compatible with the result of getServerSideProps
/ getStaticProps
and includes an internal property marking the response as having some error.
import { errorPropsResponse } from '@monteway/next/error';
import type { GetServerSidePropsContext } from 'next';
export async function getServerSideProps(context: GetServerSidePropsContext) {
try {
// Pretend we have a call here to some API that can throw
// an error when user has no access to the page.
} catch {
// This responds with HTTP 403 status
// and returns
// {
// props: {
// error: 'You cannot access this page',
// __isErrorProps__: true
// },
// }
return errorPropsResponse(context.res, {
statusCode: 403,
error: 'You cannot access this page',
});
}
}
isErrorProps
Checks if the argument is from errorPropsResponse
.
import { errorPropsResponse, isErrorProps } from '@monteway/next/error';
const response = errorPropsResponse(null, {
statusCode: 403,
error: 'You cannot access this page',
});
isErrorProps(response.props);
// true
Docker
This package includes also a official Dockerfile for Next.js project which is production ready.
We have extended it with some optional build arguments that can be used to extend the image.
| --build-arg
| Description |
| ------------------------ | -------------------------- |
| BEFORE_INSTALL_COMMAND
| Run before npm install
|
| AFTER_INSTALL_COMMAND
| Run after npm install
|
| BEFORE_BUILD_COMMAND
| Run before npm run build
|
| AFTER_BUILD_COMMAND
| Run after npm run build
|
Example usage of the Dockerfile (make sure the Docker context is root of the Next.js app):
docker build \
--build-arg BEFORE_INSTALL_COMMAND="exit 0" \
--build-arg AFTER_INSTALL_COMMAND="exit 0" \
--build-arg BEFORE_BUILD_COMMAND="exit 0" \
--build-arg AFTER_BUILD_COMMAND="exit 0" \
--tag ${IMAGE_TAG} \
--file node_modules/@monteway/next/dockerfile .