4

I want to implement GetServerSideProps with SWR, but i cant stop the clientside from fetching even though the data is loaded from the server side.

Here's a simplified example

  1. We fetch a wordlist from the api with a given wordlength in the getServerSide/getStaticProps and SSR/SSG build the page for fast delivery

  2. The user interacts with the page causing iterateWordLength() to fire

  3. now we go fetch an updated wordlist with mutate

What's happening: SWR is firing twice on load

  1. Serverside runs just once ✅
  2. Wordlist useEffect fires - get the word "foo" from my artificial getServerSideProps and render ✅
  3. WordClientSideFetcher fires ❌ (it shouldnt cause we already have the data)
  4. Wordlist changed fires again ❌

i am not using useSWRImmutable because my fetcher and GET params will change based on the state of another state variable

I've set all the revalidate options false... why is it firing twice?

Simplified Index.js

function Index( { fallback } ) {

const [wordLength, setWordLength] = useState(DEFAULT_WORD_LENGTH);  

  const { data: wordlist, mutate } = useSWR('/api/wordlist', clientSideFetcher, { 
    fallbackData: fallback['/api/wordlist'],
    revalidateIfStale: false, // all set to false to stop that clientSideFetcher
    revalidateOnMount: false, 
    revalidateOnFocus: false 
  })
}

  useEffect(()=>{
   // wordlength iterated
    mutate() // updates wordlist
  }, [wordLength]) 

  useEffect(()=>{
    console.log("### Wordlist changed")
    setWord( generateRandomWord() )
  },[wordlist])

  const iterateWordlength = () => { setWordlength(wordLength+1)}

export async function staticFetcher() {
  const res = await fetch(API_URL)
  const data = await res.json()
  console.log(`### staticFetcher fetched ${data.length} words`)
  return new Wordlist(...data) 
}

export async function getServerSideProps(context) { 
  const staticWordlist = await staticFetcher(context)
    .catch( err => {
      return { notFound: true }
    })
  const props = { 
    fallback: {
      '/api/wordlist' : ["foo"] // temp replace the actual array
    }
  }
  return { props }
}
fotoflo
  • 821
  • 1
  • 9
  • 21
  • 1
    If you don't want it to fetch the data on the client, then why even use `swr`? If you're not revalidating the data on the client-side, what's the use case for `swr`? – juliomalves Mar 14 '22 at 13:14
  • 1
    thank you @juliomalves, i've updated the example a little bit to show that we actually re-fetch the data a lot. Additionally we will reuse the old data if it's been previously fetched in other sceneries. – fotoflo Mar 15 '22 at 04:45
  • The behaviour you described is the expected one, from [SWR docs](https://swr.vercel.app/docs/with-nextjs#pre-rendering-with-default-data): _"component will render the pre-generated data first, and after the page is hydrated, it will fetch the latest data again to keep it refresh."_. – juliomalves Mar 15 '22 at 21:40
  • @juliomalves I thought all the flags i've added would disable that.... – fotoflo Mar 30 '22 at 08:26

1 Answers1

0

This is due to React18 strict mode, which should not happen in production build.
Basically, it mounts components twice (mounts, unmounts then remounts). Any piece of code within useEffect will be fired twice.

There is option to disable this feature in nextjs but you should keep it as it is.

Thanh Nhan
  • 453
  • 6
  • 17