Skip to content

Memoization during permanentRedirect #79192

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

Open
mcAnastasiou opened this issue May 14, 2025 · 1 comment
Open

Memoization during permanentRedirect #79192

mcAnastasiou opened this issue May 14, 2025 · 1 comment
Labels
Metadata Related to Next.js' Metadata API.

Comments

@mcAnastasiou
Copy link

Link to the code that reproduces this issue

https://github.com/mcAnastasiou/nextjs-memoization-issue

To Reproduce

  1. Run the application
  2. Visit http://localhost:3000/1 or any odd number
    Should see
    --- Rendering ---
    --- Fetching data ---
    --- Generating metadata ---
    
  3. Visit
    Should see
     === Will redirect ===
     --- Rendering ---
     --- Fetching data ---
     --- Generating metadata ---
     --- Generating metadata ---
     --- Fetching data ---
    
    

Metadata is called twice. Second time is not during the same server request and cache is not working

Current vs. Expected behavior

There is intentionally a timeout in fetch data. During these 2 seconds monitor network responses. While waiting for the page to load, the type changes from document to document / Redirect. This is when metadata is being changed by permanentRedirect and is causing the second call of generateMetadata.

Expecting to either not call generateMetadata twice or have an indicator to understand that it is the second request and not call fetch data

This has a high impact in our servers as we are not doing a single cached fetched

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.4.0: Fri Apr 11 18:32:50 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6041
  Available memory (MB): 24576
  Available CPU cores: 12
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.22
  pnpm: 10.9.0
Relevant Packages:
  next: 15.2.3 // There is a newer version (15.3.2) available, upgrade recommended! 
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

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

Metadata

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

next start (local)

Additional context

I tested it also in the latest version and it happening there too

@github-actions github-actions bot added the Metadata Related to Next.js' Metadata API. label May 14, 2025
@gaojude
Copy link
Contributor

gaojude commented May 21, 2025

It's expected that generateMetadata is called twice here, because the server receives two requests (one for the even number, and the other for the odd number param after the redirect roundtrip).

generateMetadata function has access to params (see https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function), so you could shortcircuit the execution of generateMetadata by checking params in the same way as you did for the page component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Metadata Related to Next.js' Metadata API.
Projects
None yet
Development

No branches or pull requests

2 participants