nuxt-modules / algolia

🔎 Algolia module for Nuxt
https://algolia.nuxtjs.org/
MIT License
185 stars 32 forks source link

`baseURL` appended twice to the URL when clicking on search result #166

Closed Lehoczky closed 9 months ago

Lehoczky commented 11 months ago

Version

"@nuxtjs/algolia": "^1.9.0", "nuxt": "~3.6.5",

Reproduction Link

Since DocSearch is only granted for running documentation sites, I'm unfortunately unable to provide a minimal reproduction, but my site is open source. Let me know if you need any help.

Repo: https://github.com/Lehoczky/frontend-tools Running site: https://lehoczky.github.io/frontend-tools/

Steps to reproduce

What is Expected?

The user is being redirected to https://lehoczky.github.io/frontend-tools/frontend-tools/style-guide/#avoid-mental-mapping which shows the 404 page, because frontend-tools is in the URL twice

What is actually happening?

Navigate to https://lehoczky.github.io/frontend-tools/style-guide/#avoid-mental-mapping instead.

Baroshem commented 11 months ago

Hey,

I think that this issue can be caused by the fact that the url property of hit has already the frontend-tools.

Because of that the code here creates a redirect to the current location with added hit.url that in your case is probably frontend-tools/something.

Could you change the url so that it does not include the prefix frontend-tools? Then it should be working as expected (for example, nuxt.com uses this Docsearch that way).

Lehoczky commented 11 months ago

Hi Jakub,

Isn't the hit.url property right here? It is used as href on the dom and if you click the url in dev tools, it navigates you to the right page:

image

and it is also equivalent with what nuxt content generates for that h3 element:

image

NuxtLink components also use the base URL:

image

If I remove the whole base path like this:

 transformItems(items) {
  return items.map((item) => ({
        ...item,
        url: item.url.replace( 'https://lehoczky.github.io/frontend-tools',  ''),
   }))
},

Then clicking on a search result is working, but the href is essentially wrong, it should have frontend-tools in it. If I click on this link now in the devtools, it goes to the 404 page:

image

Baroshem commented 10 months ago

Isn't it maybe caused by the baseUrl property set in nuxt.config.ts? https://github.com/Lehoczky/frontend-tools/blob/5ca8ec84e2b23e5e004bb2bcc748d630a1b0a57a/nuxt.config.ts#L4

Maybe it is adding this /frontend-tools automatically to your url while the link in the DocSearch seems to work correctly.

Lehoczky commented 10 months ago

Yes, it's definitely caused by the baseUrl, and I think I found the responsible line of code: nuxt/src/pages/runtime/plugins/router.ts#L53

Because the useRouter composable is already using the baseUrl, if we router.push() anything, like we do here, in the component, that path will get appended to the base url. In my case we push frontend-tools/anything while the router is already using frontend-tools as a base and that's why we get frontent-tools/frontend-tools/anything as a result.