I've recently built a Next.js website with the following structure:
- pages
- articles
- [slug].js
- index.js
- components
- nav.js
Within nav.js, I have set up routing for all links using next/link
, including in pages/articles/[slug].js
and pages/articles/index.js
<-- The latter serves as an "archive" page to display all articles.
However, I am facing issues getting Link to correctly direct to domain/articles/
and ensuring that generated links within that list work properly.
In each <Link />
, I am incorporating the
<Link href="/articles/[id].js" as={
/articles/${id}}>
approach, which functions during development. However, upon exporting to a static site, complications arise.
Initially, establishing a static link proves ineffective:
<Link href="/articles/index.js" as={`/articles/${id}`}>
leads to a link directing to domain/articles
, showing me the index of files in the generated articles/
directory. By manually adding ".html" at the end, I am able to access the page. Yet, when the same link is utilized within a generated articles/[id]
page link, it points to domain/articles/articles
.
I can't seem to pinpoint where I may be going wrong, and I am struggling to understand how to effectively leverage the <Link />
component to establish consistent and operational static links between pages.