Skip to content

Conversation

@rubixvi
Copy link
Contributor

@rubixvi rubixvi commented Dec 12, 2025

This appears to be a new error produced with the latest update of next.

Occurs when front end is using a simple component for example to send a user to the payloadcms backend. This throws a new error. This only occurs with links to the /admin page.

[20:11:25] ERROR: Failed to create URL object from URL: http://localhost:3000http://localhost:3000/admin, falling back to http://localhost:3000http://localhost:3000/admin
⨯ TypeError: Invalid URL
    at ignore-listed frames {
  code: 'ERR_INVALID_URL',
  input: 'http://localhost:3000http://localhost:3000/admin',
  digest: '1079054744'
}

node_modules\.pnpm\@payloadcms+next@3.68.3_@ty_5c4107aa334225014c648de0038e6397\node_modules\@payloadcms\next\src\utilities\initReq.ts (103:25) @ <anonymous> 101 | const { req: reqOverrides, ...optionsOverrides } = overrides || {} 102 | > 103 | const req = await createLocalReq( | ^ 104 | { 105 | req: { 106 | headers,

Fixes #14900

This appears to be a new error produced with the latest update of next.

Occurs when front end is using a simple <Link> component for example <Link href="/admin"> to send a user to the payloadcms backend. This throws a new error.

```
[20:11:25] ERROR: Failed to create URL object from URL: http://localhost:3000http://localhost:3000/admin, falling back to http://localhost:3000http://localhost:3000/admin
⨯ TypeError: Invalid URL
    at ignore-listed frames {
  code: 'ERR_INVALID_URL',
  input: 'http://localhost:3000http://localhost:3000/admin',
  digest: '1079054744'
}

node_modules\.pnpm\@payloadcms+next@3.68.3_@ty_5c4107aa334225014c648de0038e6397\node_modules\@payloadcms\next\src\utilities\initReq.ts (103:25) @ <anonymous> 101 | const { req: reqOverrides, ...optionsOverrides } = overrides || {} 102 | > 103 | const req = await createLocalReq( | ^ 104 | { 105 | req: { 106 | headers,
```
Copilot AI review requested due to automatic review settings December 12, 2025 09:46
@rubixvi rubixvi changed the title fix: Bug with basic link components in the latest Next and payload admin Dec 12, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where Next.js Link components pointing to /admin caused URL duplication errors (e.g., http://localhost:3000http://localhost:3000/admin). The issue stemmed from faulty conditional logic in URL construction that would use serverURL even when req.url was already set.

Key changes:

  • Refactored the URL construction logic to properly prioritize req.url over serverURL
  • Added trailing slash removal from serverURL to prevent malformed URLs
  • Converted ternary expression to explicit if-else blocks for clearer URL selection logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rubixvi rubixvi changed the title fix(core): correct precedence when building urlToUse in createLocalReq Dec 12, 2025
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@rubixvi rubixvi changed the title fix(next): correct precedence when building urlToUse in createLocalReq Dec 12, 2025
@jacobsfletch
Copy link
Member

jacobsfletch commented Dec 12, 2025

Hey @rubixvi, this does prevent the admin panel itself from crashing, but I was able to produce the following url when navigating to the list view:

http://localhost:3000/http://localhost:3000/admin/collections/users?limit=10

Clearly this is wrong...we must not be relying on it anywhere else. This was definitely introduced in #14869, but the problem is related to the url suffix, not necessary how the url is constructed. As the name implies, the suffix shouldn't include the server url in the first place.

Your fix passes because the incorrect suffix happens to start with a leading slash:

/http://localhost:3000/admin/collections/users?limit=10

This allows us to construct a valid url object, albeit with an incorrect path. The problem is that serverURL is included in the suffix here via currentRoute:

urlSuffix: `${currentRoute}${searchParams ? queryString : ''}`,

I also imagine your change might cause other issues too, for example req.url is no longer appended with the suffix.

@rubixvi
Copy link
Contributor Author

rubixvi commented Dec 12, 2025

@jacobsfletch I'll start a patch file and come back to this, just realise /dashboard and root /admin are affected at the moment.

I couldn't reproduce the error in list view.

@jacobsfletch
Copy link
Member

Got a fix queued up here: #14907

denolfe pushed a commit that referenced this pull request Dec 14, 2025
Fixes #14900. Supersedes
#14901.

Since #14869, loading the
admin panel with a `serverURL` crashes with the following error:

```
⨯ [TypeError: Invalid URL] {
  code: 'ERR_INVALID_URL',
  input: 'http://localhost:3000http://localhost:3000/admin',
  digest: '185251315'
}
[12:26:08] ERROR: Failed to create URL object from URL: http://localhost:3000http://localhost:3000/admin, falling back to http://localhost:3000http://localhost:3000/admin
```

This is because the suffix used to create a local req object was changed
to include the server URL. As the name implies, it should be a relative
path that appends onto the base URL.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants