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

fix: functions passed into beforeNuxtRender are not getting calledΒ #659

Open
@gumacs92

Description

@gumacs92

πŸ› The bug
I think the beforeNuxtRender is not getting called at the right place when it is placed in the setup function.

πŸ› οΈ To reproduce
I'm using this code:

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

export default defineComponent({
  layout: 'empty', //this is just a totally empty layout with <div><Nuxt /></div> in it
  setup () {
    const { ssrContext, beforeNuxtRender } = useContext()
    if (process.server) {
      //printing the functions before injection
      console.log('in setup before declaring function', ssrContext.beforeRenderFns)
      //passing the function the normal way
      beforeNuxtRender(() => {
        console.log('beforeRenderFn1')
      })
      //injecting the function directly in the beforeRenderFns
      ssrContext.beforeRenderFns.push(() => {
        console.log('beforeRenderFn2')
      })
      //printing the functions after injection
      console.log('in setup after declaring function', ssrContext.beforeRenderFns)
    }
  }
})
</script>

And in the generated server.js I've place two console.log statment like this:

export default async (ssrContext) => {
  // ... everthing else before

  console.log('before beforeRender', ssrContext.beforeRenderFns)
  // Call beforeNuxtRender methods & add store state
  await beforeRender()
  console.log('after beforeRender', ssrContext.beforeRenderFns)

  return _app
}

After this, the printed server output is the following:

before beforeRender []                                                                                                                                                                                                                                             08:58:47  
after beforeRender []                                                                                                                                                                                                                                              08:58:47  
in setup before declaring function []                                                                                                                                                                                                                              08:58:47  
in setup after declaring function [                                                                                                                                                                                                                                08:58:47  
  [Function (anonymous)],
  [Function (anonymous)]
]

The frontend not printing 'beforeRenderFn1' or 'beforeRenderFn2' either.

🌈 Expected behaviour
The function which had been passed into beforeNuxtRender should be called when the server.js calls the beforeRender and all the beforeRenderFns. The console output should be reversed: the in setup part should come first and the beforeRender part after. According to this closed issue it has been also suggested that is should work when placed in the setup function.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions