Next.js Discord

Discord Forum

How to use dynamic route with home page?

Answered
Somali posted this in #help-forum
Open in Discord
SomaliOP
My whole site consists of different blocks and in fact the main page is also a dynamic route for example [slug].js

Before that, I created index.js for the main page and [slug].js for the rest of the pages, but I realized that I was just copying the code of these two pages into each other, except for one detail.

I removed index.js and left only [slug].js, now my main page is only accessible via /home , which of course doesn't suit me.

I wrote this in next.config.js

 async rewrites() {
        return [
            {
                source: '/:locale/home',
                destination: '/:locale',
            },
        ];
    }


this removed the page from /home but it didn't appear on /, what am I doing wrong?
Answered by joulev
sorry i don't know what's wrong here
View full answer

21 Replies

@joulev js async rewrites() { return [ { source: '/:locale', destination: '/:locale/home', }, ]; }
SomaliOP
i also try this, but this not working, i guess cuz next.js require index.js file?
i cant npm run build without index.js
no nextjs doesn't require it
have you tried optional catch-all segments? does it work for your case?
@joulev have you tried optional catch-all segments? does it work for your case?
SomaliOP
sorry i cant understand how it works
i have error Error: A required parameter (slug) was not provided as an array received string in getStaticPaths for /[[...slug]]

with this
@joulev `console.log` the `paths` before the entire function `getStaticPaths` returns, what do you get
SomaliOP
@Somali Click to see attachment
.flat it one more time
@joulev `.flat` it one more time
SomaliOP
paths or item.alias?
@Somali paths or item.alias?
whatever that you console logged. the object returned by getStaticPaths must be
{
  paths: [
    { params: { slug: ["array", "of", "string"] }, locale: "en" },
    { params: { slug: ["array", "of", "string"] }, locale: "en" },
    { params: { slug: ["array", "of", "string"] }, locale: "en" },
    { params: { slug: ["array", "of", "string"] }, locale: "de" },
    { params: { slug: ["array", "of", "string"] }, locale: "de" },
    { params: { slug: ["array", "of", "string"] }, locale: "de" },
    { params: { slug: ["array", "of", "string"] }, locale: "tr" },
    { params: { slug: ["array", "of", "string"] }, locale: "tr" },
    { params: { slug: ["array", "of", "string"] }, locale: "tr" },
  ],
  fallback: ...
}
not
{
  paths: [
    [
      { params: [...], locale: "en" },
      { params: [...], locale: "en" },
      { params: [...], locale: "en" },
    ],
    [
      { params: [...], locale: "de" },
      { params: [...], locale: "de" },
      { params: [...], locale: "de" },
    ],
    [
      { params: [...], locale: "tr" },
      { params: [...], locale: "tr" },
      { params: [...], locale: "tr" },
    ],
  ],
  fallback: ...
}
SomaliOP
okay i got this
@Somali okay i got this
yeah then it has to be ["home"] not "home"
SomaliOP
with this, but still get same error
@joulev yeah then it has to be `["home"]` not `"home"`
SomaliOP
idk this ok?
i just
@Somali idk this ok? i just
i don't know your getAllPageIds or whatever so i don't know about that Promise.all, but this works so base your code on this

// pages/[[...slug]].tsx
import { GetStaticPathsResult } from "next";

type Params = {
  slug: string[];
};

export default function Page() {
  return <div>Hello world</div>;
}

export function getStaticPaths(): GetStaticPathsResult<Params> {
  return {
    fallback: false,
    paths: [
      { params: { slug: [] }, locale: "en" },
      { params: { slug: [] }, locale: "de" },
      { params: { slug: ["about"] }, locale: "en" },
      { params: { slug: ["about"] }, locale: "de" },
      { params: { slug: ["about", "us"] }, locale: "en" },
      { params: { slug: ["about", "us"] }, locale: "de" },
    ],
  };
}

export function getStaticProps() {
  return { props: {} };
}

Route (pages)                              Size     First Load JS
┌ ● /[[...slug]] (325 ms)                  285 B          76.6 kB
├   ├ /en
├   ├ /de
├   ├ /en/about
├   └ [+3 more paths]
sorry i don't know what's wrong here
Answer