Next.js 13 + next-intl默认locale跳转localhost:3000问题求助
解决Next.js 13 + next-intl部署后默认locale重定向到localhost的问题
核对默认locale拼写一致性
你提到配置了uk和en两个locale,但默认locale设为ua,这里大概率存在拼写错误(乌克兰语标准locale代码是uk,ua是国家代码)。要确保next-intl.config.js、middleware.ts及路由配置里的默认locale完全统一,比如都设为uk:// next-intl.config.js export default createNextIntlConfig({ locales: ['uk', 'en'], defaultLocale: 'uk' });修正环境变量与域名配置
部署时未设置正确的生产环境域名,会导致next-intl的重定向逻辑错误指向localhost。排查项目中是否有硬编码的localhost地址,同时在部署平台的环境变量中添加NEXT_PUBLIC_APP_URL=https://test.com;还要确保middleware.ts里的重定向逻辑用环境变量而非硬编码:import createMiddleware from 'next-intl/middleware'; import {defaultLocale, locales} from './i18n.config'; export default createMiddleware({ locales, defaultLocale, localePrefix: 'always' }); export const config = { matcher: ['/', '/(uk|en)/:path*'] };检查路由自定义重定向逻辑
确认app/[locale]目录下的layout.tsx、page.tsx等文件中,有没有错误的redirect或permanentRedirect调用硬指向localhost,这类代码要直接删除或修正。清除部署平台缓存
像Vercel、Netlify这类平台会缓存旧的构建产物或路由规则,哪怕重新构建也可能生效。手动在平台后台清除缓存后重新部署:- Vercel:进入项目→Deployments→选最新部署→点击"Redeploy"并取消勾选"Use existing build cache"
- Netlify:进入项目→Deploys→点击"Trigger deploy"→选择"Clear cache and deploy site"
排查next.config.js配置
确认next.config.js里的basePath和assetPrefix配置正确,不要设置错误的路径影响域名解析:// next.config.js const withNextIntl = require('next-intl/plugin')('./i18n.config.js'); module.exports = withNextIntl({ basePath: '', assetPrefix: '' });
内容的提问来源于stack exchange,提问作者Olexiy Zhevakin
相关产品推荐
相关产品推荐

