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.

Build error when slug ends with `.html` for dynamic routes

See original GitHub issue

Bug report

I have an old site which I’m about to migrate to Next.js. I want to generate static pages. I have to support URLs such as: /blog/article-slug.html (this is the canonical URL that users and search engines see). I found two ways to build such URLs but neither of them seem to work properly.

Describe the bug

Option 1

I create a page component such as pages/blog/[slug].html.js and in getStaticPaths I provide the slug without the .html extension.

export const getStaticPaths = async () => {
    return { fallback: false, paths: [{ params: { slug: 'article-slug' } }] };
};

This options does not work at all. I DEV mode I only get a 404 page if I try to access http://localhost:3000/blog/article-slug.html. When running next build I get

Error: getStaticPaths can only be used with dynamic pages, not ‘/articles/[slug].html’. This means that this page is considered to have a static path and not be a dynamic.

Option 2

I create a page component such as pages/blog/[slug].js and in getStaticPaths I provide the slug with the .html extension.

export const getStaticPaths = async () => {
    return { fallback: false, paths: [{ params: { slug: 'article-slug.html' } }] };
};

This option at least works in DEV mode. But when running next build I get the following error:

> Build error occurred
[Error: ENOENT: no such file or directory, rename '/tmp/next/.next/export/blog/article-slug.html.html' -> '/tmp/next/.next/server/static/78zmPfx5_aE06CmpsBKfD/pages/blog/article-slug.html.html'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'rename',
  path: '/tmp/next/.next/export/blog/article-slug.html.html',
  dest: '/tmp/next/.next/server/static/78zmPfx5_aE06CmpsBKfD/pages/blog/article-slug.html.html'
}

To Reproduce

I created a repo to reproduce these errors:
https://github.com/bdadam/next.js-error-when-.html-in-path

Expected behavior

I would expect that next build generates a file such as .next/export/blog/article-slug.html.html.

System information

  • OS: Linux, Ubuntu 19.10
  • Version of Next.js: 9.3.5
  • Version of Node.js: 13.6.0

Other considerations

I can only reproduce this behavior if the slug ends with .html a . itself in the slug does not seem to cause any problems e.g. article....slug.xhtml.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
lionelpolanskicommented, Oct 1, 2020

I think this could be solved by the new “rewrite” function, though you’ll need a new version of Nextjs. Example:

module.exports = {
    async rewrites() {
        return [
            {
                source: "/:slug*.html",  // Old url with .html
                destination: "/:slug*", // Redirect without .html
            },
        ];
    },   
}
1reaction
dipankarmaikapcommented, Apr 26, 2021

I’m waiting for this issue fixed but until that I’m using the rewrite option. next.config.js

module.exports = {
  async rewrites() {
    return [
      {
        source: "/:slug*.html", // Old url with .html
        destination: "/:slug*", // Redirect without .html
      },
    ];
  },
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS build error when slug ends with .html for dynamic routes
I have been working on converting a massive WordPress site (20K posts) to NextJS. Every post on this old site has .html in...
Read more >
Dynamic Routes - Next.js
Dynamic Routes are pages that allow you to add custom params to your URLs. Start creating Dynamic Routes and learn more here.
Read more >
Sveltekit [...slug] dynamic routing returns 404 error after ...
After deploying build using firebase, except base routes all other routes returning 404 error. Here I have attached my code structure. svelte ...
Read more >
URLs & Dynamic Routing - Ghost Themes
Build dedicated URL structures using Ghost's dynamic routing system, for custom homepages, podcasts, categories and more.
Read more >
NextJS Project Tutorial - Dynamic Routing, Route Segment, Slug
Hi Everyone,In this NextJS Project Tutorial Video We will be discussing dynamic route, different route segment and slug.
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