question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Absolute URLs don't work with V8 anymore.

See original GitHub issue

Hi there! Have been using navigo for years with a lot of Sites I built with WordPress. I was initializing it with

const navigo = new Navigo(GLOBALS.homeUrl, false); 

GLOBALS.homeUrl being the URL that the WordPress Template Tag home_url(‘/’) would return. Later in my script I would navigate links manually:

...
navigo.navigate( url, true ); // the second parameter navigates using absolute URLs in v7, which are standard in WordPress
...

How would I do that with Navigo v8? I couldn’t find anything in the documentation. The current behavior is that the full URL is being appended to the current one when navigating a link.

Expected behavior GLOBALS.homeUrl: https://my-site.com/my-path/ Link URL: https://my-site.com/my-path/my-sub-path/ Result of navigate: https://my-site.com/my-path/my-sub-path/

Current behavior Result of navigate: https://my-site.com/my-path/https://my-site.com/my-path/my-sub-path/

See how the full URL gets appended?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
krasimircommented, Oct 13, 2021

Exactly! Good one.

0reactions
hirassocommented, Oct 13, 2021

Hi @krasimir , thanks for pointing out that in my use case I indeed didn’t need a fully fledged router like Navigo. This is what I went with:

// this triggers my internal application logic
const handleRoute = () => {
  const url = window.location.href;
  // ...do something with the current url. Here I reach out to the rest of my script
}
// add event handler for when using the browser UI to navigate back and forth
window.onpopstate = () => handleRoute()
// trigger the current route once on init
handleRoute()

// navigates to an URL, triggers the route
const navigate = ( url ) => {
  // bail early if the URL is already active
  if( url.split('#')[0] === window.location.href ) return false;
  // push the URL to the history
  window.history.pushState(null, '', url)
  // trigger the route manually
  this.handleRoute();
}
// adds an event listener for all internal links
document.documentElement.addEventListener('click', e => {
  const anchor = e.target.closest('a')
  if( !anchor  || !anchor.href || !anchor.matches('.link--internal') ) return;
  navigate(anchor.href)
})

Works great! And it’s so tiny 🤦‍♂️😄 …one less dependency 🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Absolute URLs in Table of Contents doesn't work anymore?
I have been using Table of Contents (TOC) macro on a page and then enabled "Absolute URLs" in settings. Absolute URLs is required...
Read more >
Fix list for IBM WebSphere Application Server V8.5
IBM WebSphere Application Server provides periodic fixes for the base and Network Deployment editions of release V8.5. The following is a complete listing ......
Read more >
Google: Doesn't Matter if You Use Absolute or Relative URLs ...
John Mueller says, for the most part, it doesn't matter whether you use absolute or relative URLs for internal links.
Read more >
The Best Cordless Stick Vacuum - The New York Times
If you want a cordless vacuum that can actually deep-clean your rugs, the Dyson V8 series–which includes the V8 Absolute, the V8, and...
Read more >
Absolute vs relative URLs - Stack Overflow
If by absolute URLs you mean URLs including scheme (e.g. HTTP / HTTPS) and the hostname (e.g. yourdomain.example ) don't ever do that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found