https://github.com/yiminghe/next-context
unified middleware and request context for nextjs
https://github.com/yiminghe/next-context
expressjs koa middleware nextjs
Last synced: 18 days ago
JSON representation
unified middleware and request context for nextjs
- Host: GitHub
- URL: https://github.com/yiminghe/next-context
- Owner: yiminghe
- License: mit
- Created: 2023-11-01T10:16:07.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-30T17:00:35.000Z (about 1 month ago)
- Last Synced: 2025-03-30T17:34:23.626Z (about 1 month ago)
- Topics: expressjs, koa, middleware, nextjs
- Language: TypeScript
- Homepage:
- Size: 1.5 MB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# next-context
- Using koa style middlewares inside nextjs
- Unified request/response context(express api) across Page and Route/Action
- SetCookie/clearCookie both inside Page and Route/Action
- Easily access request/response context between next middleware, components inside Page and functions inside Route/Action
- I18n support both for server and client component[![NPM version][npm-image]][npm-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![npm download][download-image]][download-url]

[](https://cloud.cypress.io/projects/5v7p13/runs)[npm-image]: http://img.shields.io/npm/v/next-context.svg?style=flat-square
[npm-url]: http://npmjs.org/package/next-context
[coveralls-image]: https://img.shields.io/coveralls/yiminghe/next-context.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/yiminghe/next-context?branch=main
[node-image]: https://img.shields.io/badge/node.js-%3E=_18.0-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/next-context.svg?style=flat-square
[download-url]: https://npmjs.org/package/next-context## demo
```
pnpm i
npm run dev
```## docs
[Docs](https://github.com/yiminghe/next-context/blob/main/docs/index.md)
## Usage
### nextjs middleware
`src/middleware.ts````js
import { createMiddleware } from 'next-context/middleware';
export const middleware = createMiddleware();
export const config = {
matcher: '/((?!_next|favicon.ico|sitemap.xml|robots.txt).*)',
};
```### extends type
```ts
declare module 'next-context' {
interface NextContext {
user: string;
}
}
```### page
`src/app/page.tsx````js
import React from 'react';
import { withPageMiddlewares, getNextContext } from 'next-context';export default withPageMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user } = getNextContext();
return (
<>
{user}
>
);
},
);
```### action
`src/action/getUser.ts````js
import { withActionMiddlewares, getNextContext } from 'next-context';export default withActionMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user } = getNextContext();
return user;
},
);
```### route
`src/app/get/route.ts````js
import { withRouteMiddlewares,getNextContext } from 'next-context';export const GET = withRouteMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user, res } = getNextContext();
res.json({ user });
},
);
```### nginx
```
location /rewrite {
proxy_set_header X-Forwarded-URI $request_uri;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:3000/dynamic;
proxy_http_version 1.1;
# Disable buffering for streaming support
proxy_buffering off;
proxy_set_header X-Accel-Buffering no;
}
```