Scripts

Gravatar provides globally recognized avatars linked to email addresses. Nuxt Scripts provides a privacy-preserving integration that proxies avatar requests through your own server, preventing Gravatar from tracking your users.

Gravatar

View source

Nuxt Config Setup

The simplest way to load Gravatar globally in your Nuxt App is to use Nuxt config. Alternatively you can directly use the useScriptGravatar composable.

export default defineNuxtConfig({
  scripts: {
    registry: {
      gravatar: true
    }
  }
})

useScriptGravatar()

The useScriptGravatar composable lets you have fine-grain control over when and how Gravatar is loaded on your site.

const { proxy } = useScriptGravatar()

// renders Gravatar avatar via component

Please follow the Registry Scripts guide to learn more about advanced usage.

First-Party Mode

This script supports First-Party Mode which is auto-enabled, routing all traffic through your domain for improved privacy and ad blocker bypass.

export default defineNuxtConfig({
  scripts: {
    registry: {
      gravatar: true
    }
  }
})

Example

Using Gravatar only in production while using the proxy to send events.

<script setup lang="ts">
const { proxy } = useScriptGravatar()

// noop in development, ssr
// just works in production, client
function handleAction() {
  // renders Gravatar avatar via component
}
</script>

<template>
  <div>
    <button @click="handleAction">
      Send Event
    </button>
  </div>
</template>

<ScriptGravatar>

The <ScriptGravatar> component renders a Gravatar avatar for a given email address. All requests are proxied through your server - Gravatar never sees your user's IP address or headers.

Demo

[email protected]

80×80px · Proxied through your server

Component API

See the Facade Component API for full props, events, and slots.

Props

PropTypeDefaultDescription
emailstring,Email address, sent to your server proxy for hashing, not sent to Gravatar
hashstring-Pre-computed SHA256 hash of the email (alternative to email)
sizenumber80Avatar size in pixels
defaultstring'mp'Default avatar style when no Gravatar exists
ratingstring'g'Content rating filter
hovercardsbooleanfalseEnable hovercards on hover

useScriptGravatar()

The useScriptGravatar() composable lets you interact with the Gravatar API programmatically.

export function useScriptGravatar<T extends GravatarApi>(_options?: GravatarInput) {}

Please follow the Registry Scripts guide to learn more about advanced usage.

cacheMaxAgenumber = 3600

Cache duration for proxied avatar images in seconds.

defaultstring = 'mp'

Default image to show when no Gravatar exists.

sizenumber = 80

Avatar size in pixels (1-2048).

ratingstring = 'g'

Content rating filter.

Example

Using the composable to get avatar URLs directly.

<script setup lang="ts">
const { onLoaded } = useScriptGravatar()

const avatarUrl = ref('')

onLoaded((api) => {
  avatarUrl.value = api.getAvatarUrlFromEmail('[email protected]', { size: 120 })
})
</script>

<template>
  <img v-if="avatarUrl" :src="avatarUrl" alt="User avatar">
</template>