visionmedia / page.js

Micro client-side router inspired by the Express router
http://visionmedia.github.com/page.js
7.67k stars 687 forks source link

hashbang and file:// protocol problem #492

Closed PaulMaly closed 5 years ago

PaulMaly commented 5 years ago

When onclick event fired, we've got situation then handler never handle navigation, because this condition is always true:

if (pageBase && orig === path) return;

How to fix that?

PaulMaly commented 5 years ago

Also, pathname includes absolute path to the file, but seems should be a relative path.

Probably, we also need to use pageBase instead of base here:

    if (path.indexOf(pageBase) === 0) {
      path = path.substr(base.length);
    }
kaisermann commented 5 years ago

I'm also needing to get hashbang and file:// working with regular <a href></a> links.

Does this makes sense?

if (pageBase && orig === path && (!isLocation || this._window.location.protocol !== 'file:')) {
  return;
}

(check for isLocation and the location.protocol)

matthewp commented 5 years ago

I'm not sure I understand the situation to be honest. If you could create an HTML file that I can drop into a browser with the file protocol I can dive into it.

kaisermann commented 5 years ago

Sure! Based on the basic example:

<!DOCTYPE html>
<html>
  <head>
    <title>page: hashbang + file: protocol issue</title>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/page.js/1.10.2/page.js"></script>
  </head>
  <body>
    <p></p>
    <ul>
      <li><a href="./">/</a></li>
      <li><a href="./about">/about</a></li>
    </ul>

    <script>
      page('/', index);
      page('/about', about);

      page.start({ hashbang: true });

      function index() {
        document.querySelector('p')
          .textContent = 'viewing index';
      }
      function about() {
        document.querySelector('p')
          .textContent = 'viewing about';
      }
    </script>
  </body>
</html>

With file protocol:

file

With http protocol:

http

PaulMaly commented 5 years ago

@kaisermann could you please provide a PR for your fix?

kaisermann commented 5 years ago

@PaulMaly Sure, no problem! However, I'm not sure if this is really the right way of fixing it (I didn't understand completely the pageBase && orig === path condition). @matthewp any thoughts?

PaulMaly commented 5 years ago

@kaisermann I don't know is that the best way or not, but seems it should work, so I think it's an appropriate solution anyway.

kaisermann commented 5 years ago

@matthewp I think this can be closed now :grin:

PaulMaly commented 5 years ago

Yep, thank you!