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

Using href directly in Link #21

Closed
khankuan opened this issue Apr 15, 2017 · 7 comments · Fixed by #58
Closed

Using href directly in Link #21

khankuan opened this issue Apr 15, 2017 · 7 comments · Fixed by #58

Comments

@khankuan
Copy link

Hi, I have a url that is part of the data. I tried using the href value directly instead of params and route but it seems to be doing a full page navigation. Is it expected?

@fridays
Copy link
Owner

fridays commented Apr 15, 2017

Hey, I can't reproduce. It should not do a full page navigation. Can you show some example code?

@khankuan
Copy link
Author

// routes.js
const nextRoutes = require('next-routes')
const routes = module.exports = nextRoutes()

routes.add('index', '/')
routes.add('detail', '/detail/:id')

// pages/index.js
import { Link } from "../routes";
<div>
   <Link href="/detail/123"><a>Detail</a></Link>
</div>

Without add params and route to Link, it causes a full page reload. Also, the client displays a brief 404 before showing the new page again.

Cheers :)

@khankuan
Copy link
Author

I'm also curious if full urls are supported. For example, I might display an anchor tag on my application that contains the full url to a page on my site. Can i use that directly with Link and avoid a full page reload? Since I've got the full url, it would be harder to determine the route and params.

@fridays
Copy link
Owner

fridays commented Apr 15, 2017

When using only href, it's not handled by next-routes but passed to the original Link component, which expects a corresponding file in the pages folder (therefore the 404). To use the route defined in routes.js, do it like this:

<Link route='detail' params={{id: 123}}><a>Detail</a></Link>

Alternatively next.js lets you decorate the displayed URL in any way, but you need to tell it which page to render and pass the params. That's what next-routes normally does for you. You can do it manually like this:

<Link href='/detail?id=123' as='/this-can-be-anything'><a>Detail</a></Link>

Check out the docs here: https://github.com/zeit/next.js#with-link

Hope it helps!

@khankuan
Copy link
Author

Thanks for the detailed walk-through! I now understand better. However, I have a use case where a component would render a url link that goes to anywhere including external sites.

Example:
http://mysite.com/detail/123
http://google.com

Im wondering if I can supply the url value directly in the link component, be it with next routes or without. For the first case, I want to be able to navigate without a reload. In the second case, the site would navigate out externally.

Cheers :)

@Rukeith
Copy link

Rukeith commented Aug 25, 2018

@fridays
As your sample <Link href='/detail?id=123' as='/this-can-be-anything'><a>Detail</a></Link>
How could I get query id=123 at server ?
I found I use req.url will get value of as this-can-be-anything.

@rajendraarora16
Copy link

rajendraarora16 commented Nov 3, 2018

@Rukeith You can get the query by writing a piece of code in your pages:

static async getInitialProps({ query }) {
const _param = query && query.id;
// Here _param is your id by hitting the URL /detail/123
}

SeanDemps pushed a commit to SeanDemps/next-routes that referenced this issue Dec 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants