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.

Router Base Attribute does break hash links

See original GitHub issue

Version

@nuxt/content: 1.5.3 nuxt: 2.14.0

Reproduction Link

https://codesandbox.io/s/autumn-https-x3oxj?file=/nuxt.config.js

You need do change the demo browser window to the subfolder https://x3oxj.sse.codesandbox.io/test/

Steps to reproduce

Set base attribute inside nuxt.config.js

  router: {
    base: "/test"
  },

Go to the about page and click on the footnote. You will the URL changing to the hash but removing the current page about. This does also happening with the other types of hash routes on the same page, eg. headings or content links.

What is Expected?

Hash should be added to current page url.

What is actually happening?

Hash will remove the current Url and will be added to the base.

PS: On discord I made a video and gif of the problem, because I’m trying to deploy my nuxt-content page inside a subfolder.

Cheers Hannes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dumptydcommented, Nov 9, 2021

This https://github.com/nuxt/content/issues/376#issuecomment-702193217 almost worked for me, except for the images as @b1rdhous3 pointed out.

This is how I solved that part:

yarn add rehype-urls

In nuxt.config.js

const path = require('path');
const baseURL = '/blog/';

export default {
  content: {
    markdown: {
      rehypePlugins: [
        ['rehype-urls', function addBaseToImages(url) {
          if (!baseURL) return;
          // replace '/images/' with whatever directory you're keeping them in
          if (url.href && url.href.startsWith('/images/')) return path.join(baseURL, url.href);
        }]
      ]
    }
  }
};

Nuxt masks this issue by adding a redirect rule from / from base in dev env, so unless you build and test the generated bundle you won’t find this issue until it’s deployed to prod. Might be a good idea to not add those redirects, but that’s for upstream.

1reaction
Atinuxcommented, Oct 1, 2020

Hi there,

When using router.base, Nuxt adds a <base href="/test/">.

Sadly it looks like a known issue from the browser: https://rogerkeays.com/blog/using-base-href-with-anchors

You can use a Nuxt hook to remove the <base> tag, actually it fixes your issue but may introduces somes issues (you will have to check to make sure your websites works perfectly without it):

// nuxt.config.js
export default {
  hooks: {
    "vue-renderer:ssr:templateParams": function (params) {
      params.HEAD = params.HEAD.replace('<base href="/test/">', "");
    }
  }
}

Working sandbox: https://codesandbox.io/s/green-meadow-tquyq?file=/nuxt.config.js:838-1027

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js - How to remove hashbang #! from url? - Stack Overflow
To get rid of the hash, we can use the router's history mode, which leverages the history.pushState API to achieve URL navigation without...
Read more >
5 Remarkable React Router Features (Anchor Links, Query ...
Dynamic Solution (Hooks). A much better solution is to leverage the url property from the match object that each <Route /> has.
Read more >
Ultimate React Router v6 Guide
React Router is by far the most popular routing library in React and this article goes in depth on everything you need to...
Read more >
Hash in history mode router, breaks URL : r/vuejs - Reddit
Using Vue 2.6.14, Vue Router set to history mode, URL containing a hashtag "#" breaks dynamic path. const router = new VueRouter({ base…...
Read more >
Dealing with links in Next.js - LogRocket Blog
My first technique is wrapping the Link component. Every routing library has a similar component; it is used instead of the <a> tag....
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