Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I turn off SSR for only certain pages in Nuxt.js to use them as SPA application?

I want to develop an application with Nuxt.js that uses SSR for only certain pages (like artist page user page), so the pages without SSR will be used like an SPA. Is it possible to do it using Nuxt.js?

like image 262
piemekanika Avatar asked Feb 01 '19 03:02

piemekanika


People also ask

How does SSR work in Nuxt?

Server-side rendering (SSR), is the ability of an application to contribute by displaying the web-page on the server instead of rendering it in the browser. Server-side sends a fully rendered page to the client; the client's JavaScript bundle takes over which then allows the Vue.

Is Nuxt single page application?

Nuxt is well-suited for developing single-page apps if you have any reason that prevents you from using Nuxt as a server-side rendering app.

Is Nuxtjs a SSR?

Another important thing to note is Nuxt doesn't have to be used for SSR. It's promoted as a framework for creating universal Vue. js applications and includes a command ( nuxt generate ) for creating static generated Vue applications using the same codebase.


2 Answers

You could do that via server middleware

export default function(req, res, next) {
  const paths = ['/', '/a']

  if (paths.includes(req.originalUrl)) {
    // Will trigger the "traditional SPA mode"
    res.spa = true
  }
  // Don't forget to call next in all cases!
  // Otherwise, your app will be stuck forever :|
  next()
}

https://blog.lichter.io/posts/selectively-enable-ssr-or-spa-mode-in-a-nuxtjs-ap

like image 136
Aldarund Avatar answered Sep 20 '22 06:09

Aldarund


Wrap the contents of the component you don't want to render server side in a <no-ssr></no-ssr> tag.

@DenisTsoi's link should give you more information on how it works.

like image 30
AlxTheRed Avatar answered Sep 20 '22 06:09

AlxTheRed