Next.js Discord

Discord Forum

Static page with search params

Unanswered
Amur catfish posted this in #help-forum
Open in Discord
Amur catfishOP
Is there a way to have a page that is statically generated, and when you add search params to it, it will fallback and render dynamically?

For example if you have a blog at /blog with static content, and you want some filtering, /blog?q=test should be a dynamic page, where it searches the blog for test.
This doesn't seem possible with next right now without doing weird middleware stuff. Is there any tehnical reason for that, or am I missing something? I feel like it should just be able to treat it as a normal param...

5 Replies

@Amur catfish Is there a way to have a page that is statically generated, and when you add search params to it, it will fallback and render dynamically? For example if you have a blog at `/blog` with static content, and you want some filtering, `/blog?q=test` should be a dynamic page, where it searches the blog for `test`. This doesn't seem possible with next right now without doing weird middleware stuff. Is there any tehnical reason for that, or am I missing something? I feel like it should just be able to treat it as a normal param...
the problem is that in nextjs, a page.js can only be either static or dynamic. you can't change the staticity depending on the incoming request.

i'd cache the page data for /blog (with fetch or unstable_cache), then use that cached page data if there is no search params. in that way, while /blog is still dynamic, the most expensive part of it (the data) is static, so it's still performant.
So I feel like that should technically be possible with search params too, just that it isnt..
i fully agree
but yeah unfortunately this is what we are stuck with