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

Use getSearchParams() without force-dynamic for draft mode requests ? #39

Closed
MangoMarcus opened this issue Mar 21, 2024 · 3 comments
Closed

Comments

@MangoMarcus
Copy link

MangoMarcus commented Mar 21, 2024

Hi there,

I'm not using export const dynamic = 'force-dynamic' as I'm trying to use static rendering where possible, I understand though that unlike getPathname(), getSearchParams() requires dynamic rendering as the search params can only be known at request time, not build time.

However I wondered if it's still possible to use it in draft mode, which is rendered at request time, eg.

import { getSearchParams } from 'next-impl-getters/get-search-params';
import { draftMode } from 'next/headers';

const { isEnabled } = draftMode();

if (isEnabled) {
  // Page is being dynamically rendered
  const token = getSearchParams({ ignoreDynamicOptionErrors: true }).get('token');
} else {
  // Page is being statically rendered

}

This does work locally, but not on a preview deployment on Vercel.

Is this possible? Or am I missing something

Thanks!

@vordgi
Copy link
Owner

vordgi commented Mar 21, 2024

Hello! Thanks for the issue, this is a really interesting and important point.

Apparently next.js removes some params specifically for Vercel 😬

image

As an option, I can suggest adding a url header to the middleware and extract params from it, taking it from headers (vercel/next.js#43704 (comment))

In any case, I’ll try to dig deeper and maybe to figure it out

@vordgi
Copy link
Owner

vordgi commented Mar 23, 2024

Well. After researching decided to mark this getter as deprecated. There are too many situations where next.js already trims or may trim params.

Instead, it will be recommended to use a solution via middleware and get-page-config (as written here).

Thanks again for writing about the problem!

@vordgi vordgi closed this as completed Mar 24, 2024
@MangoMarcus
Copy link
Author

Hey @vordgi , thanks for the speedy response and action! Interesting that NextJS removes those params.

Thanks for the middleware suggestion, that was very helpful. I've got it working now, I didn't realise that middleware doesn't necessarily opt you in to dynamic rendering for a page so that does the job perfectly for me.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants