-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide access to global
setup()
function
- Loading branch information
Showing
10 changed files
with
144 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
--- | ||
|
||
# onGlobalSetup | ||
|
||
This helper will run a callback function in the global setup function. | ||
|
||
```ts | ||
import { onGlobalSetup } from 'nuxt-composition-api' | ||
|
||
export default () => { | ||
onGlobalSetup(() => { | ||
provide('globalKey', true) | ||
}) | ||
} | ||
``` | ||
|
||
::: warning | ||
This should be called from within a plugin rather than in a component context. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Plugin } from '@nuxt/types' | ||
import type { SetupContext } from '@vue/composition-api' | ||
|
||
import { setSSRContext } from './ssr-ref' | ||
|
||
type SetupFunction = ( | ||
this: void, | ||
props: Record<string, unknown>, | ||
ctx: SetupContext | ||
) => void | Record<any, any> | ||
|
||
const globalSetup = new Set<SetupFunction>() | ||
|
||
/** | ||
* Run a callback function in the global setup function. This should be called from a Nuxt plugin. | ||
* @param fn The function to run in the setup function. It receives the global props and context. | ||
* @example | ||
```ts | ||
import { onGlobalSetup } from 'nuxt-composition-api' | ||
export default () => { | ||
onGlobalSetup(() => { | ||
provide('globalKey', true) | ||
}) | ||
} | ||
``` | ||
*/ | ||
export const onGlobalSetup = (fn: SetupFunction) => { | ||
globalSetup.add(fn) | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
export const globalPlugin: Plugin = context => { | ||
const { setup } = context.app | ||
context.app.setup = (...args) => { | ||
let result = {} | ||
if (setup instanceof Function) { | ||
result = { ...setup(...args) } | ||
} | ||
for (const fn of globalSetup) { | ||
result = { ...result, ...(fn(...args) || {}) } | ||
} | ||
return result | ||
} | ||
|
||
if (!process.server) return | ||
if (context.app.context.ssrContext) { | ||
setSSRContext(context.app.context.ssrContext) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Selector } from 'testcafe' | ||
import { navigateTo, expectOnPage } from './helpers' | ||
|
||
// eslint-disable-next-line | ||
fixture`onGlobalSetup` | ||
|
||
const assertions = async () => { | ||
await expectOnPage('global setup was run on server') | ||
await expectOnPage('global setup was run on client') | ||
await expectOnPage('globally injected value was received') | ||
} | ||
|
||
test('Runs plugin on server side page', async () => { | ||
await navigateTo('/hooks') | ||
await assertions() | ||
}) | ||
|
||
test('Runs plugin on client rendered page', async t => { | ||
await navigateTo('/') | ||
await t.click(Selector('a').withText('hooks')) | ||
await assertions() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<template> | ||
<blockquote> | ||
<p> | ||
<code>global setup was {{ ranSsr ? 'run' : 'not run' }} on server</code> | ||
<br /> | ||
<code>global setup was {{ ran ? 'run' : 'not run' }} on client</code> | ||
<br /> | ||
<code> | ||
globally injected value was | ||
{{ globalInject ? 'received' : 'not received' }} | ||
</code> | ||
</p> | ||
</blockquote> | ||
</template> | ||
|
||
<script> | ||
import { defineComponent, inject } from 'nuxt-composition-api' | ||
import { ran, ranSsr } from '../plugins/global' | ||
export default defineComponent({ | ||
setup() { | ||
const globalInject = inject('globalKey', false) | ||
return { globalInject, ran, ranSsr } | ||
}, | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { onGlobalSetup, provide, ref, ssrRef } from 'nuxt-composition-api' | ||
|
||
export const ranSsr = ssrRef(false) | ||
export const ran = ref(false) | ||
|
||
export default () => { | ||
onGlobalSetup(() => { | ||
ran.value = true | ||
ranSsr.value = true | ||
|
||
provide('globalKey', true) | ||
|
||
return { | ||
ran, | ||
ranSsr, | ||
} | ||
}) | ||
} |