https://github.com/jaydenseric/next-server-context
A Next.js App or page decorator, React context object, and React hook to access Node.js HTTP server context when rendering components.
https://github.com/jaydenseric/next-server-context
esm graphql maintained mjs next node npm react
Last synced: about 2 months ago
JSON representation
A Next.js App or page decorator, React context object, and React hook to access Node.js HTTP server context when rendering components.
- Host: GitHub
- URL: https://github.com/jaydenseric/next-server-context
- Owner: jaydenseric
- License: mit
- Created: 2020-02-26T10:40:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-22T06:04:37.000Z (almost 2 years ago)
- Last Synced: 2025-06-01T00:20:02.640Z (4 months ago)
- Topics: esm, graphql, maintained, mjs, next, node, npm, react
- Language: JavaScript
- Homepage: https://npm.im/next-server-context
- Size: 104 KB
- Stars: 31
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- Funding: .github/funding.yml
- License: license.md
Awesome Lists containing this project
README
# next-server-context
A [Next.js](https://nextjs.org) [`App` or page decorator](./withServerContext.mjs), [React context object](./ServerContextContext.mjs), and [React hook](./useServerContext.mjs) to access [Node.js](https://nodejs.org) HTTP server context when rendering components.
Some uses:
- Setting the HTTP response status code for the server side rendered page according to [GraphQL](https://graphql.org) query results in components.
- Isomorphically accessing cookies within components.## Installation
To install [`next-server-context`](https://npm.im/next-server-context) with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), run:
```sh
npm install next-server-context
```Decorate either the entire [Next.js](https://nextjs.org) app or individual pages using the function [`withServerContext`](./withServerContext.mjs) to be able to use the [React](https://reactjs.org) hook [`useServerContext`](./useServerContext.mjs).
## Requirements
Supported runtime environments:
- [Node.js](https://nodejs.org) versions `^18.17.0 || >=20.4.0`.
- Browsers matching the [Browserslist](https://browsersl.ist) query [`> 0.5%, not OperaMini all, not dead`](https://browsersl.ist/?q=%3E+0.5%25%2C+not+OperaMini+all%2C+not+dead).Projects must configure [TypeScript](https://www.typescriptlang.org) to use types from the ECMAScript modules that have a `// @ts-check` comment:
- [`compilerOptions.allowJs`](https://www.typescriptlang.org/tsconfig#allowJs) should be `true`.
- [`compilerOptions.maxNodeModuleJsDepth`](https://www.typescriptlang.org/tsconfig#maxNodeModuleJsDepth) should be reasonably large, e.g. `10`.
- [`compilerOptions.module`](https://www.typescriptlang.org/tsconfig#module) should be `"node16"` or `"nodenext"`.## Exports
The [npm](https://npmjs.com) package [`next-server-context`](https://npm.im/next-server-context) features [optimal JavaScript module design](https://jaydenseric.com/blog/optimal-javascript-module-design). It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the [`package.json`](./package.json) field [`exports`](https://nodejs.org/api/packages.html#exports):
- [`ServerContextContext.mjs`](./ServerContextContext.mjs)
- [`useServerContext.mjs`](./useServerContext.mjs)
- [`withServerContext.mjs`](./withServerContext.mjs)