Comments (7)
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.
// 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.
Hey, I can't reproduce. It should not do a full page navigation. Can you show some example code?
from next-routes.
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.
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.
@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.
@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)
- Click on link and URL update is not working HOT 1
- Complex { Link } from routes module doesn't work when coming from static pages, but does when navigated to first HOT 5
- next 9 with next-routes and public folder not working
- Route pushRouter makes app refresh
- Empty value for route
- Nextjs v9 - Parameter 'url' must be a string, not undefined HOT 1
- Is this library alive? HOT 22
- Refresh results in 404 page not found HOT 4
- Bug with getServerSideProps HOT 2
- Compatibility with getStaticPaths HOT 1
- Error using Link component "Warning: Function components cannot be given refs." HOT 1
- Failed to decode downloaded font
- slugs are not working with dynamic routes after upgrading to next 9.5 HOT 2
- assigning the index page to multiple routes leads to infinite reloading HOT 1
- Page Both Accessible From Route Url And NextPage Name HOT 1
- Next 10 HOT 2
- React 17.02, next js 11 HOT 2
- Migrating from next-routes to built-in Next.js routing? HOT 1
- vercel deployment with this lib
- Migration steps for nextjs inbuilt routing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-routes.