Async Data

You may want to fetch data and render it on the server-side. Nuxt.js adds an asyncData method to let you handle async operations before initializing the component

The asyncData method

Sometimes you just want to fetch data and pre-render it on the server without using a store. asyncData is called every time before loading the page component.It will be called server-side once (on the first request to the Nuxt app) and client-side when navigating to further routes. This method receives the context as the first argument, you can use it to fetch some data and Nuxt.js will merge it with the component data.

Nuxt.js will automatically merge the returned object with the component data.

You do NOT have access of the component instance through this inside asyncData because it is called before initiating the component.

Nuxt.js offers you different ways to use asyncData. Choose the one you're the most familiar with:

We are using axios to make isomorphic HTTP requests, we strongly recommend to use our axios module for your Nuxt projects.

If you are using axios directly from node_modules and used the axios.interceptors to add interceptors to transform the data, make sure to create an instance before adding interceptors. If not, when you refresh the serverRender page, the interceptors will be added multiple times, which will cause a data error.

  1. import axios from 'axios'
  2. const myaxios = axios.create({
  3. // ...
  4. })
  5. myaxios.interceptors.response.use(function (response) {
  6. return response.data
  7. }, function (error) {
  8. // ...
  9. })

Returning a Promise

  1. export default {
  2. asyncData ({ params }) {
  3. return axios.get(`https://my-api/posts/${params.id}`)
  4. .then((res) => {
  5. return { title: res.data.title }
  6. })
  7. }
  8. }

Using async/await

  1. export default {
  2. async asyncData ({ params }) {
  3. const { data } = await axios.get(`https://my-api/posts/${params.id}`)
  4. return { title: data.title }
  5. }
  6. }

Displaying the data

The result from asyncData will be merged with data.You can display the data inside your template like you're used to doing:

  1. <template>
  2. <h1>{{ title }}</h1>
  3. </template>

The Context

To see the list of available keys in context, take a look at the API Essential context.

Use req/res objects

When asyncData is called on server side, you have access to the req and res objects of the user request.

  1. export default {
  2. async asyncData ({ req, res }) {
  3. // Please check if you are on the server side before
  4. // using req and res
  5. if (process.server) {
  6. return { host: req.headers.host }
  7. }
  8. return {}
  9. }
  10. }

Accessing dynamic route data

You can use the context parameter to access dynamic route data as well!For example, dynamic route params can be retrieved using the name of the file or folder that configured it.If you've defined a file named _slug.vue in your pages folder, you can access the value via context.params.slug:

  1. export default {
  2. async asyncData ({ params }) {
  3. const slug = params.slug // When calling /abc the slug will be "abc"
  4. return { slug }
  5. }
  6. }

Listening to query changes

The asyncData method is not called on query string changes by default.If you want to change this behavior, for example when building a pagination component,you can set up parameters that should be listened to with the watchQuery property of your page component.Learn more on the API watchQuery page page.

Handling Errors

Nuxt.js adds the error(params) method in the context, which you can call to display the error page. params.statusCode will be also used to render the proper status code from the server-side.

Example with a Promise:

  1. export default {
  2. asyncData ({ params, error }) {
  3. return axios.get(`https://my-api/posts/${params.id}`)
  4. .then((res) => {
  5. return { title: res.data.title }
  6. })
  7. .catch((e) => {
  8. error({ statusCode: 404, message: 'Post not found' })
  9. })
  10. }
  11. }

To customize the error page, take a look at the views guide .