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

Fix page reloads on missing hrefs #28

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

Conversation

jozsefsallai
Copy link

Added a check for the href attribute to the anchor link click event listener.

Description

Since the package updates the event listener of all anchor links on the website, sometimes we might not want the behavior of the new event listener to run. In this specific example, if an anchor tag doesn't have a href attribute, the page would just reload. Sometimes the lack of href tags is intentional. For example, the hamburger menu in Buefy's navigation component looks like this:

<a role="button" aria-label="menu" class="navbar-burger burger"> ... </a>

In this scenario, the hamburger anchor opens the menu but then the page reloads. My change makes sure the href attribute actually exists before doing anything.

Related Issue

N/A

Screenshots or GIFs (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@obviyus
Copy link

obviyus commented Aug 1, 2022

@ukutaht if you have a moment, it would be great to have this fix merged.

@ionicsolutions
Copy link

ionicsolutions commented Feb 18, 2023

I ran into the exact same issue and would love for this to be merged so that we can enable outbound link tracking for our sites without breaking the mobile menu.

<a> tags without href are valid HTML but there is nothing to track there.

@jozsefsallai
Copy link
Author

I ran into the exact same issue and would love for this to be merged so that we can enable outbound link tracking for our sites without breaking the mobile menu.

<a> tags without href are valid HTML but there is nothing to track there.

@ionicsolutions As a temporary workaround if you use Node.js tooling, you can use patch-package to manually fix the issue. The files you will likely need to patch are:

  • node_modules/plausible-tracker/build/main/lib/tracker.js
  • node_modules/plausible-tracker/build/module/lib/tracker.js

Adding the following line to the beginning of the trackClick function should fix the issue:

if (!this.href) return;

@ionicsolutions
Copy link

Thanks, @jozsefsallai, I implemented the workaround you suggested and it works like a charm.

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.

3 participants