Next.js Discord

Discord Forum

Does it make sense to make a client component that returns null for JS code that runs on every page?

Answered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
I'm trying to wrap my head around the app router and using server/client components. For example if I wanted to have some analytics tracking and other metric calls that happen on each page, I would normally do this in a useEffect in the base layout. Is the proper way to handle this in next13+ paradigm to make a client component that just returns null and import that in the base layout within a Suspense?
Answered by Ray
yes I would do that
View full answer

8 Replies

Nile CrocodileOP
The other option is to create a client component that returns children and than wrap the children of the base layout, but I'm still confused if that would then make the rest of the page a client component or not. Based on my search so far it seems that its okay and nextjs will correctly split the client/server chunks
Answer
Nile CrocodileOP
cool, thanks
@Ray yes I would do that
Silver Marten
is it not possible to use middleware for this?
@Silver Marten is it not possible to use middleware for this?
I don't think you can use useEffect in middleware
Silver Marten
oh sorry i get it now
my bad