I want to deploy my next js application in cpanel but on deploying the dynamic routes are not working any solution or suggestion would be great and also not working in npm build version as well
I tried in vercel it's working fine
I want to deploy my next js application in cpanel but on deploying the dynamic routes are not working any solution or suggestion would be great and also not working in npm build version as well
I tried in vercel it's working fine
You need to add a custom server to do that.
A sample code can be like this:
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 3000
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer(async (req, res) => {
try {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})
Also, you can be able to get a full demo of how to create custom server at NextJS documentation.
One more thing: The latest version of NextJS is not well-suited with Cpanel. I won't suggest you to deploy your NextJS to Cpanel.
So, one thing you can do is: You can deploy your app to vercel and change the nameservers.