Skip to content

Commit 61a6095

Browse files
committed
feat: default tag position config
1 parent 75794ec commit 61a6095

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

packages/example/nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ import NuxtJsonld from 'nuxt-jsonld';
33
export default defineNuxtConfig({
44
modules: [NuxtJsonld],
55
css: ['@/css/index.css'],
6-
devtools: true,
6+
'nuxt-jsonld': {
7+
// you can set a default tag position.
8+
// defaultTagPosition: 'bodyClose',
9+
},
710
});

packages/nuxt-jsonld/src/module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import type { Nuxt } from '@nuxt/schema';
44
import type { JsonLDFunc } from './types';
55
export type { JsonLD, JsonLDFunc } from './types';
66

7+
import type { UseJsonldOptions } from './runtime/composable';
78
export type { UseJsonldOptions } from './runtime/composable';
89

910
type ModuleOptions = {
1011
disableOptionsAPI: boolean;
12+
defaultTagPosition: UseJsonldOptions['tagPosition'];
1113
};
1214

1315
export default defineNuxtModule<ModuleOptions>({
@@ -23,6 +25,10 @@ export default defineNuxtModule<ModuleOptions>({
2325
const composable = resolve(__dirname, './runtime/composable');
2426
nuxt.options.build.transpile.push(runtimeDir);
2527
nuxt.options.alias['#jsonld'] = composable;
28+
nuxt.options.runtimeConfig.public['nuxt-jsonld'] = {
29+
defaultTagPosition: options.defaultTagPosition,
30+
};
31+
2632
addImports([{ name: 'useJsonld', as: 'useJsonld', from: composable }]);
2733

2834
if (!options.disableOptionsAPI) {
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
import { computed } from 'vue';
22
import type { JsonLD, JsonLDFunc } from '../types';
33
import { useHead, type UseHeadOptions } from '@unhead/vue';
4+
import { useRuntimeConfig } from 'nuxt/kit';
45

56
const isFunc = (json: JsonLD | JsonLDFunc): json is JsonLDFunc => typeof json === 'function';
67
export type UseJsonldOptions = Pick<UseHeadOptions, 'tagPosition'>;
78
export type { JsonLD, JsonLDFunc } from '../types';
89

10+
type RuntimeConfig = {
11+
defaultTagPosition: UseJsonldOptions['tagPosition'] | '';
12+
};
13+
14+
const unwrapConfig = <T>(str: T | ''): T | undefined => (str === '' ? undefined : str);
15+
916
export const useJsonld = (json: JsonLD | JsonLDFunc, options?: UseJsonldOptions) => {
1017
if (!json) {
1118
return;
1219
}
1320

21+
const { defaultTagPosition } = useRuntimeConfig().public['nuxt-jsonld'] as RuntimeConfig;
22+
1423
const jsonComputed = computed(() => (isFunc(json) ? json() : json));
15-
useHead(() => {
16-
if (!jsonComputed.value) {
17-
return {};
24+
useHead(
25+
() => {
26+
if (!jsonComputed.value) {
27+
return {};
28+
}
29+
return {
30+
script: [
31+
{
32+
type: 'application/ld+json',
33+
children: JSON.stringify(jsonComputed.value, null, ''),
34+
},
35+
],
36+
};
37+
},
38+
{
39+
tagPosition: options?.tagPosition ?? unwrapConfig(defaultTagPosition),
1840
}
19-
return {
20-
script: [
21-
{
22-
type: 'application/ld+json',
23-
children: JSON.stringify(jsonComputed.value, null, ''),
24-
},
25-
],
26-
};
27-
}, options);
41+
);
2842
};

packages/nuxt-jsonld/test/composable.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@ import { describe, beforeEach, it, test, expect, vi, type Mock } from 'vitest';
22
import { ref, toValue } from 'vue';
33
import { useJsonld } from '../src/runtime/composable';
44

5-
const { useHead } = vi.hoisted(() => {
5+
const { useHead, useRuntimeConfig } = vi.hoisted(() => {
66
return {
77
useHead: vi.fn(),
8+
useRuntimeConfig: vi.fn(() => {
9+
return {
10+
public: {
11+
'nuxt-jsonld': {
12+
defaultTagPosition: undefined as string | undefined,
13+
},
14+
},
15+
};
16+
}),
817
};
918
});
19+
1020
vi.mock('@unhead/vue', () => ({
1121
useHead,
1222
}));
23+
vi.mock('nuxt/kit', () => ({
24+
useRuntimeConfig,
25+
}));
1326

1427
const getLastCalledParams = (mock: Mock<any>) => mock.mock.calls[mock.mock.calls.length - 1];
1528

@@ -81,4 +94,23 @@ describe('useJsonld', () => {
8194
tagPosition: 'bodyClose',
8295
});
8396
});
97+
98+
test('default tag position', () => {
99+
useRuntimeConfig.mockImplementation(() => ({
100+
public: {
101+
'nuxt-jsonld': {
102+
defaultTagPosition: 'bodyClose',
103+
},
104+
},
105+
}));
106+
useJsonld({
107+
'@context': 'https://schema.org',
108+
'@type': 'Thing',
109+
name: 'foo',
110+
});
111+
expect(useHead).toBeCalledTimes(1);
112+
expect(toValue(getLastCalledParams(useHead)[1])).toEqual({
113+
tagPosition: 'bodyClose',
114+
});
115+
});
84116
});

0 commit comments

Comments
 (0)