Nuxt fetch hook: why triggered both in server and in client?

Daniel Martinez

fetch is called on server-side when rendering the route, and on client-side when navigating.

I understand Nuxt.js enables the concept of Universal apps, but why would anyone need to have a fetch hook block executed in both server and client? Sounds redundant to me.

kissu

This is essentially used for the isomorphic (aka Universal) app aspect. So, if you have ssr: true, this will help you for performance and SEO by generating the content ahead of time, hence on the server too.

It is done on both side because you need to have the same content on both Server and Client, otherwise there will be a DOM missmatch and the hydration will fail (implying a huge performance penalty!).

And target: static + ssr: true is a common combo, especially if hosting on Vercel/Netlify because it will provide performance, SEO and still all the benefits of an SPA.

If you use your Nuxt app as SPA only (ssr: false), the benefit is a bit less impressive but people usually come to Nuxt for it's SSR/SSG aspect.
But then, you're stuck waiting on your app while your whole SPA is doing it's thing and...you can say goodbye to SEO.

If you don't like it, you can always set fetchOnServer to false per component or in nuxt.config.js.
https://nuxtjs.org/docs/2.x/components-glossary/pages-fetch#options

Lastly, when it says that this will be generated on both server and client, we do need to understand that the server is making the call:

  • when we are generating the static content, this is not the case anymore after the initial hydration is complete
  • once if target: server, once the JS is shipped to the browser, the SPA will handle the subsequent calls on client side

It's not like it'a huge waste and that the server is always doing the call for you on each client side navigation (MPA approach).


As a sidenote (using asyncData), when generating some pages from an API, you can use the already computed server payload and not have to fetch it on the client again. Useful for some purely static content like a blog!
https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-generate#speeding-up-dynamic-route-generation-with-payload

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Nuxt Fetch run 2 times(on server and client)

How to access Nuxt context inside of fetch() hook?

Why doesn't OWASP recommend to bcrypt the password both on the client and the server?

Why specify same port in sin_port in both server and client?

Git - From a server side hook, is it possible to control which branches a client can fetch

why is gRPC server side stuck after client is done streaming (client and server both written in dart)?

How to use properly the new fetch() hook in nuxt 2.12 for SSR?

Why is the client's file descriptor used while calling the recv and send funtions on both server and client sides?

Init nuxt plugins once instead of server + client

When using hydrate, why do we need to match the classNames in both server and client, and How?

Why cant both my client communicate to each other? (java server socket)

Nuxt.js 2 - how to access hook instance in server middleware

why does nuxt.js call the beforeCreate, created hook in SSR?

Is the same port acting as both client and server?

Webpack babel config for both server and client javascript?

socket io not receiving events for both client and server

Both client and server works on one file

Dart project configuration for both server and client programming?

RPC from both client and server in Go

Using the same code for both client and server

Programming a BLE server and a client both in a Raspberry Pi

useEffect() hook is not triggered?

How to prevent template errors while fetching data with Nuxt's fetch() hook

In a mobile messaging app, should sending a message be triggered from the server or the client?

Client side can't fetch server response

NextJS server fetch based on client input

Self sign certificate client authentication with client and server both on localhost

Why won't the useEffect hook run and fetch data?

Nuxt app - Client & Server files separated - how to deploy to Heroku?