Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: middleware and basePath combo #47085

Open
petmarsh opened this issue Mar 13, 2023 · 0 comments
Open

Docs: middleware and basePath combo #47085

petmarsh opened this issue Mar 13, 2023 · 0 comments
Labels
Documentation Related to Next.js' official documentation.

Comments

@petmarsh
Copy link

What is the improvement or update you wish to see?

Explicit note of a possibility to use the same path matching as in e.g. redirects (source, destination, has, ...) for middleware's matcher and some warning in basePath docs for middleware users

Is there any context that might help us understand?

Not sure if this falls under the category of a bug or it's just missing pieces in the documentation of middleware or testing scenario, but I did struggle for a while to get it fixed - so having it documented would probably help others
The missing piece is an explicit note of a possibility to use the same path matching as in e.g. redirects (source, destination, has, ...) for middleware's matcher
Bumped into this while fixing my problem (bug?) of a combination of having both middleware and basePath set.
The matcher's path matching array, e.g. the one in doc example ('/((?!api|_next/static|_next/image|favicon.ico).*)') works fine until you start deploying the app to a system where basePath is required
It affects only the root route (/) - which may be the most visited (or linked to) route in the entire app - and for both pages and appDir setups

So let's say we have the above matcher setup from docs in middleware

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

That produces regex '^(?:\/(_next\/data\/[^/]{1,}))?(?:\/((?!api|_next\/static|_next\/image|favicon.ico).*))(.json)?[\/#\?]?$' against which the path is matched, where in case of a undefined basePath root route is '/'
Works as it should
Then if we need basePath updated for deployment to another system this regex is slightly updated
Let's say we use basePath: 'withbase' in next.config.js
then the regex is '^\/withbase(?:\/(_next\/data\/[^/]{1,}))?(?:\/((?!api|_next\/static|_next\/image|favicon.ico).*))(.json)?[\/#\?]?$'
Unfortunately the requested/rendered path is from the server's point of view '/withbase' and that does not match with this regex and/or the required rule 1 of matchers "MUST start with /", which ultimately translates to the '/withbase/' as a new root route
Changing trailingSlash setup has no effect on this matching

So maybe another note in the basePath docs should include a warning for middleware users that matcher needs modification, which for me was

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

Tested with Next.js up to 13.2.4 (React 18, node v16 and v18)

Does the docs page already exist? Please link to it.

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation.
Projects
None yet
Development

No branches or pull requests

1 participant