MaksimL
MaksimL

Reputation: 314

Replace internal Wordpress links with Next.js Link Component

I am developing Next.js app using Wordpress as headless CMS + GraphQL. Everything is clear except for internal content links in articles which I get via WP API. Such links are plain <a></a> and trigger browser reloading. Is there any solution to replace them with Link Component?

Upvotes: 0

Views: 466

Answers (1)

hulufei
hulufei

Reputation: 767

I think you content should include in your Next.js app, then on content's parent element, you can bind a onClick handler to capture click on links and use router.push to simulate <Link> behaviours like:

<div onClick={e => {
  e.preventDefault();
  if (e.target.href) router.push(e.target.href);
}}>
  internal content with links
</div>

Upvotes: 2

Related Questions