score:1
Accepted answer
you will need to use custom server, and render the "error" page when the id is not exists.
const express = require('express')
const next = require('next')
const port = parseint(process.env.port, 10) || 3000
const dev = process.env.node_env !== 'production'
const app = next({ dev })
const handle = app.getrequesthandler()
app.prepare().then(() => {
const server = express()
server.get('/:id', (req, res) => {
const page = is_id_exists? '/posts' : '/_error';
return app.render(req, res, page, { id: req.params.id })
})
server.all('*', (req, res) => {
return handle(req, res)
})
server.listen(port, err => {
if (err) throw err
console.log(`> ready on http://localhost:${port}`)
})
})
Source: stackoverflow.com
Related Query
- Axios, handle API fetch error with dynamic URL
- next js error when hard refresh page with dynamic query parameter
- Dynamic routing with multiple parameters in Next js
- Error if direct URL of react-router with params is passed to browser
- passing a dynamic url with React
- How do I create user-customizable dynamic url with api-generated content in NextJS+React?
- Reactjs react router dom useParams not working with dynamic url
- Call React component with dynamic URL parameters
- Next js dynamic route in production return 403 error but should return 404. What could be the problem?
- Next Js dynamic imports with no SSR not working
- React Router URL Throws Error With Parameter
- Next Js with react-xarrows gives error "ReferenceError: Element is not defined"
- I get internal server error while running next js app with styled component
- How to generate dynamic url with state in componentDidMount?
- Having issues with dynamic routes on Next 9
- Getting error in Next js with redux thunk TypeError: nextCallback is not a function
- React comes back with an unexpected token error when I do a dynamic destructuring of my object
- Next js error when linking to a dynamic page
- Dynamic views based on url ID with React Router V6
- Openlayers compilation error with React Next
- Getting 404 error if i am navigating to any URL other than homepage URL with react-router
- pass id through Link in next js with out effecting the url
- How to create dynamic url with hyphens in Next.js?
- Reactstrap Tooltip throws target could not be identified in the dom error with dynamic IDs
- Problem with generation of dynamic pages in Next.JS. Error 404 gets printed instead
- How to load a specific photo with dynamic URL with react.js
- getStaticProps error with Typescript, Next js
- React router 404 error when refreshing page with url params
- Material-UI CardMedia not loading dynamic image from URL but works with static image
- dynamic url for reactjs axios with a condition
More Query from same tag
- ReactJs : Nested State Not getting Updated
- What's the correct pattern to change parent state inside a child state in React?
- React/Webpack Css imports from node_modules are not loading correctly
- Make and call a function from components
- Derive keywords from search text
- Fill page with body and stick footer to bottom with flex (Tailwindcss, Nextjs)
- Using bootstrap classes with reactjs styled components
- Reactjs POST request through fetch
- How can I attach to a stateless component's ref in React?
- How I should use shouldComponentUpdate here to update parent state in React.js?
- React - JSX returned by a function doesn't get converted into a Component
- Call several dependant functions before render
- Traversing / Editing DOM in React JS
- Webpack scss & css modules react - unexpected token string
- Can you pass parameters to a React hook that takes in no parameters?
- Tracking renders in a React app?
- Weather data is not displaying
- What are the types of react-hook-form methods?
- Support for the experimental syntax 'classProperties' isn't currently enabled
- adding multiple items to Redux store
- Axios not showing informations on react page
- Redux-actions Jest testing
- Set the setState to the searched books array to made it back to the blank array in case of invalid query
- Ant design: Wrapping a menu item into a custom component
- How to dynamically access nested errors/touched on formik Field
- Why the other two object properties doesn't show up when I console log the object?
- react onsenui mocha test error "Error: Invalid state"
- Can I detect if my script is being processed by Webpack?
- Submit Button On React Form Doesnt Run Submit Function
- PropType is defined but prop is never used for arrow function with single argument