Issues with implementing the link component in next.js

I've been working on a blog using next.js and I'm having trouble with the header links not working. I followed the next.js documentation to create the links, but whenever I reload the website and try to click the links, my console shows: "GET 404 (Not Found)". I've double-checked for spelling mistakes and the folder structure, but everything seems correct. I'm feeling confused. Can someone offer assistance? Thank you.

import Link from "next/link";
import Image from "next/image";
import logo from "./image/emily-logo.jpg";

export default function Intro() {
  return (
    <section>
      <nav>
        <a href="/">HOME </a>

        <Link href="/intro-pages/about">ABOUT</Link>

        <Link href="/intro-pages/blog">BLOG</Link>

        <Link href="/intro-pages/Press">PRESS</Link>

        <Link href="/intro-pages/Services">PERSONAL STYLED SERVICES</Link>
      </nav>
      <Image alt="emily-logo" className="logo-header" src={logo} />
    </section>
  );
}

Here is the Page.tsx

import Container from "@/app/_components/container";
import { HeroPost } from "@/app/_components/hero-post";
// import { Intro } from "@/app/_components/intro";
import Intro from '@/app/_components/intro'
import {SocialMedia} from '@/app/_components/SocialMedia'
import {HomePage} from '@/app/_components/HomePage'
import {Testimonial} from '@/app/_components/Testimonial'
import { MoreStories } from "@/app/_components/more-stories";
import { getAllPosts } from "../lib/api";

export default function Index() {
  const allPosts = getAllPosts();

  const heroPost = allPosts[0];

  const morePosts = allPosts.slice(1);

  return (
    <main>
      <Container>
        <Intro />
        <HomePage/>
        <SocialMedia/>
        <Testimonial/>
        <HeroPost
          title={heroPost.title}
          coverImage={heroPost.coverImage}
          date={heroPost.date}
          author={heroPost.author}
          slug={heroPost.slug}
          excerpt={heroPost.excerpt}
        />
        {morePosts.length > 0 && <MoreStories posts={morePosts} />}
      </Container>
    </main>
  );
}

Here is the about.txs

export function About() {
  return (
    <section>
      <h1>This is the About Page</h1>
    </section>
  );
}

https://i.sstatic.net/sq7Kq.png

Above I show you where every component is located

Answer №1

If you're seeing a 404 error, it may be because your intro-pages are not in the correct folder structure. They should be inside the _components folder, which should be a direct subfolder of the app folder. To fix this issue, create a new folder called about and inside it, add a file named page.tsx.

For guidance on organizing your Next.js project, consult the official documentation at https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#pages

Following the documentation, additional pages should be created by adding a page.js file in a new folder (app/pagesYouCreated).

Here is how your folder structure should look:

./src
├── app
│   ├── _components 
│   ├── intro-pages
│   │   └── about
│   │       └── page.tsx

By organizing your folders this way, Next.js will be able to route your pages correctly. Moving the _components folder outside of the app folder, but still within src, will help with organization and maintenance of your project.

Make sure to follow Next.js conventions for folder structure to avoid routing errors. Adjusting your project as suggested should resolve the 404 error. Happy coding!

Answer №2

It appears that a slight restructuring of your /app directory is needed.

In order to access /intro_pages/about, your /app directory should be organized as follows:

/app
  /intro_pages
    /about
      page.tsx
    /blog
      page.tsx
    ...

For Next.Js to work properly, there should be a page.tsx file within the /about directory. Refer to their documentation here. Make sure to verify that you are looking at the App Router docs, not the Pages Router section.

Additionally, make sure to update your page's code to include a named export like this:

// ARROW FUNCTION
const aboutPage = () => {
  // insert your page code here
}

// OR TRADITIONAL FUNCTION

function aboutPage() {
  // insert your page code here
}

export default aboutPage;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bars of varying heights (Google Chart)

I have incorporated a Google chart within a mat-grid-tile. In this setup, one column displays a value of 50, while the other showcases a value of 30. These particular values are sourced from myService and are accurate. Although when I hover over the bars i ...

What is the best way to incorporate a TypeScript function within a JQuery function?

I'm facing an issue with installing the Angular Instascan library, so I am using it without installing and only importing the script. In order to make it work, I have to use JQuery in my TypeScript file within the component. Is there a way to call a T ...

Tips on creating type definitions for CSS modules in Parcel?

As someone who is brand new to Parcel, I have a question that may seem naive. In my project, I am using typescript, react, less, and parcel. I am encountering an error with typescript stating 'Cannot find module 'xxx' or its corresponding t ...

