Skip to content
This repository has been 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 opened this issue Jul 28, 2022 · 0 comments
Open

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

gumacs92 opened this issue Jul 28, 2022 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@gumacs92
Copy link

gumacs92 commented Jul 28, 2022

πŸ› 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.

@gumacs92 gumacs92 added the bug Something isn't working label Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants