Skip to content

middleware.ts - rewriting /_next/data/ paths is producing incorrect output (nexturl.href) #58320

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

Closed
1 task done
ben-aggus-gateway opened this issue Nov 11, 2023 · 3 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js. stale The issue has not seen recent activity.

Comments

@ben-aggus-gateway
Copy link

ben-aggus-gateway commented Nov 11, 2023

Link to the code that reproduces this issue

https://github.com/ben-aggus-gateway/middlewarebug.git

To Reproduce

  1. Run the application locally. Since this is middleware, it logs the nexturl object to the console.
  2. In the local browser for the site, add a filepath that mimics a /_next/data path. This is a working example: http://localhost:3000/_next/data/TN22pxNPSlmN59JSJ1pFb/support/security.json
  3. Observe the console output. The 'href' key will show the mangled output. (path repeats, ends with .json.json) This is the result of the URL above:
    rewrite {
    href: 'https://www.mydomain.com/_next/data/TN22pxNPSlmN59JSJ1pFb/_next/data/TN22pxNPSlmN59JSJ1pFb/support/security.json.json',
    origin: 'https://www.mydomain.com',
    protocol: 'https:',
    username: '',
    password: '',
    host: 'www.mydomain.com',
    hostname: 'www.mydomain.com',
    port: '',
    pathname: '/_next/data/TN22pxNPSlmN59JSJ1pFb/support/security.json',
    search: '',
    searchParams: URLSearchParams {
    [Symbol(query)]: [],
    [Symbol(context)]: URL {
    [Symbol(context)]: URLContext {
    href: 'https://www.mydomain.com/_next/data/TN22pxNPSlmN59JSJ1pFb/support/security.json',
    protocol_end: 6,
    username_end: 8,
    host_start: 8,
    host_end: 28,
    pathname_start: 28,
    search_start: 4294967295,
    hash_start: 4294967295,
    port: 4294967295,
    scheme_type: 2
    }
    }
    },
    hash: ''
    }

Providing "normal" URLs like /test/path will not produce the issue.

I have enabled skipMiddlewareUrlNormalize: true, in nextjs.config

Current vs. Expected behavior

Current behavior:
nexturl.href= https://www.mydomain.com/_next/data/TN22pxNPSlmN59JSJ1pFb/_next/data/TN22pxNPSlmN59JSJ1pFb/support/security.json.json

(the path seems to repeat twice)

Expected behavior:
nexturl.href should = https://www.mydomain.com/_next/data/TN22pxNPSlmN59JSJ1pFb/support/security.json when that path is provided to the redirector (and a host has been provided like our case)

This code currently works in our production Next 13.4.19 environment and produces the expected behavior.

Verify canary release

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

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
Binaries:
  Node: 18.18.2
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.3-canary.2
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Middleware / Edge (API routes, runtime)

Additional context

We are running 2 next sites. The need to rewrite /_next/data URLs is due to rewriting pages from 1 site to the other for certain pages.

@ben-aggus-gateway ben-aggus-gateway added the bug Issue was opened via the bug report template. label Nov 11, 2023
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Nov 11, 2023
@ben-aggus-gateway
Copy link
Author

ben-aggus-gateway commented Nov 13, 2023

After digging through Next.js canary source, I believe the fault lies with the use of formatNextPathnameInfo:
packages/next/src/shared/lib/router/utils/format-next-pathname-info.ts

specifically:

if (info.buildId) {
    pathname = addPathSuffix(
      addPathPrefix(pathname, `/_next/data/${info.buildId}`),
      info.pathname === '/' ? 'index.json' : '.json'
    )
  }

I know something upstream is to blame, but this looks like the code that's producing the href output I'm seeing from nexturl.

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label May 11, 2025
@nextjs-bot
Copy link
Collaborator

This issue has been automatically closed due to two years of inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!

@nextjs-bot nextjs-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 18, 2025
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. Runtime Related to Node.js or Edge Runtime with Next.js. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

2 participants