Next.js Discord

Discord Forum

What happens to SSR components nested inside CSR components?

Answered
Chub mackerel posted this in #help-forum
Open in Discord
Chub mackerelOP
Hi everyone! I'm really confused about mixing SSR and CSR components. For example, let's say I have a SearchResult component that contains a list of products as the result of a search request. I'd like it to be an SSR component since the request is made from the URL bar (e.g., the user opens the URL nextjssite.example/search?productName=cup, and on the server, we can obtain the productName search parameter and use it to query the resource). However, I have a theme provider in the root layout that wraps all components down the tree.

layout.tsx
// ThemeProvider is a Client Component (use client is defined in ThemeProvider.tsx)
import ThemeProvider from './ThemeProvider'
// SearchResults I want to have as a Server Component
import SearchResults from './SearchResults'
 
export default function Layout() {
  return (
    <ThemeProvider>
      <SearchResults />
    </ThemeProvider>
  )
}


The confusing part from the documentation is: 'This means that by defining a "use client" in a file, all other modules imported into it, including child components, are considered part of the client bundle.' SearchResults is making a request to fetch the products list from a third-party service. Does including SearchResults inside ThemeProvider mean that the request will be made from the browser? I fee like the answer is no, but what the difference then between SSR which is not nested in CSR component and SSR which is nested?
Answered by Thrianta
There is literally no difference other than in the 2nd case, the client components inside SearchResult will not have access to the states in the ThemeProvider context.
View full answer

6 Replies

@Chub mackerel Hi everyone! I'm really confused about mixing SSR and CSR components. For example, let's say I have a SearchResult component that contains a list of products as the result of a search request. I'd like it to be an SSR component since the request is made from the URL bar (e.g., the user opens the URL nextjssite.example/search?productName=cup, and on the server, we can obtain the productName search parameter and use it to query the resource). However, I have a theme provider in the root layout that wraps all components down the tree. layout.tsx // ThemeProvider is a Client Component (use client is defined in ThemeProvider.tsx) import ThemeProvider from './ThemeProvider' // SearchResults I want to have as a Server Component import SearchResults from './SearchResults' export default function Layout() { return ( <ThemeProvider> <SearchResults /> </ThemeProvider> ) } The confusing part from the documentation is: 'This means that by defining a "use client" in a file, all other modules imported into it, including child components, are considered part of the client bundle.' SearchResults is making a request to fetch the products list from a third-party service. Does including SearchResults inside ThemeProvider mean that the request will be made from the browser? I fee like the answer is no, but what the difference then between SSR which is not nested in CSR component and SSR which is nested?
Thrianta
Well, if you are using supported pattern for using Server Component in Client Component there will be no problem. Everything will work as expected. You can't directly import and use a Server Component inside a Client Component because the rendering order of them is different. However you can nest them using children prop (I believe that's what you did in the example you gave)

You can check out Next.js docs, it's very well written
https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#supported-pattern-passing-server-components-to-client-components-as-props
Chub mackerelOP
thanks Yunus for the answer, but it's not what I asked. My question is what the difference between SSR which is not nested in CSR component and SSR which is nested.

SSR inside CSR

layout.tsx
// ThemeProvider is a Client Component (use client is defined in ThemeProvider.tsx)
import ThemeProvider from './ThemeProvider'
// SearchResults I want to have as a Server Component
import SearchResults from './SearchResults'

export default function Layout() {
return (
<ThemeProvider>
<SearchResults />
</ThemeProvider>
)
}

SSR separate to CSR

layout.tsx
// ThemeProvider is a Client Component (use client is defined in ThemeProvider.tsx)
import ThemeProvider from './ThemeProvider'
// SearchResults I want to have as a Server Component
import SearchResults from './SearchResults'

export default function Layout() {
return (
#Unknown Channel
<ThemeProvider />
<SearchResults />
</>
)
}


What is difference for SearchResults in 2 examples above?
Thrianta
There is literally no difference other than in the 2nd case, the client components inside SearchResult will not have access to the states in the ThemeProvider context.
Answer
Thrianta
In the 2nd case, ThemeProvider is completely useless.
Chub mackerelOP
does it mean that SearchResults in both examples will be rendered on the server without the process of hydrating, etc.?
Thrianta
Exactly