Skip to content

Commit

Permalink
feat: Add tagPosition parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
resessh committed Nov 27, 2024
1 parent d8217a9 commit 244b44a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
12 changes: 12 additions & 0 deletions cypress/e2e/test.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,16 @@ describe('nuxt-jsonld', () => {
expect(json).to.have.property('count', 2);
});
});

it.only('places jsonld with tagPosition', () => {
cy.visit('/composable-options');
cy.get('body script[type="application/ld+json"]')
.should('exist')
.then((el) => {
const json = JSON.parse(el.text());
expect(json).to.have.property('@context', 'https://schema.org');
expect(json).to.have.property('@type', 'WebSite');
expect(json).to.have.property('name', 'nuxt-jsonld composable options');
});
});
});
24 changes: 24 additions & 0 deletions packages/example/pages/composable-options.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<div>
<h1>Composable Options</h1>
</div>
</template>

<script lang="ts">
export default defineComponent({
setup() {
useJsonld(
() => {
return {
'@context': 'https://schema.org',
'@type': 'WebSite',
name: 'nuxt-jsonld composable options',
};
},
{
tagPosition: 'bodyClose',
}
);
},
});
</script>
1 change: 1 addition & 0 deletions packages/example/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</li>
<li><nuxt-link :to="{ name: 'static' }">Static JSON</nuxt-link></li>
<li><nuxt-link :to="{ name: 'option' }">Options API</nuxt-link></li>
<li><nuxt-link :to="{ name: 'composable-options' }">Composable API Options</nuxt-link></li>
</ul>
</div>
</template>
Expand Down
2 changes: 2 additions & 0 deletions packages/nuxt-jsonld/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { defineNuxtModule, addPlugin, addImports } from '@nuxt/kit';
import type { Nuxt } from '@nuxt/schema';
import type { JsonLDFunc } from './types';

export type { UseJsonldOptions } from './runtime/composable';

type ModuleOptions = {
disableOptionsAPI: boolean;
};
Expand Down
7 changes: 4 additions & 3 deletions packages/nuxt-jsonld/src/runtime/composable.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { computed } from 'vue';
import type { JsonLD, JsonLDFunc } from '../types';
import { useHead } from '@unhead/vue';
import { useHead, type UseHeadOptions } from '@unhead/vue';

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

export const useJsonld = (json: JsonLD | JsonLDFunc) => {
export const useJsonld = (json: JsonLD | JsonLDFunc, options?: UseJsonldOptions) => {
if (!json) {
return;
}
Expand All @@ -22,5 +23,5 @@ export const useJsonld = (json: JsonLD | JsonLDFunc) => {
},
],
};
});
}, options);
};

0 comments on commit 244b44a

Please sign in to comment.