---
title: "tags:beforeResolve Hook · v2 · Unhead"
meta:
  "og:description": "Learn about the tags:beforeResolve hook in Unhead that runs before the tag resolution process"
  "og:title": "tags:beforeResolve Hook · v2 · Unhead"
  description: "Learn about the tags:beforeResolve hook in Unhead that runs before the tag resolution process"
---

**Hooks**

# **tags:beforeResolve Hook**

**On this page **

- [Lifecycle Position](#lifecycle-position)
- [Hook Signature](#hook-signature)
- [Usage Example](#usage-example)
- [Use Cases](#use-cases)

The `tags:beforeResolve` hook is called just before the tag resolution process begins. This hook provides access to all tags that have been collected from entries and allows for bulk modifications before the main resolution process.

## [Lifecycle Position](#lifecycle-position)

This hook runs after [`tag:normalise`](https://unhead.unjs.io/docs/v2/head/api/hooks/tag-normalise) and before [`tags:resolve`](https://unhead.unjs.io/docs/v2/head/api/hooks/tags-resolve).

## [Hook Signature](#hook-signature)

```
export interface Hook {
  'tags:beforeResolve': (ctx: TagResolveContext) => HookResult
}
```

### [Parameters](#parameters)

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| `ctx` | `TagResolveContext` | Context object with the collection of tags |

The `TagResolveContext` interface is defined as:

```
interface TagResolveContext {
  tagMap: Map<string, HeadTag>
  tags: HeadTag[]
}
```

### [Returns](#returns)

`HookResult` which is either `void` or `Promise<void>`

## [Usage Example](#usage-example)

```
import { createHead } from '#imports'

const head = createHead({
  hooks: {
    'tags:beforeResolve': (ctx) => {
      // Log all tags before resolution
      console.log(\`Processing ${ctx.tags.length} tags before resolution\`)

      // Add a tag that should be included in all pages
      ctx.tags.push({
        tag: 'meta',
        props: {
          name: 'generator',
          content: 'Unhead'
        }
      })
    }
  }
})
```

## [Use Cases](#use-cases)

### [Pre-processing Tags](#pre-processing-tags)

You can use this hook to pre-process tags before the main resolution:

```
import { defineHeadPlugin } from '#imports'

export const tagsPreprocessPlugin = defineHeadPlugin({
  hooks: {
    'tags:beforeResolve': (ctx) => {
      // Sort tags by priority before resolution
      ctx.tags.sort((a, b) => {
        const aPriority = a.tagPriority === 'high'
          ? 0
          : a.tagPriority === 'critical' ? -1 : 1
        const bPriority = b.tagPriority === 'high'
          ? 0
          : b.tagPriority === 'critical' ? -1 : 1
        return aPriority - bPriority
      })
    }
  }
})
```

### [Adding Global Tags](#adding-global-tags)

This hook is ideal for adding tags that should be present on all pages:

```
import { defineHeadPlugin } from '#imports'

export const globalTagsPlugin = defineHeadPlugin({
  hooks: {
    'tags:beforeResolve': (ctx) => {
      // Add global meta tags
      const globalTags = [
        {
          tag: 'meta',
          props: {
            charset: 'utf-8'
          }
        },
        {
          tag: 'meta',
          props: {
            name: 'viewport',
            content: 'width=device-width, initial-scale=1'
          }
        },
        {
          tag: 'link',
          props: {
            rel: 'icon',
            href: '/favicon.ico'
          }
        }
      ]

      ctx.tags.push(...globalTags)
    }
  }
})
```

### [Filtering Tags Based on Environment](#filtering-tags-based-on-environment)

Filter or modify tags based on the current environment:

```
import { defineHeadPlugin } from '#imports'

export const environmentFilterPlugin = defineHeadPlugin((head) => {
  return {
    hooks: {
      'tags:beforeResolve': (ctx) => {
        const isProduction = process.env.NODE_ENV === 'production'

        // Remove development-only tags in production
        if (isProduction) {
          ctx.tags = ctx.tags.filter((tag) => {
            // Remove development-specific meta tags
            if (tag.tag === 'meta' && tag.props.name === 'robots'
              && tag.props.content === 'noindex, nofollow') {
              return false
            }

            // Remove debug scripts
            if (tag.tag === 'script' && tag.props['data-debug']) {
              return false
            }

            return true
          })
        }
      }
    }
  }
})
```

[Edit this page](https://github.com/unjs/unhead/edit/v2.1.2/docs/v2/head/7.api/hooks/06.tags-before-resolve.md)

**Did this page help you? **

[**tag:normalise** Learn about the tag:normalise hook in Unhead that processes individual tags before rendering](https://unhead.unjs.io/docs/v2/head/api/hooks/tag-normalise) [**tags:resolve** Learn about the tags:resolve hook in Unhead that processes tags during the main resolution phase](https://unhead.unjs.io/docs/v2/head/api/hooks/tags-resolve)

**On this page **

- [Lifecycle Position](#lifecycle-position)
- [Hook Signature](#hook-signature)
- [Usage Example](#usage-example)
- [Use Cases](#use-cases)