All-encompassing NextJS App router with a focus on Internationalization

I am currently working with a folder structure that includes an optional catch-all feature. The issue I am facing is that the page does not change when the URL is altered to include ""/"" or ""/about-us"". It consistently remains on the home page. While the localization aspect functions correctly, it always defaults to the home page.

While I understand why this is happening, I would like the locale portion of the URL to be optional. For example, I would like it to accept URLs in the format of /en/page or simply /page. My default locale logic is operational and functioning properly.

Answer №1

To convert the route to a parameter-based route, extract the parameter from the page's props since it's automatically passed in. This will work like a catch-all route as long as you anticipate one of those parameters. If you're setting the route dynamically based on cookies or a user agent, simply add another page at the root and redirect to the appropriate translated page using this structure:

app/
    myPage/
           [lang]/
                  page.tsx // Page 1
           page.tsx // Page 2
const PageOne = (props: {params: {lang: string}}) => {
...
}
import { redirect } from "next/navigation" // Verify if this is the correct import location.
const PageTwo = async () => {
   const lang = await getLanguageFromCookiesOrUserAgentOrWhatnot()
   return redirect(`/myPage/${lang}`)
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Essential parameters needed in a Typescript function signature

My code includes the following type definition: type FuncWithRequiredParams = (a: number, b: number, c:string) => void const func1: FuncWithRequiredParams = (a: number, b: number, c: string) => {} // This is functional const func2: FuncWithRequiredP ...

Steps for creating a dynamic validation using a new form control

I have an item that needs to generate a form const textBox = { fontColor: 'blue', fontSize: '18', placeholder: 'email', name: 'input email', label: 'john', validation: { required: false } ...

Is there any distinction between using glob wildcards in the tsconfig.json file when specifying "include" as "src" versus "include" as "src/**/*"?

Is there a distinction between these two entries in the tsconfig.json file? "include": ["src"] "include": ["src/**/*"] Most examples I've come across use the second version, but upon reviewing my repository, ...

Avoid pressing on y mat-button-toogle

In my component, I have a button-toggle like this: <mat-button-toggle-group appearance="legacy"> <mat-button-toggle *ngFor="let session of sessionsArray!"> <fa-icon icon="calendar-alt"></fa-icon> ...

Ways to incorporate JavaScript code within Reactjs

I am currently working with Reactjs and using Nextjs. I am facing a challenge regarding integrating "index.html" with "index.js". At the bottom of "index.html", there is some JavaScript code that I need to transfer to another file. Can you advise me on w ...

Encountering a Typescript error when trying to pass a function as a prop that returns SX style

Imagine a scenario where a parent component needs to pass down a function to modify the styles of a reusable child component: const getStyleProps: StyleProps<Theme> = (theme: Theme) => ({ mt: 1, '.Custom-CSS-to-update': { padding ...

What could be causing TypeScript to raise an issue when a /// reference comes after the 'use strict' statement?

This particular inquiry is somewhat connected to a question I posted on Stack Overflow yesterday about why TypeScript is encountering issues when trying to import a module. The initial configuration remains unchanged. My TypeScript file appears as follows ...

Encountering an error while attempting to read the 'displayName' property in a Cypress component test due to its undefined nature

Currently, I have a functional Next.js application and my goal is to introduce Cypress component tests for my React components. While end-to-end Cypress tests are running smoothly, I'm encountering an error when trying to implement component tests. T ...

Exploring the Differences Between API Routes and getStaticProps

Forgive my confusion, but I'm struggling to grasp the distinction between the two. So, with getStaticProps, you can fetch data and display it on the site. But isn't that the same as using API routes? In both cases, you're fetching data from ...

Ways to retrieve data from an Observable and save it in an Array categorized by a specific identifier

The data I have is structured as follows: Location: lat: 43.252967 lng: 5.379856 __proto__: Object customerId: "5cd430c65304a21b9464a21a" id: "5d5a99c62a245117794f1276" siteId: "5d0ce7c4a06b07213a87a758" __proto__: Object 1: Location: {lat: 43.249466, lng ...

Removing a key from an index signature in Typescript - a step-by-step guide

In my web application built with Angular, we encountered a need for a data type to store translations of strings in different languages. To address this requirement, a team member defined the following type: export class TranslatedString { [language: str ...

How to Unsubscribe from an Angular 2 Subscription Automatically After a Timeout

I am looking for a way to disregard the response from my API in case it takes too long to fetch. Currently, I am using this.http.get(mysqlUrl).subscribe() to retrieve the response. However, I would like to terminate that subscription if it exceeds a dur ...

Top method in Angular 6 for verifying if a scrollable section has been scrolled to the very bottom

I am searching for a reliable solution in Angular 6 to determine whether a scrollable host component has reached its maximum scroll bottom. Despite my efforts, I have been unsuccessful in finding a functioning example of a function that can detect if a cu ...

The issue of images not appearing when using the next Image component in Strapi

Hey there, I'm currently using Strapi as my backend and attempting to display an image in my next application. I retrieve the URL from my API call, but when I insert it into the SRC attribute of my Image component, it renders a broken image. Here is ...

Separate an array in TypeScript based on the sign of each number, and then replace the empty spaces with null objects

Hey, I'm facing a little issue, I have an Array of objects and my goal is to split them based on the sign of numbers. The objects should then be dynamically stored in different Arrays while retaining their index and getting padded with zeros at the b ...

Experience the power of Next.js with a unique twist: Streaming SSR and PPR for SWR-like behavior

Currently, I am in the process of developing a data-intensive application for my company. The app consists of multiple pages, such as: /experiment - displaying a list of all experiments with data updated several times an hour /experiment/[id] - providing ...

What is the best way to address an eslint warning that says "'component' is declared but its value is never read," even though I am actively utilizing the component in my code?

I am encountering a strange error where 'sellerDetails' is declared but not being utilized in the component I am calling. Despite using it, this error just started occurring recently and now, even after importing the component, it is not visible ...

Display various elements depending on the size of the screen within Next.js

My goal is to display a component differently depending on whether the screen width is less than 768p or not. If the width is under 768p, I want to show the hamburger menu. Otherwise, I want to display the full menu. This is the code snippet I am using. ...

Using TypeScript arrow functions to define parameters

When setting "noImplicitAny": true in TypeScript, you may encounter the following errors: Parameter 'x' implicitly has an 'any' type This error can occur with the code snippet: .do(x => console.log(x)); Another error you might s ...

Containerizing Next.js with TypeScript

Attempting to create a Docker Image of my Nextjs frontend (React) application for production, but encountering issues with TypeScript integration. Here is the Dockerfile: FROM node:14-alpine3.14 as deps RUN apk add --no-cache tini ENTRYPOINT ["/sbin ...