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

rewrites, trailingSlash: true and getStaticProps cause TypeError [ERR_INVALID_URL]: Invalid URL #43623

Closed
1 task done
jdddog opened this issue Dec 2, 2022 · 3 comments · Fixed by #43641
Closed
1 task done
Labels
Navigation Related to Next.js linking (e.g., <Link>) and navigation. Pages Router Related to Pages Router.

Comments

@jdddog
Copy link

jdddog commented Dec 2, 2022

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #148~18.04.1-Ubuntu SMP Mon Oct 24 20:41:14 UTC 2022
Binaries:
  Node: 16.16.0
  npm: 8.11.0
  Yarn: 1.22.5
  pnpm: N/A
Relevant packages:
  next: 13.0.6-canary.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

Data fetching (gS(S)P, getInitialProps), Routing (next/router, next/navigation, next/link)

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://github.com/jdddog/next-invalid-url-reproduction

To Reproduce

Run yarn dev

Navigate to: http://localhost:3000/country/ (you want to trigger a server render not client side navigation).

Then you should receive this error in the browser:

image

And this error in the terminal for the server:

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:372:5)
    at URL.onParseError (node:internal/url:553:9)
    at new URL (node:internal/url:629:5)
    at /home/user/next-invalid-url-reproduction/node_modules/next/dist/shared/lib/router/adapters.js:97:21
    at Object.useMemo (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5124:19)
    at Object.useMemo (/home/user/next-invalid-url-reproduction/node_modules/react/cjs/react.development.js:1650:21)
    at PathnameContextProviderAdapter (/home/user/next-invalid-url-reproduction/node_modules/next/dist/shared/lib/router/adapters.js:65:31)
    at renderWithHooks (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7) {
  input: '//',
  code: 'ERR_INVALID_URL'
}
error - TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:372:5)
    at URL.onParseError (node:internal/url:553:9)
    at new URL (node:internal/url:629:5)
    at /home/user/next-invalid-url-reproduction/node_modules/next/dist/shared/lib/router/adapters.js:97:21
    at Object.useMemo (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5124:19)
    at Object.useMemo (/home/user/next-invalid-url-reproduction/node_modules/react/cjs/react.development.js:1650:21)
    at PathnameContextProviderAdapter (/home/user/next-invalid-url-reproduction/node_modules/next/dist/shared/lib/router/adapters.js:65:31)
    at renderWithHooks (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (/home/user/next-invalid-url-reproduction/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7) {
  input: '//',
  code: 'ERR_INVALID_URL',
  page: '/'
}

Describe the Bug

This bug occurs when using rewrites, where a page, e.g. /country/ is rewritten to /, trailingSlash is true and getStaticProps is present on the index page (the page we are rewriting to).

When the user requests the source page using server navigation, e.g. /country/ the error TypeError [ERR_INVALID_URL]: Invalid URL is thrown.

The error occurs in PathnameContextProviderAdapter at new URL(router.asPath, 'http://f') because router.asPath is set to //: https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/router/adapters.tsx#L120

It might be related to this bug: #40549. I've used the workaround in that thread by setting source: "/country/{/}?" to fix the client side navigation (see that thread), however, if the source is set to source: "/country/" the error from this example still occurs.

Expected Behavior

When the user requests the source page using server navigation, e.g. /country/ no error should be thrown and the page rendered.

Which browser are you using? (if relevant)

Version 107.0.5304.87 (Official Build) (64-bit)

How are you deploying your application? (if relevant)

next dev

@jdddog jdddog added the bug Issue was opened via the bug report template. label Dec 2, 2022
@balazsorban44 balazsorban44 added Navigation Related to Next.js linking (e.g., <Link>) and navigation. Pages Router Related to Pages Router. kind: bug and removed bug Issue was opened via the bug report template. labels Dec 2, 2022
@balazsorban44
Copy link
Member

Thanks, seems to be a regression introduced in #41767, we will have a look!

ijjk pushed a commit that referenced this issue Dec 3, 2022
Fixes #43623

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: Wyatt Johnson <accounts+github@wyattjoh.ca>
@jdddog
Copy link
Author

jdddog commented Dec 4, 2022

Thanks, seems to be a regression introduced in #41767, we will have a look!

Thanks very much for fixing it @balazsorban44 and @wyattjoh!

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Navigation Related to Next.js linking (e.g., <Link>) and navigation. Pages Router Related to Pages Router.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants