Skip to content

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

Description

@hems
  • I have a route /social/:user_id and which renders a Blaze template which depends on FlowRouter.getParam('user_id').
  • I'm now on the process of making an alias to this route by using /:username instead of /social/:user_id

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions