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

Commit

Permalink
fix: reinitialise global setup callbacks on each request
Browse files Browse the repository at this point in the history
closes #270
  • Loading branch information
danielroe committed Oct 15, 2020
1 parent 50b74da commit 7816afe
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type SetupFunction = (
ctx: SetupContext
) => void | Record<any, any>

const globalSetup = new Set<SetupFunction>()
let globalSetup: Set<SetupFunction>

/**
* Run a callback function in the global setup function. This should be called from a Nuxt plugin.
Expand All @@ -34,6 +34,7 @@ export const onGlobalSetup = (fn: SetupFunction) => {
*/
export const globalPlugin: Plugin = context => {
const { setup } = context.app
globalSetup = new Set<SetupFunction>()
context.app.setup = (...args) => {
let result = {}
if (setup instanceof Function) {
Expand Down
7 changes: 5 additions & 2 deletions test/e2e/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { navigateTo, expectOnPage } from './helpers'
fixture`onGlobalSetup`

const assertions = async () => {
await expectOnPage('global setup was run on server')
await expectOnPage('global setup was run on client')
await expectOnPage('global setup was run 1 times on server')
await expectOnPage('global setup was run 1 times on client')
await expectOnPage('globally injected value was received')
}

test('Runs plugin on server side page', async () => {
await navigateTo('/hooks')
await assertions()

await navigateTo('/hooks')
await assertions()
})

test('Runs plugin on client rendered page', async t => {
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/pages/hooks.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<blockquote>
<p>
<code>global setup was {{ ranSsr ? 'run' : 'not run' }} on server</code>
<code>global setup was {{ ranSsr ? 'run' : 'not run' }} {{ ranSsr }} times on server</code>
<br />
<code>global setup was {{ ran ? 'run' : 'not run' }} on client</code>
<code>global setup was {{ ran ? 'run' : 'not run' }} {{ ran }} times on client</code>
<br />
<code>
globally injected value was
Expand Down
11 changes: 7 additions & 4 deletions test/fixture/plugins/global.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { onGlobalSetup, provide, ref, ssrRef } from '@nuxtjs/composition-api'

export const ranSsr = ssrRef(false)
export const ran = ref(false)
export const ranSsr = ssrRef(0)
export const ran = ref(0)

export default () => {
ran.value = 0
if (process.server) ranSsr.value = 0

onGlobalSetup(() => {
ran.value = true
ranSsr.value = true
ran.value++
if (process.server) ranSsr.value++

provide('globalKey', true)

Expand Down

0 comments on commit 7816afe

Please sign in to comment.