@@ -6,45 +6,50 @@ import type {
66} from 'schema-org-graph-js'
77import {
88 buildResolvedGraphCtx ,
9- createSchemaOrgGraph , dedupeAndFlattenNodes , renderNodesToSchemaOrgHtml , resolveMeta ,
9+ createSchemaOrgGraph , organiseNodes , renderNodesToSchemaOrgHtml , resolveMeta ,
1010} from 'schema-org-graph-js'
1111
1212export 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
5964export 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