1

I use this code fore Dynamic Routes in my nextjs project it work fine on developer mode but not work when i published it on my server

...Object.fromEntries(fs.readdirSync('./public/assets/data/pages/').map(i => [`/ac/${i}`, { page: '/ac/[id]' } ]))

this code is in next.config.js file

if need this is the full code

const fs = require("fs");
const path = require("path");
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  distDir: "build",
  trailingSlash: true,
  sassOptions: {
    includePaths: [path.join(__dirname, "styles")],
  },
  exportPathMap: async function (
    defaultPathMap,
    { dev, dir, outDir, distDir, buildId }
  ) {
    return {
      "/": { page: "/" },
      "/ac/asrar_archived": { page: "/ac/[id]" },
      "/ac/eqbal": { page: "/ac/[id]" },
      "/ac/fum": { page: "/ac/[id]" },
      "/ac/hakimtoos": { page: "/ac/[id]" },
      
       ...Object.fromEntries(fs.readdirSync('./public/assets/data/pages/').map(i => [`/ac/${i}`, { page: '/ac/[id]' } ]))
    };
  },

the manual route part of this code work fine but the dynamic part not work when published

i search for this problem but nothing found

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Mohammad
  • 21
  • 3

1 Answers1

0

Possible duplicate of Dynamic routing in Next.js doesn't work (404 error).

Quoted from link:

"In order for your dynamic routes to work and if you're using SSG, you need to make use of getStaticPaths and getStaticProps functions to pre-render all the paths (and the content for each one as well)"

https://nextjs.org/docs/basic-features/data-fetching/get-static-paths

So instead of predefining your paths inside next.config.js you should export (and predefine your paths) getStaticPaths and getStaticProps inside your dynamic page file.

IAmdeGroot
  • 17
  • 2
  • HI this require that we need to know all the path we need to know in advance, what if 1)some [id] was not known in build time? 2) [id] was generated about build time? – Man Man Yu Jul 04 '23 at 07:43
  • Hi! If your site is static (delivered as is from dist/build folder) you can't use dynamics paths generated at runtime. If you want it to have truly dynamic paths where for example someone create product with id 1 and you navigate to that by /product/1 you need to deploy your next.js app in a node environment – IAmdeGroot Jul 07 '23 at 06:58