Skip to content

Commit f61e2f5

Browse files
committed
feat: Add tagPosition parameter
1 parent d63c17c commit f61e2f5

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

cypress/e2e/test.cy.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,16 @@ describe('nuxt-jsonld', () => {
9191
expect(json).to.have.property('count', 2);
9292
});
9393
});
94+
95+
it('places jsonld with tagPosition', () => {
96+
cy.visit('/composable-options');
97+
cy.get('body script[type="application/ld+json"]')
98+
.should('exist')
99+
.then((el) => {
100+
const json = JSON.parse(el.text());
101+
expect(json).to.have.property('@context', 'https://schema.org');
102+
expect(json).to.have.property('@type', 'WebSite');
103+
expect(json).to.have.property('name', 'nuxt-jsonld composable options');
104+
});
105+
});
94106
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
<h1>Composable Options</h1>
4+
</div>
5+
</template>
6+
7+
<script lang="ts">
8+
export default defineComponent({
9+
setup() {
10+
useJsonld(
11+
() => {
12+
return {
13+
'@context': 'https://schema.org',
14+
'@type': 'WebSite',
15+
name: 'nuxt-jsonld composable options',
16+
};
17+
},
18+
{
19+
tagPosition: 'bodyClose',
20+
}
21+
);
22+
},
23+
});
24+
</script>

packages/example/pages/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</li>
1111
<li><nuxt-link :to="{ name: 'static' }">Static JSON</nuxt-link></li>
1212
<li><nuxt-link :to="{ name: 'option' }">Options API</nuxt-link></li>
13+
<li><nuxt-link :to="{ name: 'composable-options' }">Composable API Options</nuxt-link></li>
1314
</ul>
1415
</div>
1516
</template>

packages/nuxt-jsonld/src/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { defineNuxtModule, addPlugin, addImports } from '@nuxt/kit';
33
import type { Nuxt } from '@nuxt/schema';
44
import type { JsonLDFunc } from './types';
55

6+
export type { UseJsonldOptions } from './runtime/composable';
7+
68
type ModuleOptions = {
79
disableOptionsAPI: boolean;
810
};

packages/nuxt-jsonld/src/runtime/composable.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { computed } from 'vue';
22
import type { JsonLD, JsonLDFunc } from '../types';
3-
import { useHead } from '@unhead/vue';
3+
import { useHead, type UseHeadOptions } from '@unhead/vue';
44

55
const isFunc = (json: JsonLD | JsonLDFunc): json is JsonLDFunc => typeof json === 'function';
6+
export type UseJsonldOptions = Pick<UseHeadOptions, 'tagPosition'>;
67

7-
export const useJsonld = (json: JsonLD | JsonLDFunc) => {
8+
export const useJsonld = (json: JsonLD | JsonLDFunc, options?: UseJsonldOptions) => {
89
if (!json) {
910
return;
1011
}
@@ -22,5 +23,5 @@ export const useJsonld = (json: JsonLD | JsonLDFunc) => {
2223
},
2324
],
2425
};
25-
});
26+
}, options);
2627
};

0 commit comments

Comments
 (0)