Open
Description
Describe the bug
When using next-intl in as-needed
mode, the canonical
URL in metadata shows the correct path (https://mysite.com
) during next dev
, but incorrectly appends the default locale (/en
) during next preview
or production builds. This violates the as-needed
mode specification where default locales shouldn't appear in URLs.
Steps to reproduce
- Create a Next.js project with nextjs, opennextjs and next-intl (v3+)
- Configure i18n with
as-needed
mode:
// middleware.ts
export default createMiddleware({
locales: ['en', 'zh'],
defaultLocale: 'en',
localePrefix: 'as-needed'
});
- Set metadata with relative canonical:
// app/[locale]/layout.tsx
const metadata: Metadata = {
metadataBase: new URL('https://mysite.com'),
alternates: { canonical: './' }
};
- Run
next dev
→ Open /en → Canonical shows https://mysite.com (correct) - Run
opennextjs-cloudflare build && opennextjs-cloudflare preview
→ Open /en → Canonical shows https://mysite.com/en (incorrect)
Expected behavior
Expected behavior
Canonical URL should consistently omit default locale prefixes in as-needed mode across all environments:
- Dev: https://mysite.com
- Preview/Production: https://mysite.com
@opennextjs/cloudflare version
1.0.4
Wrangler version
4.19.1
next info output
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041
Available memory (MB): 24576
Available CPU cores: 12
Binaries:
Node: 22.11.0
npm: 10.9.0
Yarn: N/A
pnpm: 10.8.0
Relevant Packages:
next: 15.3.3 // There is a newer version (15.3.5) available, upgrade recommended!
eslint-config-next: 15.3.3
react: 19.1.0
react-dom: 19.1.0
typescript: 5.8.3
Next.js Config:
output: N/A
⚠ There is a newer version (15.3.5) available, upgrade recommended!
Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
Read more - https://nextjs.org/docs/messages/opening-an-issue
Additional context
No response