NextJS 13.4 应用程序路由中间件页面重定向没有样式

huangapple go评论58阅读模式
英文:

NextJS 13.4 App Router Middleware Page Redirect Has No Styles

问题

我正在使用以下中间件来创建重定向,如果用户不在登录界面上,这只是一个测试案例。最终,条件将检查有效的身份验证会话,并有条件地重定向。

但是,当重定向完成时,页面没有样式。

这是基于NextJS文档示例中的示例。

export async function middleware(req: NextRequest) {
    if(req.nextUrl.pathname != '/sign-in') {
        return NextResponse.redirect(new URL('/sign-in', req.url))
    }
}

禁用中间件(注释掉它),我可以无问题地加载/sign-in。在“来源”开发工具选项卡中,我看到了这个。

NextJS 13.4 应用程序路由中间件页面重定向没有样式 NextJS 13.4 应用程序路由中间件页面重定向没有样式

启用中间件显示零样式(使用默认的tailwind配置)。它还显示一个次要的登录文档(紫色图标),似乎被识别为完全不同的页面。未经样式处理的页面似乎不具有响应性。

NextJS 13.4 应用程序路由中间件页面重定向没有样式 NextJS 13.4 应用程序路由中间件页面重定向没有样式

我对为什么会发生这种情况感到困惑,或者是否有更好的方法来创建中间件身份验证,如果没有有效的会话(或出现任何条件),则将用户重定向到登录页面。

英文:

I am using the following middleware to create a redirect if the user is not on the sign-in screen. This is just a test case. Eventually, the condition would check for a valid auth session and conditionally redirect.

However, when the redirect completes, the page has no styles.

This is based on the example from the NextJS documentation example.

export async function middleware(req: NextRequest) {
    if(req.nextUrl.pathname != '/sign-in') {
        return NextResponse.redirect(new URL('/sign-in', req.url))
    }
}

Disabling the middleware (commenting it out), I can load /sign-in without issues. In the "sources" dev tools tab, I see this.

NextJS 13.4 应用程序路由中间件页面重定向没有样式 NextJS 13.4 应用程序路由中间件页面重定向没有样式

Enabling the middleware shows zero styles (using the default tailwind configuration). It also shows a secondary sign-in document (purple icon) that appears to be recognized as a completely different page. The unstyled page does not appear to be reactive.

NextJS 13.4 应用程序路由中间件页面重定向没有样式 NextJS 13.4 应用程序路由中间件页面重定向没有样式

I am confused about why this is happening or if there is a better way to create middleware auth-guarding that redirects users to the sign-in page if there is no valid session (or for whatever condition).

答案1

得分: 4

好,以下是翻译好的部分:

上述重定向路径条件是为了避免在我的测试过程中发生重定向循环。然而,它存在缺陷,因为它将每个请求都重定向到 /sign-in,包括样式。

我将保留这个问题,以防有人犯这个愚蠢的错误。

仅重定向特定路径

如果你确实想有条件地从一个路径重定向到另一个路径而不搞砸一切,你可以参考完整示例并包括中间件的匹配器配置。

export const config = {
    matcher: '/about/:path*',
};

然而,这会将你整个中间件系统的功能限制为这个配置。

防止 NextJS 13 应用程序路由中间件在静态文件上运行

如果你只想阻止中间件在静态文件(如样式和字体)上运行,你可以使用这个配置:

// 阻止中间件在静态文件上运行
export const config = { matcher: '/((?!.*\\.).*)&' }
英文:

Well all it took was me creating this stack overflow question to finally realize the answer myself.

The above redirect path condition was created to avoid a redirect loop during my testing. However, it is flawed because it redirects EVERY request to /sign-in including the styles.

I'll leave this up in case anyone makes this stupid mistake.

Redirect Only Specific Paths

In the event you do want to conditionally redirect from one path to another without screwing up everything, you can follow the full example and include the matcher config for the middleware.

export const config = {
    matcher: '/about/:path*',
};

However, this limits the capabilities of your entire middleware system to this config.

Prevent NextJS 13 App Router middleware from running on static files

If you want to just stop Middleware from running on static files (such as your styles and fonts), you can use this config:

// Stop Middleware running on static files
export const config = { matcher: '/((?!.*\\.).*)' }

huangapple
  • 本文由 发表于 2023年5月28日 02:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定