Retrieve and showcase information from Firebase using Angular

I need to showcase some data stored in firebase on my HTML page with a specific database structure. I want to present the years as a clickable array and upon selecting a year, retrieve the corresponding records in my code. Although I managed to display a ...

Transform a nested array of objects into a distinct set of objects based on the data in JavaScript or TypeScript

I have a unique situation where I am dealing with a double nested array of objects, and I need to restructure it into a specific array format to better align with my table structure. Here are the current objects I'm working with and the desired resul ...

Having trouble linking the date object with the default value of the date input field

Exploring how to set the default value of a date type input using property binding. Initially, I attempted to create a new date object in app.component.ts and then bind the [value] attribute of the date input to the currentDate property within app.compone ...

Sharing data between different Angular components that are not directly related can be achieved by utilizing a service in Angular

This is the service class for managing data export class DataService { public confirmationStatus = new Subject(); updateConfirmationStatus(status: boolean) { this.confirmationStatus.next(status); } getConfirmationStatus(): Observable<any&g ...

Encountering a Typescript error while attempting to utilize mongoose functions

An example of a User interface is shown below: import {Document} from "mongoose"; export interface IUser extends Document{ email: string; password: string; strategy: string; userId: string; isValidPassword(password: string): ...

How can I stop inputs from blurring when a user navigates away from a page?

I'm currently working on a project using next.js 13 and I'm looking to create interactive authentication forms. One of the features I've implemented is a form that displays the input label above it when focused, acting like a placeholder onc ...

Retrieve information from both the client and server sides within NextJS

Looking to integrate a third-party API for data fetching in certain components within my NextJS project. The goal is to have the server pre-render these components with the API for optimal SEO benefits, but also enable client-side fetching of component pro ...

Adjust the column count in mat-grid-list upon the initial loading of the component

My goal is to implement a mat-grid-list of images with a dynamic number of columns based on the screen size. Everything works perfectly except for one small glitch – when the grid first loads, it defaults to 3 columns regardless of the screen size until ...

Challenges with date formatting arise for Spanish speakers when the date returns as NaN or an Invalid

I have been working on an Angular app Objective: My aim is to allow users to input dates in Spanish format (DD/MM/YYYY) and display them as such, while converting them back to English format when saving the data to the Database. Issue: One problem I enco ...

Ways to display a variable within an HTML element

What could be causing variable a to be undefined? export default function Surah() { let a; const updateVariable = (id) => { a = id; console.log(a); }; return ( <div> <div> <button onClick={() => ...

Error Encountered with Google Authentication in Localhost:3001 - Warning of 'Blocked Access'

Encountering a Next-auth Google authentication issue on my localhost development setup. Admin side (localhost:3000) and client side (localhost:3001) of e-commerce website are separate instances. Error message "access blocked" when trying Google authentica ...

How can I remove a specific page from the file structure of my NextJS 13 app?

Here is my folder structure: app/view-all/layout.js app/view-all/page.js app/view-all/[itemId] app/view-all/clothes app/view-all/accessories app/view-all/bags All of these components need to utilize an Aside component that I have coded in app/view-all/lay ...

Tips for patiently anticipating the completed response within an interceptor

Using the interceptor, I want to display a loading spinner while waiting for subscriptions to complete. This approach works well in individual components by showing and hiding the spinner accordingly: ngOnInit() { this.spinnerService.show(); this. ...

Optimal method for importing libraries in Angular 2 with TypeScript

Currently experimenting with Angular2, but I find myself in need of using jQuery. After downloading the d.ts file, I referenced the definitions in each file using the following syntax: /// <reference path="../../../../typings/jquery/jquery.d.ts" /> ...

What is the Angular2 equivalent of the AngularJS $routeChangeStart event?

During our time working with AngularJS, we utilized the $routeChangeStart/End event in the $rootScope to monitor changes in the route object. What is the equivalent method for monitoring route changes in Angular2? How can we achieve the same functionality ...

How can we declare and showcase a generic object with an unspecified number and names of keys in React using TypeScript?

I am facing a challenge with objects that have a 'comments' field. While all the other fields in these different objects have the same types, the 'comment' field varies. I do not know the exact number or names of the keys that will be p ...

Encountering an Error when Integrating Pusher (real-time data library) with Next.js: PusherRequestError - Unexpected status code 400

I encountered an issue while trying to integrate Pusher into my Next.js application due to Vercel's restriction on websockets in their serverless functions. The error message I keep receiving after running the program with Pusher is: error - unhandled ...