Search code examples
reactjsnext.jsserver-side-rendering

how can I use slug before patName in next.js?


I am using next.js and graphql now.

I need to set the workspace name in url.

localhost/[workspace slug]/memeber
localhost/[workspace slug]/admin

like that

Can you help me?


Solution

  • If you want to implement dynaming routing you can use Next.js built in routing system, for example with last version of next.js you can create a folder inside pages called workspace, inside workspace a folder called [id] which containes a file ex. [role].js.
    So a route like /workspace/1234/admin will be automatically matched by the server.

    Otherwise you can use a custom server like express and use something like this:

      server.get('/:workspaceSlug/admin', (req, res) => {
        const { workspaceSlug} = req.params
        return app.render(req, res, '/admin', {workspaceSlug})
      })
      server.get('/:workspaceSlug/member', (req, res) => {
        const { workspaceSlug} = req.params
        return app.render(req, res, '/member', {workspaceSlug})
      })