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}`)
  })
})

Related Query

More Query from same tag