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

Is it possible not to push unnecessary state when clicking an <a> element? #2531

Open
jiaoliao946 opened this issue Feb 7, 2025 · 0 comments

Comments

@jiaoliao946
Copy link

Feature request

Problem or desire

I click an element which has the href just like the location.href when setting routerMode to "history", and then it pushes a new state which is not necessary.
It might be better to use history.back() than history.pushState() when clicking an element which has the href just like the location.href of the previous page in session history.

Proposal

Do nothing when clicking an element which has the href just like the location.href.
Use history.back() when clicking an element which has the href just like the location.href of the previous page in session history.

Implementation

Just change the onchange function to something like this:

e.prototype.onchange = function (i) {
        void 0 === i &&
        (i = d),
        p(
          'click',
          function (e) {
            var n = 'A' === e.target.tagName ? e.target : e.target.parentNode;
            n &&
            'A' === n.tagName &&
            !g(n.href) &&
            (
              e.preventDefault(),
              n = n.href,
              n === location.href ? 1 :      //Add
              n === hrefPre ? window.history.back() :      //Add(hrefPre is used to record the href before)
              window.history.pushState({
                key: n
              }, '', n),
              i({
                event: e,
                source: 'navigate'
              })
            )
          }
        ),
        p('popstate', function (e) {
          i({
            event: e,
            source: 'history'
          })
        })
      }
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

No branches or pull requests

1 participant