6

I am new to NEXT.JS and I am having some trouble deploying a static site. I have a component Nav.tsx that looks like this:

const Nav = () => {
    return (
        <nav className='nav p-3 border-bottom'>
            <Link href='/' passHref>
                <a className='pointer my-auto h2'>Blog </a>
            </Link>
            <Link href='/about' passHref>
                <a className='ms-5 pointer lead my-auto'>Who We Are</a>
            </Link>
            <Link href='/services' passHref>
                <a className='ms-5 pointer lead my-auto'>Services</a>
            </Link>
            <Link href='/contact' passHref>
                <a className='ms-5 pointer lead my-auto'>Contact</a>
            </Link>
        </nav>
    );
};
export default Nav;

When I run my development server locally, I can use those links to navigate around my site. But when I build, using next build && next export the links do not work for navigation. All of those links (/about, /services, /contact) are files in my pages directory. Have I written something incorrect in Nav.tsx, or am I misunderstanding how NEXT works? Thank you!

3
  • "the links do not work for navigation" - can you be more specific? Does nothing happen when you click a link? Do you get a 404 instead? Commented Feb 12, 2022 at 19:22
  • Yes, nothing happens when you click a link.
    – btanner
    Commented Feb 13, 2022 at 17:24
  • Do you see any request 404ing in the Dev tools Network tab? Or any errors in the console? Commented Feb 13, 2022 at 17:31

1 Answer 1

1

Update 2022-02-14

This can be solved by applying {trailingSlash: true} inside the next.config.js. See How to deal with NextJS exporting files with .html extension but in<Link> there is no .html for details.

Original answer (which does NOT work)

I encountered the same problem very recently and found out that this can be solved by moving other non-index pages such as pages/search.js into pages/search/index.js. No need for hacking via .htaccess and so on.

However, you'll need to update your relative imports so Next.js will still be able to compile, such as:

import styles from '../styles/Home.module.css';

to

import styles from '../../styles/Home.module.css';
5
  • So if I have a component like this one that I reuse, it would have a path like pages/nav/index.tsx. wouldnt that mean if I went to /nav it would show just this small reusable component? Is there a way to avoid that?
    – btanner
    Commented Feb 13, 2022 at 17:28
  • In this case, nav.tsx is simply a component so you don't need to convert it into pages/nav/index.tsx. You only need to convert those TSX files which are responsible for /about, /services, and /contact pages, which should be pages/about.tsx, pages/services.tsx, and pages/contact.tsx respectively. Commented Feb 13, 2022 at 17:50
  • ah, so I will have pages/index.tsx, and hten pages/contact/index.tsx for everywhere I want to link to.
    – btanner
    Commented Feb 13, 2022 at 22:16
  • ^^ that did not work unfortunately.
    – btanner
    Commented Feb 13, 2022 at 22:22
  • I've updated the answer, and apparently you don't need to move pages/about.tsx to pages/about/index.tsx and so on. Commented Feb 14, 2022 at 11:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.