When dealing with URLs, it's important to note that the hash symbol portion (which identifies an HTML entity) is not transmitted to the server. This means that you won't be able to match the URL serverside if a browser loads something like /route#modal1
- the server will only see /route
. However, there are ways to work around this:
Option 1: Manage modal rendering on the client side using next/router
in your component. Here's a sample code snippet assuming you're working with class components:
import Router from 'next/router'
....
componentDidMount(){
let id = Router.asPath.match(/#([a-z0-9]+)/gi )
if(id){
// show the modal
}else{
// do something else
}
}
Option 2: Pass the ID to the URL without the #
. In your server.js
, add a route similar to the following:
server.get('/route/:id?', (req, res) => {
let {id} = req.params
return app.render(req, res, '/mypage', { id })
})
Then extract the ID, for example in getInitialProps
:
static async getInitialProps (context) {
let ctx = context.query;
return ctx
}
You can then handle the modal accordingly:
componentDidMount(){
let {id} = this.props
if(id){
// show the modal
}else{
// do something else
}
}