You're viewing Nuxt Scripts v0 (stable) documentation. View latest (v1 beta) docs →
Api

useScriptTriggerConsent

Load a script once consent has been provided either through a resolvable consent or calling the accept method.

The trigger by itself does nothing - scripts will only load when consent is granted through either the accept() method or by providing a consent option that resolves to true.

Signature

function useScriptTriggerConsent(options?: ConsentScriptTriggerOptions): UseConsentScriptTriggerApi {}

Arguments

export interface ConsentScriptTriggerOptions {
  /**
   * An optional reactive (or promise) reference to the consent state. You can use this to accept the consent for scripts
   * instead of using the accept() method.
   */
  consent?: Promise<boolean | void> | Ref<boolean> | ComputedRef<boolean> | boolean
  /**
   * Should the script be loaded on the `requestIdleCallback` callback. This is useful for non-essential scripts that
   * have already been consented to be loaded.
   */
  postConsentTrigger?: NuxtUseScriptOptions['trigger']
}

Returns

A extended promise api with an accept method to accept the consent and load the script.

interface UseConsentScriptTriggerApi extends Promise<void> {
  /**
   * A function that can be called to accept the consent and load the script.
   */
  accept: () => void
}

Examples

Basic Usage

app.vue
<script setup lang="ts">
const trigger = useScriptTriggerConsent()
useScript('https://example.com/script.js', { trigger })
</script>

<template>
  <button @click="trigger.accept">
    Accept Consent
  </button>
</template>