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

Cant generate Static pages because navbar uses SSR for user #276

Open
EpicDevv opened this issue Nov 20, 2023 · 3 comments
Open

Cant generate Static pages because navbar uses SSR for user #276

EpicDevv opened this issue Nov 20, 2023 · 3 comments

Comments

@EpicDevv
Copy link

EpicDevv commented Nov 20, 2023

I want to extend this app to include some Static pages that's use ISR for product pages. However due to the navbar using SSR to get the user and the nav bar is in the root layout. It is causing all my pages to be SSR pages. How can I fix this? Thanks for your time and help.

@ajayvignesh01
Copy link

If that's the only component that's causing it, you can change that part to use either SWR or useEffect like this:

SWR

  const supabase = createClientComponentClient()
  
  const getUser = async () => {
    const { data: { user } } = await supabase.auth.getUser()
    return user
  }

  const { data: user } = useSWRImmutable('getUser', getUser)

useEffect

const [user, setUser] = useState<User>()

useEffect(() => {
  const supabase = createClientComponentClient()

  async function getUser() {
    const { data: { user } } = await supabase.auth.getUser()
    return user
  }

  getUser().then((user) => setUser(user))
}, [])

Make sure you 'use client' at top of page and import the necessary modules.

@EpicDevv
Copy link
Author

Thank you for your prompt reply. I was wondering if there is a way to retain the supabase request for user information on the server, while also being able to statically generate or cache the rest of the page. I believed that Next.js 13 made this possible. Can I keep the navbar as it is, but use generateStaticParams for multiple static pages to be pre-built beforehand?

@chriscarrollsmith
Copy link
Contributor

Thank you for your prompt reply. I was wondering if there is a way to retain the supabase request for user information on the server, while also being able to statically generate or cache the rest of the page. I believed that Next.js 13 made this possible. Can I keep the navbar as it is, but use generateStaticParams for multiple static pages to be pre-built beforehand?

At the top of each page or component, you can add 'use client' or 'use server' as desired. Typically you want server components to be above client components in the tree, but technically I believe you can chain them however you want.

But for static content, why wouldn't you want them to be rendered server-side? Rendering static content is, like, one of the main use cases for SSR.

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

3 participants