I'm new to Nuxt - converted my vue app to it, and got it working on production: gameslate.io
The app is using the asyncData
method to get the list of games - however the full layout isn't being rendered the server...
If you view the page source you'll see that Nuxt is putting all of the data in window.__NUXT__
, but there's no grid html.
How do I render the full layout on the server after getting data with asyncData
?
Otherwise, google can't crawl the game lists - this becomes pretty useless for SEO... (checkout twitter's page source - it loads the entire page including dynamic content)
This is the "home" page component that makes the asyncData call (simplified):
<template>
<div>things</div> <!-- rendered on server -->
<game-grid :games="games"></game-grid> <!-- rendered in browser -->
</template>
<script>
export default {
data() {
return {
games: []
}
}
async asyncData() {
let games = await getGames(filters);
return { games };
}
}
</script>
Nuxt. js is a framework for Vue. js applications that can solve this problem with server-side rendering, a strategy that renders the application on the server then sends it to the client. With Nuxt, all of your routes are generated from .
Nuxt supports both client-side and universal rendering.
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.
Server Side Rendered Sites and Static Sites See deployment targets for more info on static and server hosting. You do not need to add ssr: true to your nuxt config in order to enable server-side-rendering as it is enabled by default.
You do not need to add ssr: true to your nuxt config in order to enable server-side-rendering as it is enabled by default. With client side rendering only there is no server-side rendering. Client side rendering means rendering the content in the browser using JavaScript.
In order to enable SSR in your application, we need to head to our beloved nuxt.config.js file again and make sure that the mode property is set to universal. This tells Nuxt it should create an isomorphic application, i.e. execute SSR and Client Side Rendering. If the mode property isn't declared, it automatically defaults to universal.
If you use Nuxt for server side rendering, at some point you'll likely need to use server side plugins. As a reminder, plugins are functions that can run server-side or client-side, once per user.
You have to remember that a Nuxt server is a running instance and all first hits to your site run though it. Each plugin does run per user server-side with new state and store but pretty much everything else stays in memory. Here, let me show you something.
With Nuxt >= 2.12, you can use the fetch() hook to get & process data before the page renders. It's leveraged both in back-end and front-end: "...fetch is called on server-side when rendering the route, and on client-side when navigating."
<template>
<div>things</div> <!-- rendered on server -->
<game-grid :games="games"></game-grid> <!-- rendered in browser -->
</template>
<script>
export default {
data() {
return {
games: []
}
}
async fetch() {
// Get & process data, in whatever way you prefer
await myGetFunc(filters).then(response => {
// Do something with the payload, like set component data...
this.games = response;
/// ... or send it to a VueX store:
this.$store.dispatch('globalResults', response)
}).catch(error => {
console.log('ERROR: ', error)
}})
}
methods: {
// ...
},
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With