swup / preload-plugin

A swup plugin for preloading pages to speed up navigationΒ πŸš€

Home Page:https://swup.js.org/plugins/preload-plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: External links are not being properly ignored on hover

hirasso opened this issue Β· comments

What did you expect? 🧐

If I am on a website https://example.com and I have an external link on that site, for example:

<a href="https://www.instagram.com/followme">@followme</a>

I'd expect preload plugin to ignore that link when I hover over it.

What actually happened? πŸ˜΅β€πŸ’«

Currently the plugin will attempt to preload this URL:

https://example.com/{{path/of/external/link}}

So based on the example above this: https://example.com/followme

I suspect the problem lies within Location.fromElement that is being used here:

({ url } = Location.fromElement(input));

That function doesn't check if the provided URL is actually an internal URL or not.

I'll investigate further.

Swup and plugin versions πŸ“

❯ npm list | grep swup
β”œβ”€β”€ @swup/a11y-plugin@4.5.0
β”œβ”€β”€ @swup/body-class-plugin@3.1.2
β”œβ”€β”€ @swup/debug-plugin@4.0.4
β”œβ”€β”€ @swup/fragment-plugin@0.3.7
β”œβ”€β”€ @swup/head-plugin@2.1.2
β”œβ”€β”€ @swup/preload-plugin@3.2.7
β”œβ”€β”€ @swup/progress-plugin@3.1.2
β”œβ”€β”€ @swup/scroll-plugin@3.3.1
β”œβ”€β”€ swup@4.5.1

What browsers are you seeing the problem on? 🧭

No response

Relevant log output πŸ€“

No response

URL to minimal reproduction πŸ”—

n/a

Checked all these? πŸ“š

Good catch, this should probably be href instead of url.

I've started a branch for functional tests some time ago in December. Time to finish that so we find these types of issues earlier or avoid them at all. It's already merged. Will need to add a new test for this specific bug.

Since the plugin defers to swup itself for checking whether to ignore a link or not, using href should be enough.

But is that actually really what we want? It seems like pretty unintuitive behavior of Location.url to me. How about we change the Location.url as proposed in this PR?

I'd say that's what we want. The url property is shorthand for path + search, which in a normal URL object also don't care about the origin. I'd suggest a new local getter on the location object and making sure we're using that throughout the codebase, e.g. if (!location.local) return;. Returning a full href from url in one case and a local path in another case would make things more confusing in my opinion.

class Location {
  get local() {
    return this.origin !== window.location.origin;
  }
}

The naming of the url property is a bit unfortunate, but if I remember correctly, we just haven't managed to find the perfect name for it when creating the Location abstraction. The intention was to have the path part including all query params.