Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support hash router #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lexich
Copy link

@lexich lexich commented Nov 29, 2021

#3

index.js Outdated
let router = atom()
let isHashRouter = options && options.hash;

let locationPathname = isHashRouter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s rename variable

@ai
Copy link
Member

ai commented Nov 29, 2021

I am not sure that this is a right API.

Often, hash navigation is independent of route’s navigation. For instance, I use hash navigation for popups, which can be open on every page.

In your suggestion I need to manually describe all possible hashes in URL schemas which is not useful.

@lexich
Copy link
Author

lexich commented Nov 29, 2021

I am not sure that this is a right API.

Often, hash navigation is independent of route’s navigation. For instance, I use hash navigation for popups, which can be open on every page.

In your suggestion I need to manually describe all possible hashes in URL schemas which is not useful.

What API is preferable for you? Do you want to have the possibility of using both schemas?

@ai
Copy link
Member

ai commented Nov 29, 2021

What API is preferable for you? Do you want to have the possibility of using both schemas?

It is a question for you.

This task not just about writing a code, but about creating API (something like product manager work but for open-source library).

@mattmccray
Copy link

Well, this was exactly what I was looking for.

When the application is the resource the URL is locating, it makes sense to use hash routing as pointers to internal views (not to be confused with state).

To get it to recognize routes I did need to change lines 7-9 to:

  let getPathname = isHashRouter
    ? () => location.hash.replace(/^#/, '')
    : () => location.pathname

When it stripped out the leading slash, that lead to route recognization failure... Maybe because I defined my routes with the leading slash, now I think about it.

Anyway, thanks for this patch, it's been very useful for me!

@jordmantheman
Copy link

Another use case for hash routing is when your app is distributed via some object store like AWS S3. You can set your CDN to route myhost.com -> s3://some-bucket/index.html and SPA like routing works until you attempt to refresh, in which case the CDN will attempt to route myhost.com/subpage -> s3://some-bucket/subpage which results in a 404.

While there are more robust solutions to this problem like configuring the store to redirect 404s back to your index.html... hash routing is a simple solution as well.

Hash routing (myhost.com/#/somesubpage) prevents the hash request from hitting the server. React Router's hash router has been supporting this well.

While I'll admit I wouldn't want to use this solution for anything too serious, it's a quick and dirty way to host a webapp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants