2

I make a dynamic route like [results].js

import { useRouter } from 'next/router'

const Post = () => {
    const router = useRouter();
    const { results } = router.query;

    return <p>Post: {results}</p>
}

export default Post

and configure my next.confi.js

module.exports = {
    trailingSlash: true,
}

It's working well on dev mode but in production mode, I have the only problem with the dynamic page route after npm run export.When I browse htttps://my-domain/search/searchslog it's getting 404 error page.

3
  • Next.js uses a file base routing. What is your directory structure? Commented Mar 29, 2021 at 13:21
  • My directory structure is- search/[result].js Commented Mar 29, 2021 at 13:59
  • @TanjeebAhsan were you trying to deploy this to Cloudflare Pages? Commented Jul 15, 2022 at 13:12

1 Answer 1

0

next export will pre-render all pages and export your app as static HTML. For your dynamic route, you'll need to use getStaticPaths to let Next.js know which HTML pages to generate for that route.

// pages/search/[results].js

export async function getStaticPaths() {
    return {
        paths: [
            { params: { results: 'result1' } },
            { params: { results: 'result2' } }
        ],
        fallback: false
  };
}

The function above will generate /search/result1 and /search/result2 pages. Any other /serach/* page will result in a 404.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried something like below for search from <input className="text-field" type="text" value={this.state.userName} onChange={event => this.setState({userName: event.target.value})}/> <a href={'/search/'+this.state.userName} className="search-button"></a> There have some conditions on [result].js if the search key match with fetch API data.It's working on dev mode but I for production is show 404.
You can't really use a statically generated dynamic route page like that. All possible search routes (/search/*) need to be pre-rendered at build time for them not to 404. Try using the query param in the URL to pass the search term instead (/search?key=value).
I need a URL www.domain-name.com/search-any-keys something like that for the search result. What will be the component design for that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.