使用新的NextJS 12.2中间件重定向路由时的应用
我正在将我的旁边更新到12.2.0
。
我以前将中间件嵌套在>页/_Middleware
中,但是使用此较新版本,这不再起作用。
我正在关注升级指南
:
src/Pages/_middleware.js
export function middleware(req) {
const userSession = req.headers.get('cookie');
const url = req.nextUrl.clone();
if (userSession?.includes('userToken')) {
if (req.nextUrl.pathname === '/login') {
url.pathname = '/';
return NextResponse.redirect(url);
}
return NextResponse.next();
}
url.pathname = '/login';
return NextResponse.rewrite(url);
}
从此
src/middleware.js
import { NextResponse } from 'next/server';
export function middleware(req) {
const userSession = req.headers.get('cookie');
const reqUrl = req.nextUrl.pathname;
if (userSession?.includes('userToken')) {
if (reqUrl.startsWith('/login')) {
// this one works!
return NextResponse.redirect(new URL('/', req.url));
}
return NextResponse.next();
}
if (reqUrl.startsWith('/login')) {
return NextResponse.next();
};
const loginUrl = new URL('/login', req.url);
return NextResponse.redirect(loginUrl);
};
开始 用户被重定向到/login
他获得了一个空白页(没有下一个HTML),并且在控制台中所有这些错误:
untured syntaxerror:意外的令牌'<' (在 React-refresh.js?ts = 1656708611639:1:1)webpack.js?ts = 1656708611639:1 未接收的Syntaxerror:意外的令牌'<' (在 webpack.js?ts = 1656708611639:1:1)main.js?ts = 1656708611639:1未被教 SyntaxError:意外的令牌'<' (在main.js?ts = 1656708611639:1:1) } (在_app.js?ts = 1656708611639:1:1)courses.js?ts = 1656708611639:1 未接收的Syntaxerror:意外的令牌'<' (在 courses.js?ts = 1656708611639:1:1) _buildmanifest.js?ts = 1656708611639:1未熟悉的Syntaxerror:意外的令牌'<' (在_buildmanifest.js?ts = 1656708611639:1:1) _SSSGMANIFEST.JS?ts = 1656708611639:1未接收的Syntaxerror:意外的令牌'<' (在_ssgmanifest.js?ts = 1656708611639:1:1)
奇怪的是,如果用户为auth,则第一个语句重定向。
编辑2
如果我按照文档中的文档添加匹配器,则可以正常工作:
// middleware.js
export function middleware(req) {
...
}
export const config = {
matcher: ['/login', '/'],
};
但是,我无法为每个受保护的路线添加匹配项。这是一个完整的过度杀伤力。
I'm updating my next to 12.2.0
.
I used to have my middleware nested in Pages/_middleware
but with this newer version this doesn't work anymore.
I'm following the upgrade guide
Gone from this:
src/Pages/_middleware.js
export function middleware(req) {
const userSession = req.headers.get('cookie');
const url = req.nextUrl.clone();
if (userSession?.includes('userToken')) {
if (req.nextUrl.pathname === '/login') {
url.pathname = '/';
return NextResponse.redirect(url);
}
return NextResponse.next();
}
url.pathname = '/login';
return NextResponse.rewrite(url);
}
To this
src/middleware.js
import { NextResponse } from 'next/server';
export function middleware(req) {
const userSession = req.headers.get('cookie');
const reqUrl = req.nextUrl.pathname;
if (userSession?.includes('userToken')) {
if (reqUrl.startsWith('/login')) {
// this one works!
return NextResponse.redirect(new URL('/', req.url));
}
return NextResponse.next();
}
if (reqUrl.startsWith('/login')) {
return NextResponse.next();
};
const loginUrl = new URL('/login', req.url);
return NextResponse.redirect(loginUrl);
};
However once the user is redirected to /login
he gets a blank page (no next html) and all these errors in the console:
Uncaught SyntaxError: Unexpected token '<' (at
react-refresh.js?ts=1656708611639:1:1) webpack.js?ts=1656708611639:1
Uncaught SyntaxError: Unexpected token '<' (at
webpack.js?ts=1656708611639:1:1) main.js?ts=1656708611639:1 Uncaught
SyntaxError: Unexpected token '<' (at main.js?ts=1656708611639:1:1)
_app.js?ts=1656708611639:1 Uncaught SyntaxError: Unexpected token '<' (at _app.js?ts=1656708611639:1:1) courses.js?ts=1656708611639:1
Uncaught SyntaxError: Unexpected token '<' (at
courses.js?ts=1656708611639:1:1)
_buildManifest.js?ts=1656708611639:1 Uncaught SyntaxError: Unexpected token '<' (at _buildManifest.js?ts=1656708611639:1:1)
_ssgManifest.js?ts=1656708611639:1 Uncaught SyntaxError: Unexpected token '<' (at _ssgManifest.js?ts=1656708611639:1:1)
Oddly, the first if
statement redirect when the user is auth works.
Edit 2
If I add a matcher as per in the docs, it works:
// middleware.js
export function middleware(req) {
...
}
export const config = {
matcher: ['/login', '/'],
};
However, I can't add a match for every protected route. It's a complete overkill.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这以前工作吗?通过阅读逻辑,对我来说似乎是一个无尽的循环:
/login
中降落,他们将被重定向到/
/login
我知道有一些额外的条件可以匹配这些重定向,但是在您的情况下,您正在与它们匹配,以无尽的重定向循环结束:
USERTOKEN
并尝试访问/login
- &gt;通过您的逻辑,它将被重定向到/
usertoken
,然后尝试访问/
(或其他任何其他URL) - &gt;通过您的逻辑,它将被重定向到/login
usertoken
,然后尝试访问/login
- &gt;这再次是第1点!Was this working before? By reading the logic, it seems like an endless loop to me:
/login
, they get redirected to/
/login
I know there are some extra conditions to match those redirects, but I think in your case you are matching them, ending in an endless loop of redirects:
userToken
and try to access/login
--> by your logic, it gets redirected to/
userToken
and try to access/
(or whatever other URL really) --> by your logic, it gets redirected to/login
userToken
and try to access/login
--> This is point 1 again!