Vercel Edge Functions 在本地工作,并在尝试使用“主机名重写”时在生产中给出 404

Avi*_*han 5 url-rewriting next.js serverless vercel

我正在尝试使用 Vercel next.js 中的新边缘函数,但它在生产中不起作用,总是给出 404 错误。

我按照这个例子做了,但没有任何效果。

也许是因为我使用的是 i18n?

我的_middleware.js文件如下所示:

import {NextResponse } from 'next/server'

export default function middleware(req) {
    const { pathname } = req.nextUrl
    let hostname = req.headers.get('host')


if (
    !pathname.includes('.') && // exclude all files in the public folder
    !pathname.startsWith('/api') // exclude all API routes
) {
    if (
        !hostname.includes('example.vercel.app') &&
        hostname !== 'example.com' &&
        hostname !== 'localhost:3000'
    ) {
        return NextResponse.rewrite(`/_sites/${hostname}${pathname}`)
    }
}
}
Run Code Online (Sandbox Code Playgroud)

在页面下我使用这个结构:

/pages
 /_sites
  [sites]
    index.js
Run Code Online (Sandbox Code Playgroud)

请帮我找到解决方案。