What's the real difference between target: 'static' and target: 'server' in Nuxt 2.14 universal mode?
Asked Answered
U

2

25

in the latest version of Nuxt (2.14) they introduced an optimization for building the app when no code is changed (for drastically improve build times).

I make websites in jamstack, deploy on netlify with nuxt generate and, until now, with target: 'server'. I tried the new target: 'static' in order to take advantage of this new feature, but my code won't build as it seems that in this mode the app can't access to this.$route in order to generate dynamic pages.

So, my question is: how is this different from each other? When I switch target to what I have to pay attention?

Unscratched answered 10/8, 2020 at 8:21 Comment(1)
is ssr: true or false?Ticklish
B
46

Update:

According to the latest documentation, mode: 'universal' and mode: 'spa' were deprecated in favor of ssr: true and ssr: false.

Full static works only with target: 'static' and ssr: true (counterpart of deprecated mode: 'universal'). The ssr: true is a default value. The ssr: false is a counterpart of the deprecated mode: 'spa' and cannot be used with target: 'static'. enter image description here

Original answer:

Target

It might be helpful to think about the target property as a hosting environment - whether you need a server or static files served by the CDN are good enough for your scenario. Despite the fact it's called target: 'server', it doesn't mean your app is server-side rendered (see mode bellow).

When using the static target, in a production environment, you just need a CDN that will serve your static files. These static files are prepared at the build time and are 'final' (until the next build with updated content or code). There is no need for any server in this scenario (other than CDN and build server which will be probably in your CI pipeline).

Target & Mode

On the other hand, when using the server target, your production app will need a server environment where the response for the client's request is composed and is sent. This means that with the updated content there's no need to rebuild your app. This content is composed after the server is requested. This applies to both - universal and spa mode. With the universal mode, your app is server-side rendered. In comparison, with the spa mode there is no server-side rendering (only client-side navigation) and the whole app runs as single page application

enter image description here

Server vs. Static Target

It might be a little bit tricky for newcomers to decide whether to use server-side rendering or static. A good question which might help you make a decision is whether you need to provide different content for each page/document/content item for different user/circumstances. If so, you should probably go with the target server, otherwise static.

Each of these approaches has got its pros and cons such as server requirement, security, speed, CI pipeline/build process, SEO, price, etc. The right choice depends on your use case.

As you mentioned correctly, from version 2.13 there are available two deployment targets. Those are server and static. Source

The old approach had some issues and difficulties, mainly with the client requesting your API via asyncData and fetch functions for your navigation. As a result, the generated site was not pure static whatsoever. All the drawbacks of the old approach are described here.

With the new static target (and mandatory universal mode at the same time) approach, the nuxt generate command will pre-render all your HTML pages and mocks async data requests. That old asyncData and fetch are not requesting your API from the client this time. This is already being performed during the build time. Source

Regarding the routes. The mentioned routes were not probably detected by nuxt's crawler automatically and you should generate them manually using generate.routes property.

import axios from 'axios'

export default {
  generate: {
    routes() {
      return axios.get('https://my-api/users').then(res => {
        return res.data.map(user => {
          return '/users/' + user.id
        })
      })
    }
  }
}
Benioff answered 28/8, 2020 at 16:58 Comment(12)
Hey! I found your article! xDCrifasi
I've been trying to get my head round target vs. rendering mode for some time now, as there seems (at least to me) significant overlap in implied concepts. This answer excellently sheds some light on this murkiness - thank you.Scrutiny
One thing I'm still struggling with is, how can target = static and ssr = true make any sense? Static = CDN hosting; SSR = server-side rendering. But there's no server-side rendering environment with CDN hosting - that would need a real server. Am I missing something?Scrutiny
From my understanding ssr: true only expresses "at build time" - where (and when) the document (DOM) is created - whether on client (SPA) or on the server at build time. TL;DR; it's just a static site - something like gatsby does - just bunch of html, css, and js bundled together and served by CND, that's it.Benioff
@MartinMakarsky @mitya yep, this is it: ssr: true + target: static is basically bundling the HTML files on the server but during the build time only. It'll be totally fine with a CDN because the build will already be done. If you choose target: server, it means that no server-side content will be generated ahead of time, hence you'll need to render it when you reach the website.Trondheim
Btw, I realized a huge issue here. ssr: false and target: static is actually totally doable!Trondheim
@Trondheim - feel free to update my answer or add a new one :)Benioff
Fantastic clarification! Maybe you could clarify a related post: #71072184 @TrondheimLibeler
With target=static and ssr=true, my SEO is dead :( I don't understand how to get good SEO and dynamic METAS :(Countrywide
How to understand: Server+SSR:false/client side navigation SSR:False == Client side rendering. Then why do we still need a Server?Ticklish
@MartinMakarsky I think ssr: true expresses "rendering at server side", when target is server, it renders when page is requested from client side. otherwise it render at build time when the target is staticTicklish
I think ssr: true, static:true is equavelent of ssg, right?Ticklish
T
0
target:static target:server
ssr:true (universal) SSG(pure static site pages generated at build time)), can be deployed in storage service like s3. Even API data is prefetched and cached SSR(With server populating the template at each request), deployed to a compute service like GCP Cloud run
ssr:false (spa) SPA bundle/CSR (client side output: static spa with js updating each web page), can be deployed in storage service like s3 and CloudFront Mixed(Nuxt will not fully render the HTML for each page - leaving that task to the browser. ) deployed to a compute service like GCP Cloud run

Right now, in a project I work in, we use target:server, ssr:false, the mixed one. We disable ssr because we wanna offload the work to client side. We still use server cause we need the flexibility to update the website anytime. I don't agree with @Martin Makarsky regarding it is client side navigation. The only client side navigation should be SPA. target:server, ssr:false is like a mixed mode, combing the benefits of lower response and flexibility. When deploy,

  1. nuxt build ( rendering-on-the-fly and built into production-ready bundle)
  2. nuxt start (start the server)

As for ssg, ssr and spa, I think there are a lot of docs explaining them.

SSG is purely static website with even API call cached. The typical framework of SSG is Gatsby.js.

With SSR, When a user visits a specific route, the Node.js server will quickly fetch the data, render it, and send it as a static HTML page to the client. Soon after, the application gets hydrated and becomes a single page application and SSR is no longer required

SPA is under the category of CSR(Client Side Rendering). It is slow for the initial js bundle downloading and not friendly to SEO Good thing about it is Time to Interactive (TTI)

I have only used the target:server, ssr:false. For other combinations, it is based on my research and past experience. If there is any error, welcome to update my answer!!

References:

  1. https://nuxtjs.org/announcements/going-full-static/#current-issues
  2. https://fauna.com/blog/comparing-spas-to-ssg-and-ssr
  3. https://nuxtjs.org/docs/features/rendering-modes
  4. https://nuxtjs.org/docs/features/deployment-targets/
  5. https://nuxt.com/docs/getting-started/deployment
Ticklish answered 22/1, 2023 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.