-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Middleware: Support for basePath
in the app
directory
#243
Comments
Hey, thanks for starting this discussion! This is definitely a valid feature request, I'll convert this to an issue. I just saw that in the |
I'm having this problem too. It only happens when I try to go to the locale set to
and
If I try to navigate to |
Similar problem. The Next 13.4 documentation has basePath which doesn't work for this library. I use middleware.ts: import createMiddleware from 'next-intl/middleware'
import { locales, defaultLocale } from 'shared/lib/i18n'
export default createMiddleware({
// A list of all locales that are supported
locales: [...locales],
// If this locale is matched, pathnames work without a prefix (e.g. `/about`)
defaultLocale,
// Include a prefix for the default locale as well, you can configure the middleware accordingly.
localePrefix: 'always',
// Disable automatic locale detection
localeDetection: false,
})
export const config = {
// Skip all paths that should not be internationalized
matcher: ['/((?!api|_next|.*\\..*).*)'],
} next.config.js: const withNextIntl = require('next-intl/plugin')(
// This is the default (also the `src` folder is supported out of the box)
'shared/lib/i18n/index.ts'
)
const isProduction = process.env.NODE_ENV === 'production'
/** @type {import('next').NextConfig} */
const nextConfig = {
assetPrefix: isProduction ? '/path' : '',
basePath: isProduction ? '/path' : '',
rewrites() {
return isProduction
? [
{
source: '/path/_next/:path*',
destination: '/_next/:path*',
},
]
: []
},
}
module.exports = withNextIntl(nextConfig) Redirected to I think this may be help: https://nextjs.org/docs/pages/api-reference/next-config-js/runtime-configuration. |
I've been fighting this for the past week. I believe it's a bug in NextJS, but I'm not getting much of a response from them. |
Hi, just chipping in to say that we are currently running into this same issue. Similarly to @setowilliam this only seems to be an issue for the default locale. |
I haven't looked deeper into this and currently have to look into a few other things. So if someone is interested in contributing this feature, you're more than welcome to! Middleware: https://github.com/amannn/next-intl/blob/main/packages/next-intl/src/middleware/middleware.tsx Just leave a note here if someone wants to look into this, so we don't do the work redundantly! |
Hi, maybe I am stating the obvious here but sharing it anyway for those who it may help. Setting the Then in the root I am aware this isn't really a permanent solution but maybe it helps someone bridge the gap for now. |
basePath
in the app
directorybasePath
in the app
directory
vote this! |
I'm interested in improving this feature. |
@koyama-tagbangers Great, let me know if you need help! Starting with an option on the middleware is likely a good idea, yes—at least for the beginning. By doing this, we could already release the feature to the stable channel, since there's no plugin there yet. Potentially we could add an automatic default with the plugin later. |
Side note: I think |
Hi, any updates here. Is it any way to make it work with basePath? |
To bad, need to choose another lib because of this.. 😑 |
we had to switch back to pages-router because of this. it works there |
I think @dellacagna found a workaround for this in #549. It's a bit of an advanced use case to explore though, see e.g. my note about the navigation APIs below. I'd really like to have built-in support for |
This should really be written into the docs. I spent two days migrating my website to realize during production that |
Sorry to hear about your frustration @FlorianLeChat! Is your use case a static Please let me know if someone wants to step in to work on this! I currently still have a few other priorities, but if no one else looks into it, eventually I'll try to take care of it. I can't promise an ETA at this point though. |
Thanks for your quick reply! Honestly, about 90% of developers in my opinion who use a base path will always use it statically. I'd really like to help you, but unfortunately it's definitely beyond my skills. |
Hey @amannn I have a fairly rough branch on my fork that I have gotten working locally via
|
Any eta on this? |
Same issue |
I just found a workaround, here is it: Although you may not be able to read Chinese characters, I believe that you only need to read the code to solve the problem. Good luck! |
by @Robjam and @amannn) There's no need for a new config option, `next-intl` will automatically pick up the `basePath` you've configured in `next.config.js`. Note however that you should make sure that you match `/` explicitly in your middleware `matcher` (see [the updated docs](https://next-intl-docs.vercel.app/docs/routing/middleware#base-path)). The only integration points are: - Apply the `basePath` for redirects, as Next.js doesn't handle this internally - Apply the `basePath` for rewrites, as Next.js doesn't handle this internally - Apply the `basePath` for alternate links This PR is based on the initial work in #589 by @Robjam Closes #589 Closes #243 --------- Co-authored-by: Robjam <rjames@smartshopping.co.jp> Co-authored-by: James Roberts <github@robjam.dev>
Support for Many thanks to @Robjam for getting the ball rolling here! 🙌 |
Currently the middleware assumes that
/
is the root of the app and works on top of that.We could adapt it as follows:
next-intl/packages/next-intl/src/middleware/middleware.tsx
Line 14 in ed10033
basePath
needs to be considerednext-intl/packages/next-intl/src/middleware/middleware.tsx
Line 96 in ed10033
basePath
needs to be passed to the middleware. Maybe we could use the plugin that is worked on in feat!: Next.js 13 RSC integration #149 to read it from the Next.js config and then make it automatically available to the middleware (e.g. withwebpack.DefinePlugin
).Potentially this could also help to implement region-specific routing, see #243 (comment) and #549
(discussed in #242)
The text was updated successfully, but these errors were encountered: