preact-router icon indicating copy to clipboard operation
preact-router copied to clipboard

How to override router on select anchors

Open CedarDCX opened this issue 7 years ago • 2 comments

In the readme, it states:

It also automatically wires up <a /> elements to the router.

Is there a way to override this behaviour? We have a link in our project that we want to force the page the load entirely from the server, rather than be processed by the router.

CedarDCX avatar Oct 16 '17 14:10 CedarDCX

Hi @CedarDCX, I found that you can override this behavior by using target="_blank", or using an absolute URL including the protocol. The check for this is performed here. It would be simple to turn this into a component as follows:

function absoluteUrl(uri) {
  const a = document.createElement('a');
  a.href = uri;
  return a.href;
}

const DirectLink = ({ href, ...props }) => (
  <a href={absoluteUrl(href)} {...props} />
);

I hope that helps!

pl12133 avatar Nov 13 '17 16:11 pl12133

You can add a native attribute to links to bypass preact-router's internal handling of them:

<a native href="/foo">click me to perform a full navigation</a>

developit avatar Nov 15 '17 03:11 developit