Api

useRobotsRule()

Last updated by
Harlan Wilton
in chore: broken tests.

Introduction

Type: function useRobotsRule(rule?: MaybeRef<boolean | string | Partial<RobotDirectives>>): Ref<string>

View and control the robots rule using a simple reactivity API. Supports standard directives (index, noindex, follow, nofollow) and non-standard directives like noai and noimageai.

It's recommended to use this composable when you need to dynamically change the robots rule at runtime. For example when a user changes their profile from private to public.

Note: This does not modify the /robots.txt file, only the X-Robots-Tag header and the robots meta tag.

Server Side Behavior

In a server-side context, this can be used to change the rule used for X-Robots-Tag header and the robots meta tag.

Providing a boolean will either enable or disable indexing for the current path using the default rules.

import { useRobotsRule } from '#imports'

const rule = useRobotsRule(true) // modifies the rules

Client Side Behavior

In a client-side context you can only read the value of the rule, modifying it will have no effect. This is due to robots only respecting the initial SSR response.

import { useRobotsRule } from '#imports'

const rule = useRobotsRule(true) // does not do anything, just returns the value

Available Directives

When using the object syntax, you can use the following directives:

Standard Directives

  • index: Allow search engines to index the page
  • noindex: Prevent search engines from indexing the page
  • follow: Allow search engines to follow links on the page
  • nofollow: Prevent search engines from following links on the page
  • none: Equivalent to noindex, nofollow
  • all: Equivalent to index, follow

Non-Standard Directives

  • noai: Request AI crawlers not to use content for training
  • noimageai: Request AI crawlers not to use images for training

Preview Control Directives

  • max-image-preview: Controls image preview size ('none', 'standard', or 'large')
  • max-snippet: Controls text snippet length in characters (use -1 for no limit)
  • max-video-preview: Controls video preview length in seconds (use -1 for no limit)

Usage

Accessing the rule:

import { useRobotsRule } from '#imports'

const rule = useRobotsRule()
// Ref<'noindex, nofollow'>

Setting the rule - argument:

import { useRobotsRule } from '#imports'

useRobotsRule('index, nofollow')
// Ref<'index, nofollow'>
useRobotsRule(false)
// Ref<'noindex, nofollow'>

Setting the rule - reactive:

import { useRobotsRule } from '#imports'

const rule = useRobotsRule()
rule.value = 'index, nofollow'
// Ref<'index, nofollow'>

Setting the rule - object syntax:

import { useRobotsRule } from '#imports'

// Using object syntax for directives
useRobotsRule({ noindex: true, nofollow: true })
// Ref<'noindex, nofollow'>

// Combining standard and non-standard directives
useRobotsRule({ index: true, noai: true, noimageai: true })
// Ref<'index, noai, noimageai'>

// Only true values are included
useRobotsRule({ index: true, follow: false, noai: true })
// Ref<'index, noai'>

// Empty object defaults to enabled value
useRobotsRule({})
// Ref<'index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1'>

Setting the rule - with max- directives:*

import { useRobotsRule } from '#imports'

// Control search result preview settings
useRobotsRule({
  'index': true,
  'max-image-preview': 'large', // 'none', 'standard', or 'large'
  'max-snippet': 150, // number of characters
  'max-video-preview': 30 // seconds of video preview
})
// Ref<'index, max-image-preview:large, max-snippet:150, max-video-preview:30'>

// Disable all previews
useRobotsRule({
  'index': true,
  'max-image-preview': 'none',
  'max-snippet': 0,
  'max-video-preview': 0
})
// Ref<'index, max-image-preview:none, max-snippet:0, max-video-preview:0'>
Did this page help you?