Next.js Discord

Discord Forum

Issue with the middleware matcher

Answered
gin posted this in #help-forum
Open in Discord
ginOP
So im trying to match all request except /ocelot/evidence/:id

export const config = {
  matcher: [
    "/((?!^/ocelot/evidence/\\d+$).*)",
  ],
};


const nextConfig = {
    basePath: "/ocelot",
    compiler: {
        styledComponents: true,
    }, images: {
        remotePatterns: [
            {
                protocol: "http",
                hostname: "**",
            },
            {
                protocol: "https",
                hostname: "**",
            },
        ],
    },
    env: {
        COMMIT_HASH,
        COMMIT_DATE,
    }
}

In my nextconfig i have basepath set as /ocelot so when a user goes on domain.com/ocelot for my nextapp it will be only domain.com/
this should be correct right?

60 Replies

ginOP
now my issue is that my matcher only matches /ocelot/ocelot even tho it should also match /ocelot
if i go on http://localhost:3000/ocelot my logs show me
http://localhost:3000/ocelot/_next/static/chunks/webpack.js?v=1716670501873
http://localhost:3000/ocelot/_next/static/chunks/main-app.js?v=1716670501873
but not the path itself
only the assets
its weird its like my app doesnt recognize the path even tho its rendering the layout
it doesnt recognize the /ocelot path but every other like /ocelot/123
export const config = {
  matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};
even after this it still doesnt work
i could use conditional statements but how if request.nextUrl.pathname returns null
ginOP
bump
ginOP
bump
ginOP
bump
Lemme see
@gin I think it should be /ocelot/path/**/*
@Anay-208 <@759064479521701888> I think it should be `/ocelot/path/**/*`
ginOP
my matcher should match everything
Can you try what i told you?
i didnt explain it correctly look bro,
in my config i have
basePath: "/ocelot",

now when i go on:
http://localhost:3000/ocelot

middleware doesnt recognize the request even tho i have this in matcher
console.log(request.nextUrl.pathname);

export const config = {
  matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};


when i go on http://localhost:3000/ocelot/123
i get logged
/123

thats correct!
@Anay-208
when i remove basePath from config it works
the problem is my whole app needs to be on the /ocelot path because i have other apps runing on /123
example.
@gin <@755810867878297610>
Mark the message which solved your question as answer
😭
thats the problem i have
@Anay-208 Remove ocelot on matching then
ginOP
nono, i have to match ocelot
everything that is on /ocelot
domain.com/ocelot is for nextjs just /
because of the basepath
but that / is not matched
thats my problem
its not detected even tho my page is still rendered on /
@gin nono, i have to match ocelot
Try removing it since basePath is ocelot
@Anay-208 Try removing it since basePath is ocelot
ginOP
remove what?
export const config = {
  matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};

this is matching everything right
so it should also match /
but its not
i had this issue before aswell
i kinda fixed it with some thirdparty router for the middlewarew
but i dont want to use that
ginOP
its not fixing btw
ginOP
bump
ginOP
bump
ginOP
bump
ginOP
bump
ginOP
bump
American Crow
just a heads up mods will ban you if you bump more than once a day
try to help me, its important
Original message was deleted
ginOP
alr
ginOP
bump
ginOP
bumpuus
Answer
ginOP
this fixed my issue
@gin https://github.com/vercel/next.js/issues/47085#issue-1621947306
ginOP
@Anay-208 if you need this at any time have a look at this
this was the fix
export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico).*)',
    { source: '/' },
 ]
}