Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextjs - Getting the referer as null sometimes when reading from the next headers #59301

Open
1 task done
rakeshneelarapu opened this issue Dec 5, 2023 · 12 comments
Open
1 task done
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@rakeshneelarapu
Copy link

Link to the code that reproduces this issue

https://github.com/rakeshneelarapu/nextjs-headers

To Reproduce

  1. checkout the project
  2. npm install
  3. npm run dev
  4. open the browser on localhost:3000
  5. check the referer is printed on the UI sometimes and upon refreshing the browser, we re getting the value as null consistently

Working Example
image

Not Working Example
image

Current vs. Expected behavior

Bug: Referer is coming as null when reading from the next/headers

Expected Behaviour: Referer should be fetched correctly when reading from the next/headers

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 18.7.0: Tue Jun 22 19:37:08 PDT 2021; root:xnu-4903.278.70~1/RELEASE_X86_64
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.3
  eslint-config-next: 14.0.3
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router, Routing (next/router, next/navigation, next/link)

Additional context

Sometimes when we hot reload the server or make any changes in the component files, it works fine, but most of the times, we see this issue of null

@rakeshneelarapu rakeshneelarapu added the bug Issue was opened via the bug report template. label Dec 5, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Dec 5, 2023
@AdarshKonchady

This comment has been minimized.

@memeteli

This comment has been minimized.

@frankie-ug
Copy link

Currently facing the same issue. A workaround might be to use nextjs middleware

import { NextResponse } from 'next/server';

export function middleware(request: Request) {
  const requestHeaders = new Headers(request.headers);
  requestHeaders.set('my-url', request.url);

  return NextResponse.next({
    request: {
      headers: requestHeaders,
    },
  });
}

Then you can do:
const myUrl = headersList.get('my-url');

@desmati
Copy link

desmati commented Apr 14, 2024

At times, headers() returns no headers whatsoever. This problem isn't limited to just the referer header; I'm experiencing it with other headers like next-url as well.

Even using middleware hasn't resolved the issue; I'm consistently receiving empty headers in certain instances.

@erkannebil

This comment has been minimized.

@TomasSestak
Copy link

can confirm this

@carlpaten

This comment has been minimized.

@SimRunBot
Copy link

for me referals from twitter are null in the headers.get("referer") object

however i suspect it is because of twitters handling of outgoing links

@contigen
Copy link

Getting the referer header in Next.js layout returns null for subsequent requests/renders, is there a fix? @leerob

@GeekMikle
Copy link

I´ve been able to reproduce this issue on my end too. Facing the same error.

@MantasJa
Copy link

having the same problem in Nextjs middleware, I get null all the time.

@IsraExe

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests