brohlson / gatsby-plugin-anchor-links

⚓ Gatsby plugin for using smooth scroll anchor links with a Gatsby Link component
https://www.npmjs.com/package/gatsby-plugin-anchor-links
MIT License
68 stars 22 forks source link

Comparison Shouldn't Fail When 'To' Contains a Query String / Search #46

Open Undistraction opened 1 year ago

Undistraction commented 1 year ago

The comparison between window.location.pathname will always fail if the the to contains a query string, for example if the to is: /example/?id=1234#foo, the comparison will be between pathname which is only /example/ and everything before the hash /example/?id=1234, which will fail.

Code here

if (window.location.pathname === withPrefix(anchorPath)) {
Undistraction commented 1 year ago

@brohlson would you accept an PR for this. It's a simple fix:

const { pathname, search } = window.location
const pathnameWithSearch = `${pathname}${search}`

if (pathnameWithSearch === withPrefix(anchorPath)) { 
  ...
}