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

[NEXT-601] search params object is empty in production with next 13 app dir #43077

Closed
1 task done
raymclee opened this issue Nov 18, 2022 · 56 comments · Fixed by #46205
Closed
1 task done

[NEXT-601] search params object is empty in production with next 13 app dir #43077

raymclee opened this issue Nov 18, 2022 · 56 comments · Fixed by #46205
Assignees
Labels
area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@raymclee
Copy link

raymclee commented Nov 18, 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: #1 SMP Fri Apr 2 22:23:49 UTC 2021
Binaries:
Node: 18.8.0
npm: 8.19.2se
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.0.4
eslint-config-next: 13.0.4
react: 18.2.0
react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

search params object is empty in production with next 13 app dir
Its ok if its a dynamic route

Expected Behavior

search params object is not empty in production

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

https://stackblitz.com/edit/nextjs-ncgro9?file=app/page.tsx

To Reproduce

npm run build && npm run start
go to /?page=1
go to /products/1?page=1

From SyncLinear.com | NEXT-601

@raymclee raymclee added the bug Issue was opened via the bug report template. label Nov 18, 2022
@balazsorban44 balazsorban44 added kind: bug Navigation Related to Next.js linking (e.g., <Link>) and navigation. area: app App directory (appDir: true) and removed bug Issue was opened via the bug report template. labels Nov 18, 2022
@Deploydon
Copy link

Strange,

Your link does seem to have valid search params when running.

However my app on a newer canary version still seems to have the same issue of empty searchParams when running on Vercel.

@iammatthias
Copy link

Ran into this on Next 13.0.5 last night. Works fine locally, but returns undefined once it is published on Vercel.

@neoliao
Copy link

neoliao commented Dec 6, 2022

Solution. In dev mode searchParams was given, but not in build mode. If you use a client component you need to use the useSearchParams hook: https://beta.nextjs.org/docs/api-reference/use-search-params

Also be aware of the getter-API, you can't access searchParams directly.

@hyperbolictonic
Copy link

Can confirm the same issue. Works fine if I "run dev" but returns empty after "run build" then "run start"

@baronha
Copy link

baronha commented Dec 20, 2022

same +1

@llorenspujol
Copy link

Same, it does not work when deployed on Vercel...

@abakan26
Copy link

Search params object is empty after running commands next build, next start

@DrMisQ
Copy link

DrMisQ commented Dec 24, 2022

any solution for this problem !!

@Waqas-Abbasi
Copy link

Waqas-Abbasi commented Dec 26, 2022

With nextjs version 13.0.6 searchParams seem to work as expected. The versions after seem to have this bug

@LIMPIX31
Copy link

Same thing. Any solution other than rolling back the version.

@yondifon
Copy link

+1.

@Hanskrogh
Copy link

Hanskrogh commented Jan 2, 2023

Can confirm that it is not only a problem with"app" dir, the issue also happens in "pages" dir. But only upon navigation to index path: (href="/").

@MahmoudAlawdh
Copy link

+1

1 similar comment
@abakan26
Copy link

+1

@simmondsty
Copy link

A similar issue is occurring with layout files, when calling an async function, the call is made at build time and then serves the same response on each call. Do not have this issue when dealing with dev.

@Jose-DallAsta-008
Copy link

can confirm the error is happening, can also confirm that the version 13.0.6 is also having this issue in the reproduction link (opposing Waqas-Abbasi comment)
+1

@hyperbolictonic
Copy link

@ijjk is there any update on a fix for this?

@simmondsty
Copy link

@ijjk This is a major issue, breaking essential functionality so I would imagine so.

@alexis78i
Copy link

I noticed that on some pages everything works for me, but on others (mostly where 4-5 requests to the API) it does not work.

@leerob leerob added the linear: next Confirmed issue that is tracked by the Next.js team. label Jan 14, 2023
@leerob
Copy link
Member

leerob commented Jan 14, 2023

Thanks for flagging this folks, we will investigate 🙏

@abakan26
Copy link

Will the issue be resolved in the next update?

@barel31

This comment was marked as spam.

@Trugamr
Copy link

Trugamr commented Jan 16, 2023

You may have to force page to be dynamic to get query params.

This can be done by exporting variable named dynamic and it's value as force-dynamic

export const dynamic='force-dynamic';

export default function Home(props: unknown) {
  return (
    <main>
      <pre>
        {JSON.stringify(props, null, 2)}
      </pre>
    </main>
  )
}

Originally posted by @hubertlepicki in #44005 (comment)

@benkingcode
Copy link

Why would you ever want non dynamic props? Isn't that the point of passing them

Pagination, for one example? If you have a URL with ?page=2 all of a sudden this means it's not cacheable? Doesn't make sense to me why query params must force dynamic rendering.

@typeofweb
Copy link
Contributor

The ability to specify static searchParams along with params would be great.

@hyperbolictonic
Copy link

Why would you ever want non dynamic props? Isn't that the point of passing them

Pagination, for one example? If you have a URL with ?page=2 all of a sudden this means it's not cacheable? Doesn't make sense to me why query params must force dynamic rendering.

Exactly this. Our users share their searches with each other frequently, having cached versions of those pages takes a load off the server.

@simmondsty
Copy link

Why would you ever want non dynamic props? Isn't that the point of passing them

