preactjs / preact-router

:earth_americas: URL router for Preact.
http://npm.im/preact-router
MIT License
1.01k stars 156 forks source link

Links missing a leading slash trigger a page reload #437

Open JoelLefkowitz opened 1 year ago

JoelLefkowitz commented 1 year ago

Links of the form <Link href="path" /> rather than <Link href="/path" /> that are missing the leading slash are routed but trigger a page reload. It's an easy mistake to make since the routing occurs as expected and a difficult problem to diagnose.

Possible solutions:

Example:

import Router, { Link } from "preact-router";
import { VNode, render } from "preact";

const Home = (): VNode => (
  <>
    <Link href="/about">/about</Link>
    <br />
    <Link href="about">about (matches but reloads)</Link>
  </>
);

const About = (): VNode => <Link href="/home">/home</Link>

render(
  <Router>
    <Home default path="/" />
    <About path="/about" />
  </Router>,
  document.getElementById("root") as HTMLDivElement
);