Vercel Edge Functions works locally and give 404 on production when trying to work with 'hostname-rewrites'

Viewed 702

I'm trying to work with the new edge functions from Vercel next.js and it's not working on production, always gives a 404 error.

I followed this example and nothing works.

maybe because I'm using i18n?

my _middleware.js file looks like this:

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}`)
    }
}
}

And under pages I'm using this structure:

/pages
 /_sites
  [sites]
    index.js

Please help me find a solution for that.

0 Answers
Related