Skip to content

Commit

Permalink
feat: default tag position config
Browse files Browse the repository at this point in the history
  • Loading branch information
ymmooot committed Dec 11, 2024
1 parent 75794ec commit 61a6095
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/example/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import NuxtJsonld from 'nuxt-jsonld';
export default defineNuxtConfig({
modules: [NuxtJsonld],
css: ['@/css/index.css'],
devtools: true,
'nuxt-jsonld': {
// you can set a default tag position.
// defaultTagPosition: 'bodyClose',
},
});
6 changes: 6 additions & 0 deletions packages/nuxt-jsonld/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import type { Nuxt } from '@nuxt/schema';
import type { JsonLDFunc } from './types';
export type { JsonLD, JsonLDFunc } from './types';

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

type ModuleOptions = {
disableOptionsAPI: boolean;
defaultTagPosition: UseJsonldOptions['tagPosition'];
};

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -23,6 +25,10 @@ export default defineNuxtModule<ModuleOptions>({
const composable = resolve(__dirname, './runtime/composable');
nuxt.options.build.transpile.push(runtimeDir);
nuxt.options.alias['#jsonld'] = composable;
nuxt.options.runtimeConfig.public['nuxt-jsonld'] = {
defaultTagPosition: options.defaultTagPosition,
};

addImports([{ name: 'useJsonld', as: 'useJsonld', from: composable }]);

if (!options.disableOptionsAPI) {
Expand Down
38 changes: 26 additions & 12 deletions packages/nuxt-jsonld/src/runtime/composable.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import { computed } from 'vue';
import type { JsonLD, JsonLDFunc } from '../types';
import { useHead, type UseHeadOptions } from '@unhead/vue';
import { useRuntimeConfig } from 'nuxt/kit';

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

type RuntimeConfig = {
defaultTagPosition: UseJsonldOptions['tagPosition'] | '';
};

const unwrapConfig = <T>(str: T | ''): T | undefined => (str === '' ? undefined : str);

export const useJsonld = (json: JsonLD | JsonLDFunc, options?: UseJsonldOptions) => {
if (!json) {
return;
}

const { defaultTagPosition } = useRuntimeConfig().public['nuxt-jsonld'] as RuntimeConfig;

const jsonComputed = computed(() => (isFunc(json) ? json() : json));
useHead(() => {
if (!jsonComputed.value) {
return {};
useHead(
() => {
if (!jsonComputed.value) {
return {};
}
return {
script: [
{
type: 'application/ld+json',
children: JSON.stringify(jsonComputed.value, null, ''),
},
],
};
},
{
tagPosition: options?.tagPosition ?? unwrapConfig(defaultTagPosition),
}
return {
script: [
{
type: 'application/ld+json',
children: JSON.stringify(jsonComputed.value, null, ''),
},
],
};
}, options);
);
};
34 changes: 33 additions & 1 deletion packages/nuxt-jsonld/test/composable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import { describe, beforeEach, it, test, expect, vi, type Mock } from 'vitest';
import { ref, toValue } from 'vue';
import { useJsonld } from '../src/runtime/composable';

const { useHead } = vi.hoisted(() => {
const { useHead, useRuntimeConfig } = vi.hoisted(() => {
return {
useHead: vi.fn(),
useRuntimeConfig: vi.fn(() => {
return {
public: {
'nuxt-jsonld': {
defaultTagPosition: undefined as string | undefined,
},
},
};
}),
};
});

vi.mock('@unhead/vue', () => ({
useHead,
}));
vi.mock('nuxt/kit', () => ({
useRuntimeConfig,
}));

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

Expand Down Expand Up @@ -81,4 +94,23 @@ describe('useJsonld', () => {
tagPosition: 'bodyClose',
});
});

test('default tag position', () => {
useRuntimeConfig.mockImplementation(() => ({
public: {
'nuxt-jsonld': {
defaultTagPosition: 'bodyClose',
},
},
}));
useJsonld({
'@context': 'https://schema.org',
'@type': 'Thing',
name: 'foo',
});
expect(useHead).toBeCalledTimes(1);
expect(toValue(getLastCalledParams(useHead)[1])).toEqual({
tagPosition: 'bodyClose',
});
});
});

0 comments on commit 61a6095

Please sign in to comment.