"I'm building a blog website and I'm having a routing issue in Next.js. I have a homepage, a blog page (bai-viet), and individual blog posts, and the directory structure is as follows:
├ app
├─ layout.tsx
├─ page.tsx
├─ [uid]
│ └─ page.tsx
├─ bai-viet
└─ [uid]
│ ├─ page.tsx
│ ├ page.tsx
│ ........
The problem I'm facing is that when navigating to '/bai-viet', instead of fetching data from app/bai-viet/page.tsx, it's fetching data from app/[uid]/page.tsx.
In my prismicio.ts file, I've configured the routes as follows:
const routes: prismic.ClientConfig['routes'] = [
{ type: 'page', path: '/', uid: 'home' },
{ type: 'page', path: '/:uid' },
{ type. 'page', path: '/bai-viet', uid: 'bai-viet' },
{ type: 'article_post', path: '/bai-viet/:uid' },
{ type: 'settings', path: '/' },
{ type: 'navigation', path: '/' }
]
This error only occurs after executing the 'npm run build' script on local or deployed on Vercel. The local development environment working correctly.
I follow this github repo, add new page.tsx file in bai-viet folder, define new route in prismicio.ts
{ type. 'page', path: '/bai-viet', uid: 'bai-viet' },
In admin dasboard Prismicio, I create a new page with UID == 'bai-viet'
I think UID == 'bai-viet' in app/[uid]/page.tsx conflict with app/bai-viet/page.tsx file, because in app/bai-viet/page.tsx file, I query data from uid == 'bai-viet'
export default async function ArticlePage() {
const client = createClient()
const page = await client.getByUID('page', 'bai-viet').catch(() => notFound())
const posts = await client.getAllByType('article_post', {
orderings: [{ field: 'my.article_post.publicDate', direction: 'desc' }]
})
return (
<>
<SliceZone slices={page.data.slices} components={components} />
<Bounded as='section'>
<div className='grid grid-cols-1 gap-10 md:grid-cols-2 lg:grid-cols-3'>
{posts.map((post) => (
<ArticlePostCard key={post.id} post={post} />
))}
</div>
</Bounded>
</>
)
}
Thanks everyone.