Giter Club home page Giter Club logo

Comments (7)

fridays avatar fridays commented on May 18, 2024 6

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!

from next-routes.

khankuan avatar khankuan commented on May 18, 2024 1
// 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 :)

from next-routes.

fridays avatar fridays commented on May 18, 2024

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

from next-routes.

khankuan avatar khankuan commented on May 18, 2024

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.

from next-routes.

khankuan avatar khankuan commented on May 18, 2024

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 :)

from next-routes.

Rukeith avatar Rukeith commented on May 18, 2024

@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.

from next-routes.

rajendraarora16 avatar rajendraarora16 commented on May 18, 2024

@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
}

from next-routes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.