next-compose-middlewares
v0.1.5
Published
- 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 components inside Page a
Downloads
446
Readme
next-compose-middlewares
- 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 components inside Page and functions inside Route/Action
demo
pnpm i
npm run dev
docs
Usage
nextjs middleware
src/middleware.ts
export { middleware } from 'next-compose-middlewares/middleware';
extends type
declare module 'next-compose-middlewares' {
interface NextContext {
user: string;
}
}
page
src/app/page.tsx
import React from 'react';
import { withPageMiddlewares, getNextContext } from 'next-compose-middlewares';
export default withPageMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user } = getNextContext();
return (
<>
<p>{user}</p>
</>
);
},
);
action
src/action/getUser.ts
import { withActionMiddlewares, getNextContext } from 'next-compose-middlewares';
export default withActionMiddlewares([
async (context, next) => {
context.user = 'test';
await next();
}])(
async () => {
const { user } = getNextContext();
return user;
},
);
route
src/app/get/route.ts
import { withRouteMiddlewares,getNextContext } from 'next-compose-middlewares';
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;
}