Можно сделать примерно так:
server.jsconst {createServer} = require('http');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 4000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({dev});
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const [, category] = req.url.split(/(?<=^\/products\/+)([^\/]+)/);
if (!category)
handle(req, res);
else
app.render(req, res, '/catalog');
}).listen(port, err => {
if (err)
throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
pages/catalog.jsimport React from 'react';
export default class extends React.Component {
static getInitialProps({asPath}) {
const [, category] = asPath.split(/(?<=^\/products\/+)([^\/]+)/);
return {category};
}
render() {
return (
<div>
<h1>Catalog</h1>
<p>{this.props.category}</p>
</div>
);
}
}
И переходить по
<Link href='/catalog' as='/products/some-category'>
<a>Some category</a>
</Link>