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

Fetching my endpoints from client component without locale path #553

Closed
denkochev opened this issue Oct 6, 2023 · 2 comments
Closed

Fetching my endpoints from client component without locale path #553

denkochev opened this issue Oct 6, 2023 · 2 comments
Labels
bug Something isn't working unconfirmed Needs triage.

Comments

@denkochev
Copy link

Description

I have one problem and one question.

  1. I'm using "next": "13.5.1" and "next-intl": "^3.0.0-rc.2".
    My default locale doesn't work. Every time I call URL without language it redirects me to URL with /de/ path in it. My navigation.ts:
import { createSharedPathnamesNavigation } from 'next-intl/navigation';

export const locales = ['de', 'ua'];
export const defaultLocale = 'de';

export const { Link, redirect, usePathname, useRouter } =
  createSharedPathnamesNavigation({ locales });
  1. How can I avoid locale when I call fetch in my client component, example:
const userText = await fetch('api/whisper', { ... })

This request this URL -> http://localhost:3000/ua/api/whisper
I want http://localhost:3000/api/whisper

Thank you

Mandatory reproduction URL (CodeSandbox or GitHub repository)

https://codesandbox.io/p/sandbox/boring-knuth-crls32

Reproduction description

/app/[locale]/layout.tsx

...

export type Locale = (typeof locales)[number];

export function generateStaticParams() {
  return locales.map((locale) => ({ locale }));
}

export default function RootLayout({
  children,
  params: { locale },
}: {
  children: React.ReactNode;
  params: { locale: Locale };
}) {
  /*
    Unstable version of next-intl for static rendering
  */
  const isValidLocale = locales.some((cur) => cur === locale);
  if (!isValidLocale) notFound();

  unstable_setRequestLocale(locale);
...

middleware.ts

import { defaultLocale, locales } from '@/navigation';
import createMiddleware from 'next-intl/middleware';

export default createMiddleware({
  locales,
  defaultLocale,
});

export const config = {
  // Skip all paths that should not be internationalized. This example skips
  // certain folders and all pathnames with a dot (e.g. favicon.ico)
  matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'],
};

navigation.ts

import { createSharedPathnamesNavigation } from 'next-intl/navigation';

export const locales = ['de', 'ua'];
export const defaultLocale = 'de';

export const { Link, redirect, usePathname, useRouter } =
  createSharedPathnamesNavigation({ locales });

Expected behaviour

When I call website without locale (/en/ or /de/ just localhost:3000) I expect my URL without locale but with the default language.

@denkochev denkochev added bug Something isn't working unconfirmed Needs triage. labels Oct 6, 2023
@zolero
Copy link

zolero commented Oct 7, 2023

I think you need to add localePrefix as stated here in the documentation locale-prefix-as-needed.

export default createMiddleware({
  locales,
  defaultLocale,
  localePrefix: 'as-needed'
});

@denkochev
Copy link
Author

Oh, I actually managed to fetch my endpoints without locale.
Instead of:

const userText = await fetch('api/whisper', { ... })

I had to do this:

const userText = await fetch('/api/whisper', { ... })

And big thanks for it, this works:

I think you need to add localePrefix as stated here in the documentation locale-prefix-as-needed.

export default createMiddleware({
  locales,
  defaultLocale,
  localePrefix: 'as-needed'
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants