Skip to content

Commit

Permalink
chore: update example code
Browse files Browse the repository at this point in the history
  • Loading branch information
ymmooot committed Dec 11, 2024
1 parent 47f159d commit a31ddd3
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 132 deletions.
17 changes: 0 additions & 17 deletions cypress/e2e/test.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ describe('nuxt-jsonld', () => {
});
});

it('dumps static jsonld', () => {
cy.visit('/static');
cy.get('script[type="application/ld+json"]')
.should('exist')
.then((el) => {
const json = JSON.parse(el[0].innerText);
expect(json).to.have.property('@context', 'https://schema.org');
expect(json).to.have.property('@type', 'Thing');
expect(json).to.have.property('name', 'Static json');

const json2 = JSON.parse(el[1].innerText);
expect(json2).to.have.property('@context', 'https://schema.org');
expect(json2).to.have.property('@type', 'Thing');
expect(json2).to.have.property('name', 'test');
});
});

it('replaces jsonld on page transition', () => {
cy.visit('/static');
cy.get('script[type="application/ld+json"]')
Expand Down
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.

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

0 comments on commit a31ddd3

Please sign in to comment.