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.

next/link prefixing urls with / only on client side

See original GitHub issue

Bug report

Describe the bug

With the latest next.js version (10.0.3), I noticed a new warning in my console, without changing anything at the code. When using next/link a slash / get’s prefixed to the url on client side but not on server. I had no errors on 10.0.1.

To Reproduce

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

  1. Clone any next.js example repo (for ex.: using-router-app)
  2. Modify your index.js page to this:
import Link from "next/link";

export default function Home() {
  return (
    <div>
      <Link href={`test`}>
        <a>test</a>
      </Link>
    </div>
  );
}

  1. Check the console
react-dom.development.js:88 Warning: Prop `href` did not match. Server: "test" Client: "/test"
    in a (at pages/index.js:7)
    in Link (at pages/index.js:6)
    in div (at pages/index.js:5)
    in Home (created by App)
    in App
    in ErrorBoundary (created by ReactDevOverlay)
    in ReactDevOverlay (created by Container)
    in Container (created by AppContainer)
    in AppContainer
    in Root

Expected behavior

Prefix the url on client and server side, or don’t prefix it at all.

Screenshots

image

System information

  • OS: Windows 10
  • Browser Chrome
  • Version of Next.js: v10.0.3
  • Version of Node.js: v14.15.1
  • Deployment: yarn dev

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
sarangkartikey50commented, Dec 9, 2020

Fix proposal

Currently I have tried by making removing the trailing slash in the client side. Now at both Server and Client the href will be ‘about’ and ‘about’ respectively. Not sure whether correct or not. pull request

next-server/lib/router/router.ts

const resolvedHref =
      finalUrl.origin === base.origin
-        ? finalUrl.href.slice(finalUrl.origin.length)
+        ? finalUrl.href.slice(finalUrl.origin.length + 1)
        : finalUrl.href
3reactions
marnixhohcommented, Apr 25, 2021

I am experiencing a similar issue after adding i18n / Internationalized Routing to my next.config.js file. Note, that without adding that, this error does not occur. The i18n section of my next.config.js file:

i18n: {
    locales: ['en-US', 'nl'],
    defaultLocale: 'en-US',

    domains: [{
        domain: 'localhost',
        defaultLocale: 'nl',
        locales: ['en-US']
    }]
},

Below is the error I receive. Note that I am developing on http://localhost, but for some reason next.js is forcing https on the server:

Warning: Prop `href` did not match. Server: "https://localhost/" Client: "/"

Next version: 10.0.8

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - next/link prefixing urls with / only on client side -
3), I noticed a new warning in my console, without changing anything at the code. When using next/link a slash / get's prefixed...
Read more >
next/link
Enable client-side transitions between routes with the built-in Link component.
Read more >
Advanced Linking with the Next-Link
Client -side transitions between routes may be made easier with the Link component imported from the next/link module.
Read more >
Client-Side Routing In Next.js
The next/link API # · Routes With Dynamic Segments # · Use-cases For The passHref Prop # · URL Objects #.
Read more >
Next.js - Error: only absolute urls are supported
As the error states, you will have to use an absolute URL for the fetch you're making. I'm assuming it has something to...
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