From a31ddd3591909eaa8971a5330c8b440887154c06 Mon Sep 17 00:00:00 2001 From: ymmooot Date: Wed, 11 Dec 2024 19:16:20 +0900 Subject: [PATCH] chore: update example code --- cypress/e2e/test.cy.js | 17 ------ packages/example/composables/dog.ts | 32 +++++++++++ packages/example/nuxt.config.ts | 6 +- packages/example/package.json | 2 +- packages/example/pages/composable-options.vue | 29 +++++----- packages/example/pages/context.vue | 32 +++++++++++ packages/example/pages/index.vue | 51 +++++++---------- packages/example/pages/products/[id].vue | 55 ++++++++----------- packages/example/pages/static.vue | 31 ----------- yarn.lock | 2 +- 10 files changed, 125 insertions(+), 132 deletions(-) create mode 100644 packages/example/composables/dog.ts create mode 100644 packages/example/pages/context.vue delete mode 100644 packages/example/pages/static.vue diff --git a/cypress/e2e/test.cy.js b/cypress/e2e/test.cy.js index 5c04e7da..11f57c69 100644 --- a/cypress/e2e/test.cy.js +++ b/cypress/e2e/test.cy.js @@ -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"]') diff --git a/packages/example/composables/dog.ts b/packages/example/composables/dog.ts new file mode 100644 index 00000000..2c8c6c1c --- /dev/null +++ b/packages/example/composables/dog.ts @@ -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, + }; +}; diff --git a/packages/example/nuxt.config.ts b/packages/example/nuxt.config.ts index 576f3c87..ad0f5011 100644 --- a/packages/example/nuxt.config.ts +++ b/packages/example/nuxt.config.ts @@ -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', }); diff --git a/packages/example/package.json b/packages/example/package.json index 22f6c3d8..84c2b7cf 100644 --- a/packages/example/package.json +++ b/packages/example/package.json @@ -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" diff --git a/packages/example/pages/composable-options.vue b/packages/example/pages/composable-options.vue index 6b18f187..389f06ae 100644 --- a/packages/example/pages/composable-options.vue +++ b/packages/example/pages/composable-options.vue @@ -1,24 +1,21 @@ - diff --git a/packages/example/pages/context.vue b/packages/example/pages/context.vue new file mode 100644 index 00000000..c7149411 --- /dev/null +++ b/packages/example/pages/context.vue @@ -0,0 +1,32 @@ + + + + + diff --git a/packages/example/pages/index.vue b/packages/example/pages/index.vue index f7649465..3c066fa8 100644 --- a/packages/example/pages/index.vue +++ b/packages/example/pages/index.vue @@ -8,43 +8,34 @@ {{ p.name }} -
  • Static JSON
  • Options API
  • Composable API Options
  • +
  • Context
  • - diff --git a/packages/example/pages/products/[id].vue b/packages/example/pages/products/[id].vue index 2bc22233..be7c1d21 100644 --- a/packages/example/pages/products/[id].vue +++ b/packages/example/pages/products/[id].vue @@ -10,39 +10,30 @@ - diff --git a/packages/example/pages/static.vue b/packages/example/pages/static.vue deleted file mode 100644 index acb9c403..00000000 --- a/packages/example/pages/static.vue +++ /dev/null @@ -1,31 +0,0 @@ - - - - - diff --git a/yarn.lock b/yarn.lock index 5109e978..066010a3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==