I created a blog posts page with multiple posts listed. Each post has an associated [slug].js file when clicked on.
While the default English locale functions correctly, other locales result in a 404 error when attempting to open individual blog posts.
Although I believe the issue lies within the next.config.js file, I'm uncertain about its exact location.
The default URL functions properly:
localhost:3000/blog-posts/some-blog-post
However, this URL generates a 404 error:
localhost:3000/de/blogeintraege/some-blog-post
Any advice or assistance would be greatly appreciated!
next.config.js
module.exports = {
basePath: '',
reactStrictMode: true,
i18n: {
locales: ['en', 'de'],
defaultLocale: 'en',
},
async rewrites() {
return [
{
// Does not handle locales automatically as locale: false is set
source: '/de/blogeintraege',
destination: '/blog-posts',
locale: false,
},
...
}
}
blog-posts.js
export default function BlogPosts({ children, posts }) {
const router = useRouter();
const { locale } = router;
const routeTranslation = locale === 'en' ? enRoutes : deRoutes
return (
<div className="container p-4">
<div className="row justify-content-center">
{
posts.categories.edges[0].node.posts.edges.map(post => (
<div className="col-12 col-sm-10 card" key={post.node['slug']}>
<Link href={routeTranslation.publications + '/' + post.node['slug']} key={post.node['slug']}>
<div className="d-flex">
<div className="mb-3">
<img src={post.node['featuredImage']['node']['sourceUrl']} />
</div>
</div>
</Link>
</div>
))
}
</div>
</div>
)
}
export async function getStaticProps({locale}) {
const where = locale === "en" ? "Publishing" : "Blogeintraege";
const res = await fetch('https://example.com/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
query: `
query MyQuery {
categories(where: {name: "${where}"}) {
edges {
node {
posts {
edges {
node {
featuredImage {
node {
sourceUrl
}
}
customUrl {
customUrl
}
title
slug
}
}
}
}
}
}
}
`,
})
})
const json = await res.json()
return {
props: {
posts: json.data,
},
}
blog-posts/[blog-post].js
const BlogPost = ({ blogpost }) => {
const router = useRouter();
const { locale } = router;
const translation = locale === 'en' ? en : mk;
return (
<div className="container p-4">
<div className="row justify-content-center">
<div className="col-12 card">
</div>
<div className="text-white text-break px-4 text-cms" dangerouslySetInnerHTML={{ __html: blogpost.content }} />
</div>
</div>
</div>
)
}
export async function getStaticPaths({locale}) {
const res = await fetch('https://example.com/wp-migration/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
query: `
query MyQuery {
posts(first: 100, where: {categoryName: "Publishing"}) {
nodes {
slug
}
}
}
`,
})
})
const json = await res.json()
return {
paths: json.data.posts.nodes.map(blogpost => {
return {
params: {
blogpost: blogpost.slug
},
};
}),
fallback: false,
};
}
export async function getStaticProps({ params }) {
const res = await fetch('https://example.comwp-migration/graphql', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
query: `
query MyQuery {
postBy(slug: "${params.blogpost}") {
title
content
featuredImage {
node {
sourceUrl
}
}
}
}
`,
})
})
const json = await res.json()
return {
props: {
blogpost: json.data.postBy,
},
};
}
export default BlogPost