#Is it possible to prevent a dynamically

1 messages ยท Page 1 of 1 (latest)

unkempt mulch
#

Maybe my composition is wrong. Because the reason for changing the url is to pass SearchParams to the ArticleContainer to render articles depending on the active tags in the DataSearch component.

crimson sundial
#

so u want to stop the server side data fetching when u open a specific route ?

unkempt mulch
#

Essentially I want it to run only once, at the first render.

crimson sundial
#

im bad at english so bare with me

coral grove
#

so skip data fetching on soft and hard navigation?

unkempt mulch
#

Only solution I've been able to come up with is to set a cookie that checks wheter to run the data-fetching methods.

#

Exactly, @coral grove

coral grove
#

you can check the header to differentiate those kind of navigation

unkempt mulch
#

Oh, really

#

That would be perfect

crimson sundial
#

i can imagine a weird implementation tho. like use redis as cache and if there is cache stop the data fetch ig (or i didnt understand the question)

unkempt mulch
#

I still think my composition is poor, somehow. Managing global state with url search params isn't exactly great

#

I just feel like the ArticleContainer should be server side rendered

coral grove
#

This is the code to differentiate those navigation

#

now I do not know if this would solve your issue as a bigger picture

#

my guts says that you don't need this if you set it up properly

unkempt mulch
#

Yeah, I have the same gut feeling.

#

How would you solve it though?

I have a client side search component that sets state from the chosen tags:

#

So on every tag change my url changes (doesn't have to)

#

Then I need to render the server component with the articles based on the chosen tags

coral grove
unkempt mulch
coral grove
#

idk i havent experimented with it

#

also whats that neat label showing server/client comp?

unkempt mulch
#

Okay, I though about that, but the layout doesn't have access to search params

coral grove
#

they do on client

unkempt mulch
#

I want the search to be sharable

#

Aah

#

So a client side layout

coral grove
#

and if you process it in client, I THINK (experiment needed) SSR would be minimal

#

not client-side layout

#

client-side component inside a layout.tsx

#

did you made the server/client component label thingy btw

unkempt mulch
#

This one

unkempt mulch
#

So let me see if I understand you correctly.

Server side Layout, client side DataSearch within Layout. But then would the ArticleContainer be server side or client side?

coral grove
#

also client

unkempt mulch
#

That's just data I use for debugging my search:

coral grove
#

oooo

#

neat stuff

unkempt mulch
#

๐Ÿ™‚

#

Thanks

coral grove
#

also client, coz you lose the privilege to ~~Serverside ~~ static render if you don't know what the searchParam is gonna be...

unkempt mulch
#

Okay, I kinda wanted to preserve that privelege :\

coral grove
unkempt mulch
#

I don't need it to be statically rendered. Dynamic is fine, but I would like for it to render on server

#

Rather than in the client

coral grove
unkempt mulch
#

Well, I want the searchParams to change, which they do, but I do want to prevent re-running the data-fetching methods.

#

They are there only for the first page load, really. So that when someone pastes a url with search params, those things are fetched. But when you do navigation afterwards, such as by choosing a new tag in the search, then I want the data-fetching to be prevented.

#

Because, as I said, it's just initial data

#

It's a bit hard to explain, probably because it's a stupid concept

#

And I'm doing it wrong

coral grove
#

ohhhhh so all the unfiltered data is already on the page.tsx?
and the searchquery on filters the unfiltered data?

#

so the filter is done client-side not server-side?

unkempt mulch
#

Yes

#

Essentially I want to send client side state to the server, which I know is not usually possible

#

Except maybe through url params

coral grove
#

I dont think thats what you want. You just want to filter the given data from the server in the client

#

i.e the filtered data is the subset of the whole fetched data. not like a paginated result

unkempt mulch
#

Hmm, not exactly.

I have two indexes on Meilisearch, one is called "Data" and one is called "Articles".

In DataSearch I choose items from "Data", those items are then used to find items from "Articles"

#

So there are two searches happening, basically

coral grove
#

client-side fetching? client-side filtering?

unkempt mulch
#

On page load an empty "Data" search is prefetched and all "Articles" are prefetched too

#

When I choose a new tag two things happen:

  1. Through Jotai an atom with activeTags is updated.
  2. The URL is updated with for example team=rosenborg
coral grove
#

right so its a client-side filtering from the prefetched data

unkempt mulch
#

Would it help if I streamed to you?

coral grove
#

im uncomfortable with that :D

unkempt mulch
#

The interaction is done client-side, and I have an atom that uses tanstack-query to fetch from my api route api/searchData

coral grove
#

right

#

well

coral grove
unkempt mulch
coral grove
#

HAHA

unkempt mulch
#

Yeah, I'll try

#

What? ๐Ÿ˜„

coral grove
#

wrap the function to prefetch data with const data = onPageReload()

#

so that it is only prefetched at hard refresh

unkempt mulch
#

Great! Where is this code from?

coral grove
#

homebrew

unkempt mulch
#

what's that?

coral grove
coral grove
unkempt mulch
#

Ah, cool

coral grove
#

its inspired by cache() from react

unkempt mulch
#

I guess the notFound stuff is unecessary for me

coral grove
#

Just know that on soft/navigation the header ssc-fetch-dest is not equal to document

unkempt mulch
#

Dude, it works flawlessly

#

What does that mean actually?

coral grove
#

Which.. part...?

unkempt mulch
#

headers().get("sec-fetch-dest") === "document";

#

What is this actually doing?

#

I should look into nextjs headers more.
Sec-Fetch-Dest

#

indicates the request's destination. That is the initiator of the original fetch request, which is where (and how) the fetched data will be used.

#

Hmm

coral grove
#

Each route change sends a request to the server

Each request sends to the serve rhas headers attached to it

headers() is a server funtion to get headers of request

So i checked how navigation and hard refresh can differentited on the headers

Idk what sec-fetch-dest is

Soft/hard navigation doesnt send the sec-fetch-dest : "document" upon investigation

coral grove
unkempt mulch
#

Yeah. So it's kind of a hack ๐Ÿ˜„

#

But it works, for now

coral grove
#

It really is xD

unkempt mulch
#

But there isn't

#

Anyway, you just saved me a lot of work, so thanks a lot