Pagination, for one example? If you have a URL with ?page=2 all of a sudden this means it's not cacheable? Doesn't make sense to me why query params must force dynamic rendering.

Exactly this. Our users share their searches with each other frequently, having cached versions of those pages takes a load off the server.

I don't think either of you understand what the issue is here, searchParams are empty if you do not force-dynamic, so your page param will never reach your route. Of course for data loading you don't always want it to be dynamic...

@iamkibeh
Copy link

iamkibeh commented Mar 6, 2023

When will they solve this! Should be fixed asap

@parixscm
Copy link

parixscm commented Mar 7, 2023

Also having same issue.. !

timneutkens added a commit that referenced this issue Mar 16, 2023
Ensures that using `searchParams` opts into dynamic rendering.

Fixes #43077 
fix NEXT-601 ([link](https://linear.app/vercel/issue/NEXT-601))

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:
-->

## 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)
@Annukul
Copy link

Annukul commented Mar 20, 2023

Is this issue fixed yet?

@IonelLupu
Copy link

it still doesn't work on canary for you @Annukul?

@Annukul
Copy link

Annukul commented Mar 20, 2023

@IonelLupu Saw that this was just closed 3 days ago. I have upgraded my nextjs version from v13.1.6 to v13.2.4. Let's hope it works

@IonelLupu
Copy link

@Annukul try canary. Not 13.2.4. You will see 13.2.4 was released a week ago and this issue was fixed 3 days ago. Which means the fix is not yet officially released.

@Annukul
Copy link

Annukul commented Mar 20, 2023

@IonelLupu It's not working in v13.2.4. How can I use canary? Also is it safe to use the canary branch? It might have more issues before release

@IonelLupu
Copy link

@Annukul just use a version from here that contains "canary" in the name

@MarkWittens
Copy link

The latest canary version (v13.2.5-canary.21) works for the most part, but in dev mode (npm run dev) I get an error message on pages using the searchParams:

Dynamic server usage: searchParams.p

After refreshing the page it works fine and production everything works fine as well.

@davecarlson
Copy link
Contributor

Even with the latest canary, and force-dynamic, still having issues with this.

const searchParams = useSearchParams();
const formattedQueryParams = `?${searchParams.toString()}`;

formattedQueryParams is empty

@michaelcummings12
Copy link

michaelcummings12 commented Apr 6, 2023

Even with the latest canary, and force-dynamic, still having issues with this.

const searchParams = useSearchParams();
const formattedQueryParams = `?${searchParams.toString()}`;

formattedQueryParams is empty

useSearchParams only works in client components. For server components, you can use the optional searchParams page prop.

export default function Page({ searchParams }: { searchParams: { [key: string]: string | string[] | undefined } }) {
  const formattedQueryParams = `?${searchParams.toString()}`;
  return <p>{formattedQueryParams}</p>;
}

ikizey added a commit to ikizey/video-search that referenced this issue Apr 8, 2023
@yamsellem
Copy link

yamsellem commented Apr 14, 2023

@Trugamr solution works fine with next 13.2.x but does not work with 13.3.x. Anyone has experienced this too?

@mbuffa
Copy link

mbuffa commented Apr 16, 2023

Hello there.

I'm not sure how this is related, or if I'm doing something wrong, but I'm encountering a similar issue, with:

  • NextJS 13.3.0
  • No app or src dir
  • JS setup
  • Dev (no production env yet)
  • On the server-side with an Edge API endpoint
  • Using Fetch API on client-side to do the call (await fetch('/api/dropbox/authorize?auth_code=' + code);)
  • No particular middleware
  • I generated the project a few hours ago with the official tool

Accessing req.query works with a NodeJS API endpoint, but it seemed easier to handle cookies, so I switched.

Here's the relevant NextRequest nextUrl value:

{
  cookies: REDACTED,
  geo: {},
  ip: undefined,
  nextUrl: {
  href: 'http://localhost:3000/api/dropbox/authorize?auth_code=REDACTED',
  origin: 'http://localhost:3000',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'localhost:3000',
  hostname: 'localhost',
  port: '3000',
  pathname: '/api/dropbox/authorize',
  search: '?auth_code=REDACTED',
  searchParams: URLSearchParams {  },
  hash: ''
}

If I toString() the searchParams object, I get the equivalent of the search field (without question mark).

Edit: Same issue with Next 13.2.x.

@ellemedit
Copy link

ellemedit commented Apr 18, 2023

In development, RSC searchParams works without any additional configuration but production build searchParams not work in same conditions.

Only works when I set:

export const dynamic = "force-dynamic";

Is this intended? I don't so as documentation explains...

@davecarlson
Copy link
Contributor

Apparently so.

Personally, I'm not a have of having to use to different apis to get the data and having to figure out which to use in which content ( server / client ).

It all seems rather, messy

@DannyPark1
Copy link

is this issue solved? at the latest version?

@MarkWittens
Copy link

In development, RSC searchParams works without any additional configuration but production build searchParams not work in same conditions.

Only works when I set:

export const dynamic = "force-dynamic";

Is this intended? I don't so as documentation explains...

I hope not, this makes every page dynamic (i.e. partially uncachable) that might have a filter or pager in the query parameters for example.

@timneutkens
Copy link
Member

If you're running into an issue please open a new one with a reproduction, happy to have a look!

@github-actions
Copy link
Contributor

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 Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team. Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.