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

query/search support? #109

Closed
frozenfung opened this issue Aug 29, 2017 · 2 comments
Closed

query/search support? #109

frozenfung opened this issue Aug 29, 2017 · 2 comments

Comments

@frozenfung
Copy link

frozenfung commented Aug 29, 2017

Hey, team. Really appreciate you guys work on this project.

Without it I can't migrate my existing Rails app into a SPA base on nodeJS. Or I need to try to figure out what the hell react-router is going on, which I think is too complete and brilliant to me, my app is a tiny one.

Now I face a situation that I need to access query/search in the route object.

For example, I have an endpoint http://www.example.com/?foo=bar. Owing to the different query foo=bar or baz=quz I have to set different state into my template. Is it possible we have the parsed query as a default params in context object just like params?

Right now I use this library for parsing in client side, and access with req.query in server side. But I think it maybe cool if we pre-parsed it.

I can come with a PR if you buy this one.

thanks

@frenzzy
Copy link
Member

frenzzy commented Aug 29, 2017

import Router from 'universal-router'
import queryString from 'query-string'

const router = new UniversalRouter({
  path: '/user/:username',
  action(context) {
    console.log(context.path)   // => '/user/jhon'
    console.log(context.params) // => { username: 'jhon' }
    console.log(context.query)  // => { query: 'and', hash: '' }
    console.log(context.hash)   // => '#example'
    return /* ... */
  }
})

// location = 'http://example.com/user/jhon?query=and&hash#example'
router.resolve({
  path: location.pathname,
  query: queryString.parse(location.search), // <=== pass query params here
  hash: location.hash
}).then(render)

Demo: https://jsfiddle.net/frenzzy/g7ymd7ho/

@frenzzy frenzzy mentioned this issue Aug 29, 2017
20 tasks
@frozenfung
Copy link
Author

@frenzzy Thanks for the demo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants