Skip to content

Manipulating the URL in the clicked link #959

Closed Answered by bozdevs
bozdevs asked this question in Help
Discussion options

You must be logged in to vote

Manipulating the URL in the clicked link

1- For SEO, we add the permalink directly to the link.

<a href="localhost/about">About</a>

2- To differentiate we add a data-preview attribute.

<a href="localhost/about" data-preview>About</a>

3- By taking advantage of Swup's hooks, we actually create the connection we want.

swup.hooks.on('link:click', (visit) => {
   const element = visit.trigger.el;
   if (document.contains(element)) {
      if (element.hasAttribute("data-preview") || element.closest("[data-preview]")) {
         var target_url = new URL(element.href),
                current_url = new URL(window.location.href);
         current_url.searchParams.set("preview", target_url.pathname);

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
7 replies
@daun
Comment options

daun Sep 26, 2024
Maintainer

@daun
Comment options

daun Sep 26, 2024
Maintainer

@bozdevs
Comment options

@bozdevs
Comment options

@daun
Comment options

daun Sep 26, 2024
Maintainer

Comment options

You must be logged in to vote
0 replies
Answer selected by daun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants