Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

fix: vue warn toRefs() when using useMeta #244

Closed
vaiil opened this issue Sep 21, 2020 · 6 comments Β· Fixed by #251
Closed

fix: vue warn toRefs() when using useMeta #244

vaiil opened this issue Sep 21, 2020 · 6 comments Β· Fixed by #251
Assignees
Labels

Comments

@vaiil
Copy link
Contributor

vaiil commented Sep 21, 2020

πŸ› The bug
When I use useMeta, I get the following warning in dev mode on server side:
toRefs() expects a reactive object but received a plain one.

πŸ› οΈ To reproduce
Steps to reproduce the behavior:

  1. Write smth like that:
import { defineComponent, useMeta } from '@nuxtjs/composition-api'

export default defineComponent({
  setup() {
    useMeta({ title: '234' })
    return () => <div></div>
  },
  head: {}
})
  1. exec npm run dev
  2. see in console:
ERROR  [Vue warn]: toRefs() expects a reactive object but received a plain one.

🌈 Expected behaviour
No warnings

ℹ️ Additional context

Package versions:

"nuxt": "^2.14.5",
"vue": "^2.6.12",
"@nuxtjs/composition-api": "^0.12.5",

Nuxt mode is universal

@vaiil vaiil added the bug Something isn't working label Sep 21, 2020
@TheAlexLichter
Copy link
Member

Can reproduce when solely using useMeta.

composition-api/src/meta.ts

Lines 110 to 114 in dc6f253

const refs = toRefs(_head) as ToRefs<
ReturnType<typeof createEmptyMeta> & {
titleTemplate: ReactiveHead['titleTemplate']
} & T
>

@danielroe Would it makes sense to entirely skip the toRefs here or maybe checking via isReactive first?

@danielroe
Copy link
Member

This is an upstream bug (reported).

@danielroe danielroe added upstream and removed bug Something isn't working labels Sep 28, 2020
antfu added a commit to antfu/composition-api-1 that referenced this issue Oct 4, 2020
@bissolli
Copy link

bissolli commented Oct 5, 2020

Guys, would this bug affect the metatags on SSR?

My metatags are generated dynamically based on the first segment of the URL, and I just did a refactoring where I switched my layout from Options API to Composition API and I started getting a very weird behavior.

Let's say, this first segment is a business that uses my platform and it's is used to compose the title page like below:

Access: /carrefour
Title should be: Order from Carrefour

Access: /mini-market
Title should be: Order from Mini Market

But weirdly, in the document sent by the SSR, it comes with the wrong business in the title and later on after client hydration it gets updated to the correct business - and because of that I definitely can't go live with the refactoring πŸ˜“

Would it be related? Any guesses if not?

As an example, all I do is that:

layouts/default.vue

<template>
  <div>
    <nuxt />
  </div>
</template>

<script lang="ts">
import { defineComponent, useContext, useMeta } from '@nuxtjs/composition-api'

export default defineComponent({
  name: 'LayoutsDefault',
  head: {},
  setup(_, { root }) {
    const { store } = useContext()

    // the business is loaded in the nuxtServerInit based on the first URL segment and feed into the vuex store.
    const business = store.getters['business/getData']

    const nuxtI18nSeo = root.$nuxtI18nSeo()
    const title = `Order from ${business.name}`

    useMeta({
      title,
      htmlAttrs: {
        ...nuxtI18nSeo.htmlAttrs,
      },
      meta: [
        // ... my own metatags
        ...(nuxtI18nSeo.meta ?? []),
      ],
      link: [
        // ... my own links
        ...(nuxtI18nSeo.link ?? []),
      ],
    })

    return {}
  },
})
</script>

I also get this [Vue warn]: toRefs() expects a reactive object but received a plain one. during SSR.

@bissolli
Copy link

bissolli commented Oct 5, 2020

Guys I was able to reproduce the error above in development mode, and basically if I keep 2 browser tabs open one with /carrefour and another with /mini-market and start refreshing both at the same time - that is what happens in production due to the high movement in the app - somehow the data fetch from vuex gets mixed and the markup response from the document comes with wrong metatags.

If I just replace this

  setup(_, { root }) {
    const { store } = useContext()

    // the business is loaded in the nuxtServerInit based on the first URL segment and feed into the vuex store.
    const business = store.getters['business/getData'] 

for this

  setup(_, { root }) {
    // the business is loaded in the nuxtServerInit based on the first URL segment and feed into the vuex store.
    const business = root.$store.getters['business/getData'] 

The error will go away.

Not sure if it is really related to the fix #251.

@danielroe
Copy link
Member

@bissolli The issue you describe sounds like the same issue as nuxt/nuxt#7696 with pending PR: nuxt/nuxt#8132

@bissolli
Copy link

bissolli commented Oct 6, 2020

@bissolli The issue you describe sounds like the same issue as nuxt/nuxt.js#7696 with pending PR: nuxt/nuxt.js#8132

That is exactly what I am facing! Thanks helping out =)
Until it's fixed I have to avoid using the context to generate my metas.

danielroe added a commit that referenced this issue Oct 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants