preact-router
preact-router copied to clipboard
How to override router on select anchors
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.
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!
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>