Skip to content

Commit

Permalink
feat: integrate with @nuxtjs/robots
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 10, 2024
1 parent 0b3d66c commit dda8eb5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/content/1.integrations/2.robots.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Nuxt Robots
description: How to use the Nuxt Robots module with Nuxt Content.
---

### Respecting Non-Indexable Pages

When using the [@nuxtjs/robots](https://github.com/nuxt-modules/robots), the `useSchemaOrg` composable will verify
the page you're rendering Schema.org is indexable by search engines.

If the page is not indexable, it will not add Schema.org to the page.

For more details see the guide on [Disabling Page Indexing](/robots/guides/disable-page-indexing).
2 changes: 2 additions & 0 deletions playground/_robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-Agent: *
Disallow: /blocked
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineNuxtConfig({
Module,
'@nuxt/ui',
'@nuxtjs/i18n',
'@nuxtjs/robots',
/**
* Start a sub Nuxt Server for developing the client
*
Expand Down Expand Up @@ -69,4 +70,6 @@ export default defineNuxtConfig({
failOnError: false,
},
},

compatibilityDate: '2024-09-10',
})
9 changes: 9 additions & 0 deletions playground/pages/blocked.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup lang="ts">
import { useRobotsRule } from '#imports'
useRobotsRule(false)
</script>

<template>
<div>blocked</div>
</template>
7 changes: 6 additions & 1 deletion src/runtime/nuxt/imports/useSchemaOrg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useHead, useRuntimeConfig, useServerHead } from '#imports'
import { useHead, useRequestEvent, useRuntimeConfig, useServerHead } from '#imports'
import type { ActiveHeadEntry, DataKeys, ScriptBase, TagUserProperties } from '@unhead/schema'
import type { useSchemaOrg as _useSchemaOrg } from '@unhead/schema-org/vue'
import type { ModuleRuntimeConfig, UnheadAugmentation } from '../../types'
Expand All @@ -14,6 +14,11 @@ export function useSchemaOrg<T extends Input>(input: T): ActiveHeadEntry<UnheadA
...config?.scriptAttributes || {},
}
if (import.meta.server) {
const event = useRequestEvent()
// don't bother rendering schema.org if the page is not indexable
if (typeof event?.context.robots !== 'undefined' && !event.context.robots?.indexable) {
return
}
// we don't need to use the direct composable as the plugin is already registered
return useServerHead<UnheadAugmentation<T>>({
script: [script],
Expand Down

0 comments on commit dda8eb5

Please sign in to comment.