Giter Club home page Giter Club logo

Comments (6)

liximomo avatar liximomo commented on April 28, 2024 2

@posva I think It's feasible. Assigning the param of onServerPrefetch to $options.serverPrefetch should be enough.

from composition-api.

TooBug avatar TooBug commented on April 28, 2024 2

#80

There exists a pull request.

from composition-api.

rebeccarich avatar rebeccarich commented on April 28, 2024 2

I'm also encountering this issue.

I have made the change from PR #80 locally and the onServerPrefetch hook is being called and I can see async function being called server side, but I'm not sure how to assign the result of the call to a reactive property to be used in the template.

What is the correct way to assign the result of a server side async call to a reactive property?

Sample code:

import { createComponent, reactive, watch, onServerPrefetch } from '@vue/composition-api'
import gql from 'graphql-tag'

async function useCourses(client) {
  return client
    .query({
      query: gql`
        query getCourses {
          courses {
            id
            title
            author
            description
            topic
            url
          }
        }
      `
    })
    .then(res => res.data.courses)
}

const Courses = createComponent({
  name: 'Courses',
  components: {
    'c-course': () => import('./Course.vue')
  },

  setup(initialProps, context) {
    const { defaultClient: apolloClient } = context.root.$apolloProvider

    const state = reactive({
      courses: []
    })

    onServerPrefetch(async () => {
      // Working: the gql call is being made
      const result = await useCourses(apolloClient)
      // console.log(result)
      // Not working: Can't assign the result to state.courses
      watch(async () => {
        state.courses = result
      })
    })

    // Working: client side call and assignment
    watch(async () => {
      state.courses = await useCourses(apolloClient)
    })

    return { state }
  }
})
export default Courses

from composition-api.

LinusBorg avatar LinusBorg commented on April 28, 2024

I think this is a limitation of this plugin in Vue 2. Making the new API work with SSR will require change to internals that we will do in Vue 3, but not Vue 2.

from composition-api.

posva avatar posva commented on April 28, 2024

@liximomo if this is indeed a limitation we can label it as wont fix and close it

from composition-api.

dsseng avatar dsseng commented on April 28, 2024

not sure how to test this one, but I can implement it.

from composition-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.