How to use dynamic route with home page?
Answered
Somali posted this in #help-forum
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
this removed the page from /home but it didn't appear on /, what am I doing wrong?
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?
21 Replies
@Somali 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?
async rewrites() {
return [
{
source: '/:locale',
destination: '/:locale/home',
},
];
}i think you meant this? this should work
@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
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
i have error Error: A required parameter (slug) was not provided as an array received string in getStaticPaths for /[[...slug]]
with this
@Somali 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
console.log the paths before the entire function getStaticPaths returns, what do you get@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: ...
}@joulev whatever that you console logged. the object returned by `getStaticPaths` must be
ts
{
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: ...
}
SomaliOP
sorry i cant understand.
i understand what i shold do, but i dont know how
i understand what i shold do, but i dont know how
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
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]@joulev 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
ts
// 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]
SomaliOP
idk i try a lot of variations and still get 404 or some error.
But I've noticed that when a hot reload happens, I don't get a 404, I get a page, and page transitions don't work with page reload.
do you have some example with locales loop?
this my api ref for all pages if you can check this please
https://next.virtuaethic.com/api/de/resources
But I've noticed that when a hot reload happens, I don't get a 404, I get a page, and page transitions don't work with page reload.
do you have some example with locales loop?
this my api ref for all pages if you can check this please
https://next.virtuaethic.com/api/de/resources
sorry i don't know what's wrong here
Answer