Next.js Discord

Discord Forum

NextJS wont recornise middleware.ts

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
src\middleware.ts

import { NextRequest } from "next/server";

export default async function name(req: NextRequest) {
    if (req.nextUrl.pathname === '/') {
        console.log('TRACK!')
    }
}

export const matcher = {
    matcher: ['/'],
}


next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
    matcher: ['/*'],
}

module.exports = nextConfig

5 Replies

Barbary Lion
pretty sure "name" is a reserved keyword try naming your function.
Komondor
Don't export default. And name the function middleware
@Sun bear src\middleware.ts import { NextRequest } from "next/server"; export default async function name(req: NextRequest) { if (req.nextUrl.pathname === '/') { console.log('TRACK!') } } export const matcher = { matcher: ['/'], } next.config.js /** @type {import('next').NextConfig} */ const nextConfig = { matcher: ['/*'], } module.exports = nextConfig
change middleware to
import { NextRequest } from "next/server";

export async function middleware(req: NextRequest) {
    if (req.nextUrl.pathname === '/') {
        console.log('TRACK!')
    }
}

export const matcher = {
    matcher: ['/'],
}

and you can remove the extra config option from nextcofig
Sun bearOP
Thanks that looks much cleaner ! but I found my problem...

I head to store it in my main that first off all and second I have 2 middleware.ts scripts at the moment that is a conflicted.

import { NextRequest } from "next/server";

export async function middleware(req: NextRequest) {
    if (req.nextUrl.pathname === '/') {
        console.log('TRACK!')
    }
}

export const matcher = {
    matcher: ['/'],
}


and


export { default } from "next-auth/middleware";

export const config = { matcher: ["/dashboard/:path*", "/admin/:path*", "/users/:path*", "/main/:path*", "/editCv/:path*",  "/cv/:path*"] };
`

this is a conflicted happening because you can store just one middleware