kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 193 forks source link

Render route without changing url or making a route "alias" or set "hidden" param ? #728

Open hems opened 6 years ago

hems commented 6 years ago

The problem here is that when I receive the username via URL, for instance mysite.com/britneyspears i have to do an _id lookup to find the user_id based on username

Once i fetch the user_id i would like to render my old route, /social/:user_id by doing something similar to:

FlowRouter.withReplaceState( => { FlowRouter.go( '/social/' + _id ) } )

But without changing the URL, this way the username only works as a "mask" to the end-user but the rest of my application can rely on

FlowRouter.getParam('user_id')

so it's actually the username "mask" is transparent for the client and for the backend side.

Other option would be to accept "username" instead of user_id in most places, but that would require the whole application to have some sort of "findIdByUsername" in a lot of places which would increase the number of queries my application do.

Another solution i tried was to set user_id param

Router.route( '/:username/', { action: =>
  Meteor.call( "find_id_by_username", username, ( error, _id ) => 
    FlowRouter.setParam( {user_id: _id })
    BlazeLayout.render 'layout', 'private' : 'social_user_id'
  )
})

But this won't work, because user_id param doesn't exist on the route FlowRouter doesn't allow me to set it! One solution would be to do something like:

...
    FlowRouter.set__Hidden__Param( {user_id: _id })
...

and then render, my template wouldn't even know what's happening!

    BlazeLayout.render 'layout', 'private' : 'social_user_id'

And if i add the 'user_id' to the route, for instance:

Router.route( '/:username/:user_id?', { action: =>
  Meteor.call( "find_id_by_username", username, ( error, _id ) => 
    FlowRouter.setParam( {user_id: my_user_id })
    BlazeLayout.render 'layout', 'private' : 'social_user_id'
  )
})

The route will change to /username/mongo_db_id which is very unfriendly to the user and won't allow me to do user-friendly routes like /:username/followers and so on.

What am I doing wrong? Would be much simpler for my application if I could just make the user_id work transparently so I won't have to deal with username everywhere.

hems commented 6 years ago

One solution I found was to render BlazeLayout and sending user_id through the data parameter and then getting it on the layout through @data.user_id