5

I have an image lazy load component that just renders the image src when on the server, and renders a loader when on client pending when the actual image is lazyloaded.

The problem. After the initial render and client side takes over, the image src mismatch. This is because the server set the actual src, but the client is setting the loader (placeholder).

Question. Is there a way to detect that this initial render was server rendered? just before the client renders/mounts

2
  • 1
    nextJS has good support for lazy loading so you don't have to use own logic nextjs.org/docs/basic-features/image-optimization Commented Jul 15, 2022 at 4:59
  • Thanks @Buggies. At that time, this feature wasn't available. Unfortunately I am not working on nextjs project at this time but will test it out to see if it solves the original problem. Nothing like that was mentioned on the docs but since this is coming from the framework, I will assume it was handled. > After the initial render and client side takes over, the image src mismatch. This is because the server set the actual src, but the client is setting the loader (placeholder) Commented Jul 15, 2022 at 23:49

3 Answers 3

3

You can find out if it is currently executed on the server by checking for req attribute inside getInitialProps

Example page code

function Page({ isServer }) {
  return <div>Is this page on the server? - {isServer ? 'YES' : 'NO'}</div>;
}

Page.getInitialProps = async ({ req }) => {
  return { isServer: !!req };
};

export default Page;

Some info on official repo about isServercheck

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for taking the time to answer. However, this is not what i want to achieve.
Let's say my image component get's this YES prop when mounted on the server, and then user interacts with the page later, will this prop update to NO?
yes, as docs say For the initial page load, getInitialProps will execute on the server only. getInitialProps will only be executed on the client when navigating to a different route via the Link component or using the routing APIs.
Yes, i am aware of that. That's why i was specific "User interacts with page", not navigate from page
For example, user clicks on see more to load more images and new image components are mounted
|
0

You can check that typeof Window === 'undefined'

1 Comment

To make this a good answer, please expand your answer with an explanation.
0

The fix is to use useEffect as described in the Next.js docs at https://nextjs.org/docs/messages/react-hydration-error

do this:

import { useState, useEffect } from 'react'

function AnyComponent() {
  const [isSSR, setIsSsr] = useState(true)
  console.log(`isSSR: `, isSSR);
  useEffect(() => setIsSsr(false))

  return (
    <span>Is SSR? {isSSR ? 'Yes!' : 'No!'}</span>
  )
}

Obviously, this trivial example will update so quickly that you would only ever see "Is SSR? No!" but the first render will be yes. Check the console.

Why? useEffect is only executed in the browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.