-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Nextjs] Create plugins approach for the nextjs middleware (#945)
* [Nextjs] Create plugins approach for the nextjs middleware * add NextFetchEvent
- Loading branch information
1 parent
7beb44d
commit 8d452e7
Showing
4 changed files
with
44 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/create-sitecore-jss/src/templates/nextjs/src/lib/middleware/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextFetchEvent, NextRequest } from 'next/server'; | ||
import * as plugins from 'temp/middleware-plugins'; | ||
|
||
export interface MiddlewarePlugin { | ||
/** | ||
* Detect order when the plugin should be called, e.g. 0 - will be called first (can be a plugin which data is required for other plugins) | ||
*/ | ||
order: number; | ||
/** | ||
* A middleware to be called, it's required to return @type {NextResponse} for other middlewares | ||
*/ | ||
(req: NextRequest, res: NextResponse, ev: NextFetchEvent): Promise<NextResponse>; | ||
} | ||
|
||
export default async function middleware(req: NextRequest, ev: NextFetchEvent): Promise<NextResponse> { | ||
const response = NextResponse.next(); | ||
|
||
return (Object.values(plugins) as MiddlewarePlugin[]) | ||
.sort((p1, p2) => p1.order - p2.order) | ||
.reduce((p, plugin) => p.then((res) => plugin(req, res, ev)), Promise.resolve(response)); | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/create-sitecore-jss/src/templates/nextjs/src/pages/_middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { NextRequest } from 'next/server'; | ||
import middleware from 'lib/middleware'; | ||
|
||
// eslint-disable-next-line | ||
export default async function (req: NextRequest) { | ||
return middleware(req); | ||
} |