Skip to content

Cannot read property 'ssrContext' of undefined #5941

@liamwang

Description

@liamwang

Version

2.3.4

Reproduction link

https://github.com/vuejs/vue-hackernews-2.0/blob/master/src/util/title.js#L13

Steps to reproduce

  1. git clone https://github.com/vuejs/vue-hackernews-2.0.git

  2. Change /src/util/title.js file like this:

const serverTitleMixin = {
  created() {
    /* before change */
    // const title = getTitle(this)
    // if (title) {
    //   this.$ssrContext.title = `Vue HN 2.0 | ${title}`
    // }

    /* after change */
    const title = getTitle(this) || 'Vue HN 2.0'
    if (this.$ssrContext) {
      this.$ssrContext.title = title
    }
  }
}

According to functional and robustness, the changed code should be ok as normal thinking.
按照正常的编程思路,修改后的代码应该更加严谨才是

  1. npm install & npm run dev

  2. open browser, enter http://localhost:8080

What is expected?

The if condition statement ensured this.$ssrContext to be not undefined when trying to set title a value on it. It shoud be ok as normal expected.

What is actually happening?

Error trace in console:

[Vue warn]: Error in created hook: "TypeError: Cannot read property 'ssrContext' of undefined"

(found in <Root>)
error during render : /
TypeError: Cannot read property 'ssrContext' of undefined
    at Vue$3.get (D:\Temp\vue-demos\vue-hackernews-2.0\node_modules\vue\dist\vue.runtime.common.js:4423:23)
    at Vue$3.created (src/util/title.js:20:21)
    at callHook (D:\Temp\vue-demos\vue-hackernews-2.0\node_modules\vue\dist\vue.runtime.common.js:2554:21)
    at Vue$3.Vue._init (D:\Temp\vue-demos\vue-hackernews-2.0\node_modules\vue\dist\vue.runtime.common.js:3998:5)
    at new Vue$3 (D:\Temp\vue-demos\vue-hackernews-2.0\node_modules\vue\dist\vue.runtime.common.js:4093:8)
    at new Store (D:\Temp\vue-demos\vue-hackernews-2.0\node_modules\vuex\dist\vuex.js:264:21)
    at createStore (src/store/index.js:10:9)
    at createApp (src/app.js:21:16)
    at module.exports.__webpack_exports__.default (src/entry-server.js:13:35)
    at module.exports.__webpack_exports__.default (src/entry-server.js:11:9)

Error in browser:

500 | Internal Server Error


I suppose it is a bug of vue-server-renderer. If not, what happened?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions