Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants