Skip to content

Commit 40540c9

Browse files
committed
feat(core): improve createSchemaOrg API
- add forceRefresh method - allow promises for meta resolution - rename client to SchemaOrgVuePlugin
1 parent dc1fc58 commit 40540c9

File tree

3 files changed

+59
-59
lines changed

3 files changed

+59
-59
lines changed

packages/schema-org/runtime/composables/injectSchemaOrg.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { PROVIDE_KEY } from '@vueuse/schema-org'
2-
import type { SchemaOrgClient } from '@vueuse/schema-org'
2+
import type { SchemaOrgVuePlugin } from '@vueuse/schema-org'
33
import { inject } from 'vue'
44

55
export function injectSchemaOrg() {
6-
let client: SchemaOrgClient | undefined
6+
let client: SchemaOrgVuePlugin | undefined
77
try {
8-
client = inject<SchemaOrgClient>(PROVIDE_KEY)
8+
client = inject<SchemaOrgVuePlugin>(PROVIDE_KEY)
99
}
1010
catch (e) {}
1111

packages/schema-org/runtime/composables/useSchemaOrg.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ export function useSchemaOrg(input: any) {
1919
// SSR Mode does not need to do anything else.
2020
if (typeof window === 'undefined') {
2121
nextTick(() => {
22-
watch(() => input, () => {
23-
client.generateSchema()
24-
client.setupDOM()
22+
watch(() => input, async () => {
23+
await client.forceRefresh()
2524
}, {
2625
immediate: true,
2726
deep: true,
@@ -40,8 +39,7 @@ export function useSchemaOrg(input: any) {
4039

4140
// CSR Mode will need to manually trigger the schema to re-generate
4241
onMounted(() => {
43-
client.generateSchema()
44-
client.setupDOM()
42+
client.forceRefresh()
4543
})
4644

4745
onBeforeUnmount(() => {

packages/schema-org/src/composables/createSchemaOrg.ts

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,50 @@ import type {
66
} from 'schema-org-graph-js'
77
import {
88
buildResolvedGraphCtx,
9-
createSchemaOrgGraph, dedupeAndFlattenNodes, renderNodesToSchemaOrgHtml, resolveMeta,
9+
createSchemaOrgGraph, organiseNodes, renderNodesToSchemaOrgHtml, resolveMeta,
1010
} from 'schema-org-graph-js'
1111

1212
export interface CreateSchemaOrgInput {
1313
/**
1414
* The meta data used to render the final schema.org graph.
1515
*/
16-
meta: () => MetaInput
16+
meta: () => MetaInput | Promise<MetaInput>
1717
/**
1818
* Client used to write schema to the document.
1919
*/
20-
updateHead: (fn: ComputedRef) => void
21-
/**
22-
* Will enable debug logs to be shown.
23-
*/
24-
debug?: boolean
20+
updateHead: (fn: ComputedRef) => void | Promise<void>
2521
}
2622

27-
export interface SchemaOrgClient {
23+
export interface SchemaOrgVuePlugin {
24+
/**
25+
* Install the plugin on the Vue context.
26+
*
27+
* @param app
28+
*/
2829
install: (app: App) => void
29-
3030
/**
3131
* Given a Vue component context, deleted any nodes associated with it.
3232
*/
3333
removeContext: (uid: Number) => void
3434
/**
3535
* Sets up the initial placeholder for the meta tag using useHead.
3636
*/
37-
setupDOM: () => void
38-
37+
setupDOM: () => void | Promise<void>
3938
/**
4039
* Trigger the schemaRef to be updated.
4140
*/
42-
generateSchema: () => Ref<string>
43-
resolveGraph: () => SchemaOrgContext
44-
resolvedSchemaOrg: () => string
45-
46-
schemaRef: Ref<string>
41+
generateSchema: () => Promise<Ref<string>> | Ref<string>
42+
/**
43+
* Force Schema.org to be refreshed in the DOM.
44+
*/
45+
forceRefresh: () => Promise<void>
46+
/**
47+
* The inner context being used to generate the Schema.org graph.
48+
*/
4749
ctx: SchemaOrgContext
50+
/**
51+
* Options used to render the Schema.
52+
*/
4853
options: CreateSchemaOrgInput
4954
}
5055

@@ -54,39 +59,38 @@ const unrefDeep = (n: any) => {
5459
return n
5560
}
5661

57-
export const PROVIDE_KEY = Symbol('schemaorg') as InjectionKey<SchemaOrgClient>
62+
export const PROVIDE_KEY = Symbol('schemaorg') as InjectionKey<SchemaOrgVuePlugin>
5863

5964
export const createSchemaOrg = (options: CreateSchemaOrgInput) => {
6065
const schemaRef = ref<string>('')
6166

6267
let ctx = createSchemaOrgGraph()
6368

64-
const client: SchemaOrgClient = {
65-
install(app) {
66-
app.config.globalProperties.$schemaOrg = client
67-
app.provide(PROVIDE_KEY, client)
68-
},
69+
const resolveGraphNodesToHtml = async () => {
70+
const meta = await options.meta()
71+
const resolvedMeta = resolveMeta(unrefDeep(meta))
72+
const resolvedCtx = buildResolvedGraphCtx(ctx.nodes.map(unrefDeep), resolvedMeta)
73+
const nodes = organiseNodes(resolvedCtx.nodes)
74+
return renderNodesToSchemaOrgHtml(nodes)
75+
}
6976

77+
const client: SchemaOrgVuePlugin = {
7078
ctx,
7179
options,
72-
schemaRef,
7380

74-
resolveGraph() {
75-
const meta = resolveMeta(unrefDeep(options.meta()))
76-
if (!meta.host)
77-
console.warn('[WARN] `@vueuse/schema-org`: Missing required `host` from `createSchemaOrg`.')
78-
return buildResolvedGraphCtx(ctx.nodes.map(unrefDeep), meta)
81+
install(app) {
82+
app.config.globalProperties.$schemaOrg = client
83+
app.provide(PROVIDE_KEY, client)
7984
},
8085

81-
resolvedSchemaOrg() {
82-
const resolvedCtx = client.resolveGraph()
83-
const nodes = dedupeAndFlattenNodes(resolvedCtx.nodes)
84-
return renderNodesToSchemaOrgHtml(nodes)
86+
async generateSchema() {
87+
schemaRef.value = await resolveGraphNodesToHtml()
88+
return schemaRef
8589
},
8690

87-
generateSchema() {
88-
schemaRef.value = client.resolvedSchemaOrg()
89-
return schemaRef
91+
async forceRefresh() {
92+
await client.generateSchema()
93+
await client.setupDOM()
9094
},
9195

9296
removeContext(uid) {
@@ -97,22 +101,20 @@ export const createSchemaOrg = (options: CreateSchemaOrgInput) => {
97101
},
98102

99103
setupDOM() {
100-
if (options?.updateHead) {
101-
options.updateHead(computed(() => {
102-
return {
103-
// Can be static or computed
104-
script: [
105-
{
106-
'type': 'application/ld+json',
107-
'data-id': 'schema-org-graph',
108-
'key': 'schema-org-graph',
109-
'children': schemaRef.value,
110-
'body': true,
111-
},
112-
],
113-
}
114-
}))
115-
}
104+
return options.updateHead(computed(() => {
105+
return {
106+
// Can be static or computed
107+
script: [
108+
{
109+
'type': 'application/ld+json',
110+
'data-id': 'schema-org-graph',
111+
'key': 'schema-org-graph',
112+
'children': schemaRef.value,
113+
'body': true,
114+
},
115+
],
116+
}
117+
}))
116118
},
117119
}
118120
return client

0 commit comments

Comments
 (0)