Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Routing #929

Closed
Ehesp opened this issue Jan 30, 2017 · 3 comments
Closed

Dynamic Routing #929

Ehesp opened this issue Jan 30, 2017 · 3 comments

Comments

@Ehesp
Copy link
Contributor

Ehesp commented Jan 30, 2017

Hello,

I know this has been discussed in a few other places and also there is a example repo for express, however I'm struggling to get this working with the examples and help on slack:

I need two routes:

  • /notifications (pages/notifications/index.js)
  • /notifications/:id (pages/notifications/single.js)

My server (express) is setup to like so:

    server.get('/notifications/*', (req, res) => {
      return app.render(req, res, '/notifications/single', req.query);
    });

On the server getInitialProps I can grab the ID from ctx.req.params. However, I'm struggling to navigate to the route using Router.

  • The following changes the URL in my browser, but reloads the /notifications route.
Router.push(`/notifications?id=${id}`, `/notifications/${id}`);
  • The following hits a 404 page:
Router.push(`/notifications/${id}`);

Is there anyway to handle this?

Cheers

@arunoda
Copy link
Contributor

arunoda commented Jan 30, 2017

You server route should be something like this:

server.get('/notifications/:id', (req, res) => {
  const params = { id: req.params.id }
  return app.render(req, res, '/notifications/single', params);
});

And you client side Router.push code should be something like this:

Router.push(`/notifications/single?id=${id}`, `/notifications/${id}`);

@fridays
Copy link
Contributor

fridays commented Jan 30, 2017

Maybe next-routes can be helpful to you, you could use it like this:

routes.add('notification', '/notifications/:id', 'notifications/single')
routes.add('notifications', '/notifications', 'notifications/index')

Router.pushRoute('notification', {id: 1})
Router.pushRoute('notifications')

<Link route="notification" params={{id: 1}}> ... </Link>

@Ehesp
Copy link
Contributor Author

Ehesp commented Jan 30, 2017

@arunoda that makes sense cheers :) Works!

@Ehesp Ehesp closed this as completed Jan 30, 2017
@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants