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.

The basePath is repeated twice in URL with query params

See original GitHub issue

Bug report

Describe the bug

The basePath is repeated twice in URL when we add query parameters. Say we have set basePath: '/base' in next.config.js, if we enter the /base?foo=bar, it changes to the /base/base?foo=bar.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Run the https://repl.it/@theshem/NextBasePathWithQueryParams
  2. Navigate to https://nextbasepathwithqueryparams.theshem.repl.co/base?foo=bar

Expected behavior

The URL should remain unchanged. I.e. /base?foo=bar.

Screenshots

System information

  • OS: macOS 10.14.5
  • Browser (if applies) Chrome 85
  • Version of Next.js: 9.5.3
  • Version of Node.js: 12.x

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jnvcommented, Oct 8, 2020

Well, since the PR with a fix didn’t get any attention so far, I have implemented an ugly workaround. Just call this hook in you index page and it should get the work done; I lose hash part of URL if also query string is present, but other than that it seems to get the job done:

import {useRouter} from 'next/router'

export function useBasepathFix(): void {
  const {basePath} = useRouter()
  if (typeof window === 'undefined') {
    return
  }
  if (window.location.pathname === basePath + basePath) {
    const fixedUrl = basePath + window.location.search + window.location.hash
    const newState = {
      ...window.history.state,
      as: fixedUrl,
    }
    window.history.replaceState(newState, undefined, fixedUrl)
  }
}

(Edit: Removed interpolation, replicates stored history state)

2reactions
indotek68commented, Jul 24, 2021

Has this been fixed? I’m not seeing it in the latest versions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Correct way to pass multiple values for same parameter name ...
Most applications use the first option you have shown: http://server/action?id=a&id=b .
Read more >
Query string contains repetitive parameters - Sitebulb
Query string contains repetitive parameters. This means that the URL in question contains a query string with the same parameter more than once....
Read more >
Spring WebClient Requests with Parameters - Baeldung
Learn how to reactively consume REST API endpoints with WebClient from Spring Webflux.
Read more >
ASP.NET Core Blazor routing and navigation - Microsoft Learn
Learn how to manage request routing in Blazor apps and how to use the Navigation Manager and NavLink component for navigation.
Read more >
Paths and Operations - Swagger
All paths are relative to basePath (see API Host and Base URL). ... Swagger supports operation parameters passed via path, query string, headers...
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