Techiio-author
Started by Natasha JonesMay 7, 2022

Open
How can I fix the react-rounter-dom v6's url in GitHub pages without the use of HashRouter?

3 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWES SHARE

I am getting 404 whenever I input the url manually.

Please help solve this problem.

1 Replies

Techiio-commentatorDebarghya Das replied 3 months ago0 likes0 dislikes

Hello,

For those looking to just make their projects compatible with old URLs, you can force update any hash missing the leading "/". This is not the most elegant solution, but it solved it for me.

Add this to your index.js entry file:

const forceSlashAfterHash = () => {
    let _hash = window.location.hash;
    if (_hash[1] && _hash[1] != '/') {
        window.location.href = window.location.origin + window.location.pathname + window.location.search + "#/" + _hash.slice(1);
    }
}
forceSlashAfterHash();
window.addEventListener('hashchange', forceSlashAfterHash);

When the page loads or the hash changes, it checks for a leading "/" in the hash, and if not present, adds one.

I hope this will solve your problem...

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance