GithubHelp home page GithubHelp logo

i18n locales 404 issues about platforms HOT 4 CLOSED

vercel avatar vercel commented on May 15, 2024
i18n locales 404 issues

from platforms.

Comments (4)

Smart387doo avatar Smart387doo commented on May 15, 2024 1

Well if you log stuff, you will see strange behavior, it does not add locale to the URL for default locale, but does for other ones.

If you do:

url.pathname = `/${locale}/_sites/${currentHost}${pathname}`;

then your default one will work, but you'll get 404 on all others. And reason is that other locales are added twice, once by you, and once by system. I solved it by adding this:

const { pathname, locale, defaultLocale } = req.nextUrl;
.
.
.
if (currentHost == "app") {
      if (
        pathname === "/login" &&
        (req.cookies["next-auth.session-token"] ||
          req.cookies["__Secure-next-auth.session-token"])
      ) {
        url.pathname = `/${locale}`;
        return NextResponse.redirect(url);
      }

      url.pathname = `/${locale}/app${pathname}`;
      return NextResponse.rewrite(url);
    }

    if (hostname === "localhost:3000" || hostname === "yourdomain.something") {
      url.pathname = `/${locale}/home${pathname}`;
      return NextResponse.rewrite(url);
    }

    if (locale === defaultLocale) {
      url.pathname = `/${locale}/_sites/${currentHost}${pathname}`;
      return NextResponse.rewrite(url);
    }

    url.pathname = `/_sites/${currentHost}${pathname}`;
    return NextResponse.rewrite(url);
  }

Hope it helps

from platforms.

wuichen avatar wuichen commented on May 15, 2024

im having the exact same issue when using i18n

from platforms.

steven-tey avatar steven-tey commented on May 15, 2024

Hey guys! Sorry for the late response but this should've been fixed in the latest version of Next.js. Feel free to reference this issue here: vercel/next.js#36563

from platforms.

eugle avatar eugle commented on May 15, 2024

The problem still exists:

nextjs version

"next": "13.2.4",

middleware code:

import { NextRequest, NextResponse } from "next/server";

export const config = {
  matcher: [
    "/((?!api/|_next/|_static/|/|[\\w-]+\\.\\w+).*)",
  ],
};

export default async function middleware(req: NextRequest) {
  const url = req.nextUrl;

  const hostname = req.headers.get("host") || "demo.localhost:3000";

  const path = url.pathname;

  return NextResponse.rewrite(
    new URL(`/_sites/${currentHost}${path}`, req.url)
  );
}

nextconfig code

/**
 * @format
 * @type {import('next').NextConfig}
 */

const withBundleAnalyzer = require('@next/bundle-analyzer')({
	enabled: process.env.ANALYZE === 'true',
});

const nextConfig = {
	reactStrictMode: true,
	poweredByHeader: false,
	i18n: {
		localeDetection: false,
		locales: ['en', 'cn', 'jp', 'it', 'tw', 'kr', 'hk', 'fr'],
		defaultLocale: 'cn',
	}
};

module.exports = withBundleAnalyzer(nextConfig);

The problem still exists

from platforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.