question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

trailingSlash + custom 404 + next export defaults to default 404 page.

See original GitHub issue

Bug report

Describe the bug

When using a trailing slash (e.g. /about/) and a custom 404 page, the custom 404 page does not appear, rather the default 404 page is used instead.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create and set trailingSlash: true in next.config.js
  2. Create a 404.js page in the /pagesfolder (pages/404.js)
  3. Run yarn next build and yarn next export
  4. Run yarn next dev
  5. Go to a page that does not exist and you see the defualt 404 page.

Reproduction Repo:

http://github.com/pateljoel/nextjs-issue-16528

Expected behavior

My own custom 404 page on every page that does not exist.

System information

  • OS: macOS
  • Browser Chrome
  • Version of Next.js: 9.5.2
  • Version of Node.js: v12.16.2

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
cleversprocketcommented, Jan 21, 2021

I was able to hack a workaround by including a /404 export in the path map and copying the file into the out dir root in a post build step.

// next.config.js

module.exports = {
  exportPathMap() {
    return {
      "/404": { page: "/404" }
    }
  },
  trailingSlash: true
}
// package.json

{
  "scripts": {
    "build": "next build && next export",
    "postbuild": "node ./post-build.js"
  }
}
// post-build.js

const {
  writeFile,
  readFile,
} = require("fs").promises;
const { join } = require("path");

const main = async () => {
  const file = await readFile(join(__dirname, "out/404/index.html"));

  await writeFile(join(__dirname, "out/404.html"), file);
};

main();
1reaction
stahlmanDesigncommented, Nov 2, 2020

I have this same problem. I am deploying to Digital Ocean’s new App service like Vercel’s Now, and I need to set

module.exports = {
  trailingSlash: true
}

so that /music renders pages/music.js instead of having to navigate to /music.html

But setting that also alters my custom 404.js which does require 404.html

So fixing the routes breaks my custom 404 page, and vice-versa.

I tried to create folders for each page such as /music, /about and insert index.js, but it still treats the files the same way as if they were music.js, about.js inside /pages.

Read more comments on GitHub >

github_iconTop Results From Across the Web

404 page is not showing in nextjs after export - Stack Overflow
I've added a custom 404.js file in /pages directory and it's working fine locally but when I run next export and put all...
Read more >
18 Making a custom 404 page in Next.js - YouTube
Chapter Eighteen : Making a custom 404 page in Next.js and 503 ... 404 page in Next.js | Redirect to 404 | trailingSlash...
Read more >
Configuring a custom error document - Amazon Simple ...
Configure your Amazon S3 bucket as a website by providing a custom error ... For example, when an HTTP 404 Not Found error...
Read more >
Making a custom 404 page in Next.js - Netlify
Why have a boring default error page when you can spruce it up? Next.js has a custom static 404 page built right into...
Read more >
Client-Side Routing In Next.js - Smashing Magazine
Each page is a default exported React component from the pages directory that can be used to define the most common route patterns....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found