Skip to content

hashchange doesn't fire for relative links #327

Open
@vpzomtrrfrt

Description

@vpzomtrrfrt

Linking with just a fragment will trigger a hashchange event, but it doesn't appear to work if the href also includes the path.

Sample code showing the problem:

import { h, Component } from "preact";

export default class HashTest extends Component {
	updateHash = () => {
		this.setState({hash: location.hash});
	};

	componentDidMount() {
		window.addEventListener("hashchange", this.updateHash);
		this.updateHash();
	}

	componentWillUnmount() {
		window.removeEventListener("hashchange", this.updateHash);
	}

	render({}, {hash}) {
		return <div>
			<h1>Hash Link Test</h1>
			<p><a href="#bare">Fragment link</a></p>
			<p><a href="/hashtest#relative">Relative link</a></p>
			<p>The hash is: {hash}</p>
		</div>;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions