最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Catch-all route with a default static page In Next.js - Stack Overflow

programmeradmin3浏览0评论

I have a catch-all route on which i wish to render different pages depending on the slugs - this is not hard to achieve. The following is my structure:


app/  
├── (home)/  
│   ├── layout.tsx  
│   └── page.tsx  
└── [...slug]/
    ├── layout.tsx
    └── page.tsx
├── layout.tsx  
└── page.tsx

Now my question is, how can I - or can I - have a home page on the root URL '/' and all the other routes handled by the [...slug] catcher? The reason I am asking is because during `next build`:


Error occurred prerendering page "/". Read more: []()

Error: The provided export path '/' doesn't match the '/\[...slug\]' page.

Read more: []()

at getParams (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\helpers\\get-params.js:27:15)

at exportPageImpl (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:108:43)

at C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:350:108

at turborepoTraceAccess (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\build\\turborepo-access-trace\\helpers.js:36:51)

at C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:350:103

at Span.traceAsyncFn (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\trace\\trace.js:157:26)

at exportPage (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:350:39)

at exportPageWithRetry (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:239:21)

at C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:321:72

at [Array.map]() (<anonymous>)

Export encountered an error on /\[...slug\]/page: /, exiting the build.

⨯ Static worker exited with code: 1 and signal: null

error Command failed with exit code 1  

(It might seem weird, but the setup was working, for some reason, it dropped again, and now I'm looking for a more proper solution)

My Next version is: 15.0.1

I have a catch-all route on which i wish to render different pages depending on the slugs - this is not hard to achieve. The following is my structure:


app/  
├── (home)/  
│   ├── layout.tsx  
│   └── page.tsx  
└── [...slug]/
    ├── layout.tsx
    └── page.tsx
├── layout.tsx  
└── page.tsx

Now my question is, how can I - or can I - have a home page on the root URL '/' and all the other routes handled by the [...slug] catcher? The reason I am asking is because during `next build`:


Error occurred prerendering page "/". Read more: [https://nextjs./docs/messages/prerender-error](https://nextjs./docs/messages/prerender-error)

Error: The provided export path '/' doesn't match the '/\[...slug\]' page.

Read more: [https://nextjs./docs/messages/export-path-mismatch](https://nextjs./docs/messages/export-path-mismatch)

at getParams (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\helpers\\get-params.js:27:15)

at exportPageImpl (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:108:43)

at C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:350:108

at turborepoTraceAccess (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\build\\turborepo-access-trace\\helpers.js:36:51)

at C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:350:103

at Span.traceAsyncFn (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\trace\\trace.js:157:26)

at exportPage (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:350:39)

at exportPageWithRetry (C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:239:21)

at C:\\git\\adinsure-docs-fumadocs-portal\\node\_modules\\next\\dist\\export\\worker.js:321:72

at [Array.map](http://Array.map) (<anonymous>)

Export encountered an error on /\[...slug\]/page: /, exiting the build.

⨯ Static worker exited with code: 1 and signal: null

error Command failed with exit code 1  

(It might seem weird, but the setup was working, for some reason, it dropped again, and now I'm looking for a more proper solution)

My Next version is: 15.0.1

Share Improve this question asked Mar 27 at 22:55 Vanja StojanovićVanja Stojanović 14710 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So it happened that I got an answer.

In my [...slug]/page.tsx I just filtered out the root path before returning all the pages:

export async function generateStaticParams() {
  const pages = await getPages();
  return pages
    .filter(page => page.path !== '/') // Exclude root path
    .map(page => ({
      slug: page.path.split('/').filter(Boolean)
    }));
}

and (the thing I think actually helped) was turning off trailing slashes in the next.config.mjs:

/** @type {import('next').NextConfig} */
const config = {
  output: 'standalone',
  reactStrictMode: true,
  ...,
  trailingSlash: false,
};

发布评论

评论列表(0)

  1. 暂无评论