Next.js Discord

Discord Forum

Incorrect metadata canonical URL when using next-intl and opennextjs

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Describe the bug

Hi, guys! 👋 I’ve been struggling with an i18n metadata issue when using next-intl and would really appreciate some guidance from the community.

Brief context

I try to migrate nextjs project to cloudflare workers. The project uses opennextjs as the adapter, and next-intl for i18n. 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

1. Create a Next.js project with nextjs, opennextjs and next-intl (v3+)
2. Configure i18n with as-needed mode:
   // middleware.ts
   export default createMiddleware({
     locales: ['en', 'zh'],
     defaultLocale: 'en',
     localePrefix: 'as-needed'
   });

3. Set metadata with relative canonical:
// app/[locale]/layout.tsx
const metadata: Metadata = {
  metadataBase: new URL('https://mysite.com'),
  alternates: { canonical: './' }
};

4. Run next dev → Open /en → Canonical shows https://acme.com (correct)
Run opennextjs-cloudflare build && opennextjs-cloudflare preview → Open /en → Canonical shows https://acme.com/en (incorrect)

Expected behavior

Canonical URL should consistently omit default locale prefixes in as-needed mode across all environments:

Dev: https://acme.com
Preview/Production: https://acme.com

Environment
nextjs version: 15.3.3
@opennextjs/cloudflare version: 1.0.4
Wrangler version: 4.19.1
next-intl version: 4.1.0

__

Question

Could anyone help me diagnose this? Specifically looking for:
1. Known solutions for static exports
2. Workaround experiences
3. Whether this might be a library limitation

Thanks so much for your time! Any pointers would be incredibly helpful 🙏

0 Replies