Skip to content

20241211 #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/example/composables/dog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type Dog = {
breed: string;
name: string;
age: number;
};

export const useDog = async () => {
const app = useNuxtApp();

// fetch Dog data
await new Promise((res) => setTimeout(res, 100));
const dog: Dog = {
breed: 'Golden Retriever',
name: 'Buddy',
age: 5,
};

// Note: This jsonld will not disappear even after page transition.
// If you want to link it to the page, use useJsonld in the component side.
app.runWithContext(() => {
useJsonld(() => ({
'@context': 'https://schema.org',
'@type': 'Thing',
name: dog.name,
description: `A ${dog.breed} dog: not linked to the page`,
}));
});

return {
dog,
};
};
6 changes: 2 additions & 4 deletions packages/example/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import NuxtJsonld from 'nuxt-jsonld';

export default defineNuxtConfig({
modules: [NuxtJsonld],
modules: ['nuxt-jsonld'],
css: ['@/css/index.css'],
devtools: true,
compatibilityDate: '2024-12-11',
});
2 changes: 1 addition & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "node ./.output/server/index.mjs"
},
"dependencies": {
"nuxt": "^3.11.2"
"nuxt": "^3.12.4"
},
"devDependencies": {
"@nuxt/devtools": "^0.8.5"
Expand Down
29 changes: 13 additions & 16 deletions packages/example/pages/composable-options.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<template>
<div>
<h1>Composable Options</h1>
<nuxt-link to="/"> Back to list </nuxt-link>
</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 lang="ts" setup>
useJsonld(
() => {
return {
'@context': 'https://schema.org',
'@type': 'WebSite',
name: 'nuxt-jsonld composable options',
};
},
});
{
tagPosition: 'bodyClose',
}
);
</script>
32 changes: 32 additions & 0 deletions packages/example/pages/context.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<template>
<div>
<h1>Context example</h1>
<div>
<pre>{{ JSON.stringify(dog, null, 4) }}</pre>
<nuxt-link to="/"> Back to list </nuxt-link>
</div>
</div>
</template>

<script lang="ts" setup>
import { useDog } from '@/composables/dog';
const { dog } = await useDog();
useJsonld(() => ({
'@context': 'https://schema.org',
'@type': 'Thing',
name: dog.name,
description: `A ${dog.breed} dog: linked to this page`,
}));
</script>

<style scoped>
pre {
display: block;
margin: 10px auto;
max-width: 300px;
padding: 12px;
text-align: left;
background-color: gainsboro;
border-radius: 4px;
}
</style>
51 changes: 21 additions & 30 deletions packages/example/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,34 @@
{{ p.name }}
</nuxt-link>
</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>
<li><nuxt-link :to="{ name: 'context' }">Context</nuxt-link></li>
</ul>
</div>
</template>

<script lang="ts">
import { WithContext, ItemList } from 'schema-dts';
<script lang="ts" setup>
useHead({
title: 'Product List',
});

export default defineComponent({
setup() {
useHead({
title: 'Product List',
});
const products = [
{ name: 'Foo', id: 1 },
{ name: 'Bar', id: 2 },
{ name: 'Baz', id: 3 },
];

return {
products: [
{ name: 'Foo', id: 1 },
{ name: 'Bar', id: 2 },
{ name: 'Baz', id: 3 },
],
};
},
jsonld(): WithContext<ItemList> {
return {
'@context': 'https://schema.org',
'@type': 'ItemList',
itemListElement: this.products.map((p) => ({
'@type': 'ListItem',
position: p.id,
item: {
'@type': 'Product',
name: p.name,
},
})),
};
},
useJsonld({
'@context': 'https://schema.org',
'@type': 'ItemList',
itemListElement: products.map((p) => ({
'@type': 'ListItem',
position: p.id,
item: {
'@type': 'Product',
name: p.name,
},
})),
});
</script>
55 changes: 23 additions & 32 deletions packages/example/pages/products/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,30 @@
</div>
</template>

<script lang="ts">
export default defineComponent({
setup() {
const { params } = useRoute();
const count = ref<number>(0);
const updateCount = () => {
count.value++;
};
const product = reactive({
name: params.id,
description: `This is a sample ${params.id} product.`,
count,
});

useHead({
title: `Product: ${product.name}`,
});
<script lang="ts" setup>
const { params } = useRoute();
const count = ref<number>(0);
const updateCount = () => {
count.value++;
};
const product = reactive({
name: params.id,
description: `This is a sample ${params.id} product.`,
count,
});

useJsonld(() => {
if (!product.count) {
return null;
}
return {
'@context': 'https://schema.org',
'@type': 'Product',
...product,
};
});
useHead({
title: `Product: ${product.name}`,
});

return {
updateCount,
product,
};
},
useJsonld(() => {
if (!product.count) {
return null;
}
return {
'@context': 'https://schema.org',
'@type': 'Product',
...product,
};
});
</script>
31 changes: 0 additions & 31 deletions packages/example/pages/static.vue

This file was deleted.

1 change: 1 addition & 0 deletions packages/nuxt-jsonld/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resolve } from 'pathe';
import { defineNuxtModule, addPlugin, addImports } from '@nuxt/kit';
import type { Nuxt } from '@nuxt/schema';
import type { JsonLDFunc } from './types';
export type { JsonLD, JsonLDFunc } from './types';

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

Expand Down
1 change: 1 addition & 0 deletions packages/nuxt-jsonld/src/runtime/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 type { JsonLD, JsonLDFunc } from '../types';

export const useJsonld = (json: JsonLD | JsonLDFunc, options?: UseJsonldOptions) => {
if (!json) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7553,7 +7553,7 @@ nuxi@^3.15.0:
resolved "https://registry.yarnpkg.com/nuxi/-/nuxi-3.15.0.tgz#ed54923ca46727c6e7df10495143db340d9791c9"
integrity sha512-ZVu45nuDrdb7nzKW2kLGY/N1vvFYLLbUVX6gUYw4BApKGGu4+GktTR5o48dGVgMYX9A8chaugl7TL9ZYmwC9Mg==

nuxt@^3.11.2:
nuxt@^3.11.2, nuxt@^3.12.4:
version "3.14.1592"
resolved "https://registry.yarnpkg.com/nuxt/-/nuxt-3.14.1592.tgz#0f94132b7e0ffe9087b37392f295e2c7d5d05ee3"
integrity sha512-roWAQH4Mb6WY72cNos+YVw0DgTCNAhNygiAMCedM7hbX6ESTR2n3VH7tU0yIWDPe/hfFdii4M4wWTTNHOtS44g==
Expand Down
Loading