Skip to content

Commit

Permalink
doc: misc API
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Apr 30, 2024
1 parent cdd749c commit 123d0b0
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/content/docs/1.guides/2.bundling.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ useScript('https://example.com/script.js', {
bundle: true,
})
```

## Asset Config

- Type: `object`
- Default: `{ prefix: '/_scripts/', strategy: 'public' }`

Controls the way scripts are bundled to be served by Nuxt.
19 changes: 17 additions & 2 deletions docs/content/docs/3.api/1.use-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ title: useScript
description: Use lifecycle hooks to stay synced with Nuxt Scripts.
---

- TODO nuxt specific script options
This composable is a wrapper around the Unhead [useScript](https://unhead.unjs.io/usage/composables/use-script) with extra Nuxt goodies on top, for
full documentation please refer to this.

## Signature

```ts
function useScript<T extends Record<string | symbol, any>>(input: UseScriptInput, options?: NuxtUseScriptOptions): UseScriptReturn<T> {}
```

## NuxtUseScriptOptions

### `trigger`

- Type: `string`
- Default: `onNuxtReady` (set from the [defaultScriptOptions](/) Nuxt config)

The trigger to load the script. This can be any of the following:

Check failure on line 23 in docs/content/docs/3.api/1.use-script.md

View workflow job for this annotation

GitHub Actions / ci

Too many blank lines at the end of file. Max of 0 allowed
Please see the [useScript](https://unhead.unjs.io/usage/composables/use-script) documentation.
51 changes: 51 additions & 0 deletions docs/content/docs/3.api/5.nuxt-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Nuxt Config
description: Configure Nuxt Scripts using your Nuxt Config.
---

## `registry`

- Type: `ScriptRegistry`

Global registry scripts that should be loaded.

Check failure on line 10 in docs/content/docs/3.api/5.nuxt-config.md

View workflow job for this annotation

GitHub Actions / ci

Trailing spaces not allowed

See the [Script Registry](/scripts) for more details.

## `defaultScriptOptions`

- Type: `NuxtUseScriptOptions`
- Default: `{ trigger: 'onNuxtReady' }`

Default options for scripts. See the [useScript](/use-script) documentation for more details.

## `globals`

- Type: `(NuxtUseScriptInput | [NuxtUseScriptInput, NuxtUseScriptOptions])[]`
- Default: `[]`

Global scripts that should be loaded on all pages. This is a configuration for the [useScript](/use-script) composable.

See the [Globals](/guides/global) documentation for more details.

## `assets`

- Type: `object`
- Default: `{ prefix: '/_scripts/', strategy: 'public' }`

Controls the way scripts are bundled to be served by Nuxt.

See the [Bundling](/guides/bundling) documentation for more details.

## `enabled`

- Type: `boolean`
- Default: `true`

Disables the Nuxt Scripts module.

## `debug`

- Type: `boolean`
- Default: `false`

Enable to see debug logs.
43 changes: 43 additions & 0 deletions docs/content/docs/3.api/6.nuxt-app-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: Nuxt App Hooks
description: Use Nuxt App hooks to extend the Nuxt Scripts runtime behavior.
---

## `scripts:updated`

- Type: `async (ctx: { scripts: ScriptRegistry }) => HookResult`

Triggered after the script status is updated.

This is used internally for the DevTools but can be used however you see fit.

```ts [plugins/nuxt-scripts.ts]
export default defineNuxtPlugin({
setup() {
useNuxtApp().hooks.hook('scripts:updated', (ctx) => {
console.log('Scripts updated', ctx.scripts)
})
}
})
```

## `script:instance-fn`

- Type: `(ctx: { script: ScriptInstance<any>, fn: string | symbol, args: any, exists: boolean }) => HookResult`

This is exposed only from Unhead, it's fired when accessing properties via the proxy instance.

This is also used internally for the DevTools but can be used however you see fit.

```ts
export default defineNuxtPlugin({
setup() {
const head = injectHead()
head.hooks.hook('script:instance-fn', ({ fn, args }) => {
console.log('Function called:', ctx)
})
const { doSomething } = useScript()
doSomething() // Function called: doSomething
}
})
```
33 changes: 33 additions & 0 deletions docs/content/docs/3.api/6.nuxt-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Nuxt Hooks
description: Use Nuxt hooks to extend the Nuxt Scripts module.
---

## `scripts:registry`

- Type: `async (ctx: { registry: ScriptRegistry }) => HookResult`

Add registry scripts at build, allowing them to be loaded via `scripts.registry` and bundled if available.

Check failure on line 10 in docs/content/docs/3.api/6.nuxt-hooks.md

View workflow job for this annotation

GitHub Actions / ci

Trailing spaces not allowed

This is intended to be used by modules.

```ts [module.ts]
export default defineNuxtModule({
setup() {
nuxt.hooks.hook('scripts:registry', async (ctx) => {
ctx.registry.add({
// used in DevTools
label: 'My Custom Script',
logo: `<svg class="w-10 h-10" xmlns="http://www.w3.org/2000/svg" width="28.85" height="32" viewBox="0 0 256 284"><path fill="#F9AB00" d="M256.003 247.933a35.224 35.224 0 0 1-39.376 35.161c-18.044-2.67-31.266-18.371-30.826-36.606V36.845C185.365 18.591 198.62 2.881 216.687.24a35.221 35.221 0 0 1 39.316 35.16z"/><path fill="#E37400" d="M35.101 213.193c19.386 0 35.101 15.716 35.101 35.101c0 19.386-15.715 35.101-35.101 35.101S0 267.68 0 248.295c0-19.386 15.715-35.102 35.101-35.102m92.358-106.387c-19.477 1.068-34.59 17.406-34.137 36.908v94.285c0 25.588 11.259 41.122 27.755 44.433a35.161 35.161 0 0 0 42.146-34.56V142.089a35.222 35.222 0 0 0-35.764-35.282"/></svg>`,
// if the script can be bundled we need to define a resolver
scriptBundling: 'https://cdn.jsdelivr.net/npm/[email protected]',
// how to load the script, will be added as an auto import
import: {
name: 'useScriptMyCustomScript',
from: resolve('./runtime/scripts/my-custom-script'),
},
})
})
},
})
```
2 changes: 1 addition & 1 deletion docs/content/docs/3.api/_dir.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
icon: i-ph-star-duotone
title: API
title: Nuxt API

0 comments on commit 123d0b0

Please sign in to comment.