Skip to content

Commit

Permalink
feat(core): improve createSchemaOrg API
Browse files Browse the repository at this point in the history
- add forceRefresh method
- allow promises for meta resolution
- rename client to SchemaOrgVuePlugin
  • Loading branch information
harlan-zw committed Aug 22, 2022
1 parent dc1fc58 commit 40540c9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 59 deletions.
6 changes: 3 additions & 3 deletions packages/schema-org/runtime/composables/injectSchemaOrg.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PROVIDE_KEY } from '@vueuse/schema-org'
import type { SchemaOrgClient } from '@vueuse/schema-org'
import type { SchemaOrgVuePlugin } from '@vueuse/schema-org'
import { inject } from 'vue'

export function injectSchemaOrg() {
let client: SchemaOrgClient | undefined
let client: SchemaOrgVuePlugin | undefined
try {
client = inject<SchemaOrgClient>(PROVIDE_KEY)
client = inject<SchemaOrgVuePlugin>(PROVIDE_KEY)
}
catch (e) {}

Expand Down
8 changes: 3 additions & 5 deletions packages/schema-org/runtime/composables/useSchemaOrg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export function useSchemaOrg(input: any) {
// SSR Mode does not need to do anything else.
if (typeof window === 'undefined') {
nextTick(() => {
watch(() => input, () => {
client.generateSchema()
client.setupDOM()
watch(() => input, async () => {
await client.forceRefresh()
}, {
immediate: true,
deep: true,
Expand All @@ -40,8 +39,7 @@ export function useSchemaOrg(input: any) {

// CSR Mode will need to manually trigger the schema to re-generate
onMounted(() => {
client.generateSchema()
client.setupDOM()
client.forceRefresh()
})

onBeforeUnmount(() => {
Expand Down
104 changes: 53 additions & 51 deletions packages/schema-org/src/composables/createSchemaOrg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,50 @@ import type {
} from 'schema-org-graph-js'
import {
buildResolvedGraphCtx,
createSchemaOrgGraph, dedupeAndFlattenNodes, renderNodesToSchemaOrgHtml, resolveMeta,
createSchemaOrgGraph, organiseNodes, renderNodesToSchemaOrgHtml, resolveMeta,
} from 'schema-org-graph-js'

export interface CreateSchemaOrgInput {
/**
* The meta data used to render the final schema.org graph.
*/
meta: () => MetaInput
meta: () => MetaInput | Promise<MetaInput>
/**
* Client used to write schema to the document.
*/
updateHead: (fn: ComputedRef) => void
/**
* Will enable debug logs to be shown.
*/
debug?: boolean
updateHead: (fn: ComputedRef) => void | Promise<void>
}

export interface SchemaOrgClient {
export interface SchemaOrgVuePlugin {
/**
* Install the plugin on the Vue context.
*
* @param app
*/
install: (app: App) => void

/**
* Given a Vue component context, deleted any nodes associated with it.
*/
removeContext: (uid: Number) => void
/**
* Sets up the initial placeholder for the meta tag using useHead.
*/
setupDOM: () => void

setupDOM: () => void | Promise<void>
/**
* Trigger the schemaRef to be updated.
*/
generateSchema: () => Ref<string>
resolveGraph: () => SchemaOrgContext
resolvedSchemaOrg: () => string

schemaRef: Ref<string>
generateSchema: () => Promise<Ref<string>> | Ref<string>
/**
* Force Schema.org to be refreshed in the DOM.
*/
forceRefresh: () => Promise<void>
/**
* The inner context being used to generate the Schema.org graph.
*/
ctx: SchemaOrgContext
/**
* Options used to render the Schema.
*/
options: CreateSchemaOrgInput
}

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

export const PROVIDE_KEY = Symbol('schemaorg') as InjectionKey<SchemaOrgClient>
export const PROVIDE_KEY = Symbol('schemaorg') as InjectionKey<SchemaOrgVuePlugin>

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

let ctx = createSchemaOrgGraph()

const client: SchemaOrgClient = {
install(app) {
app.config.globalProperties.$schemaOrg = client
app.provide(PROVIDE_KEY, client)
},
const resolveGraphNodesToHtml = async () => {
const meta = await options.meta()
const resolvedMeta = resolveMeta(unrefDeep(meta))
const resolvedCtx = buildResolvedGraphCtx(ctx.nodes.map(unrefDeep), resolvedMeta)
const nodes = organiseNodes(resolvedCtx.nodes)
return renderNodesToSchemaOrgHtml(nodes)
}

const client: SchemaOrgVuePlugin = {
ctx,
options,
schemaRef,

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

resolvedSchemaOrg() {
const resolvedCtx = client.resolveGraph()
const nodes = dedupeAndFlattenNodes(resolvedCtx.nodes)
return renderNodesToSchemaOrgHtml(nodes)
async generateSchema() {
schemaRef.value = await resolveGraphNodesToHtml()
return schemaRef
},

generateSchema() {
schemaRef.value = client.resolvedSchemaOrg()
return schemaRef
async forceRefresh() {
await client.generateSchema()
await client.setupDOM()
},

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

setupDOM() {
if (options?.updateHead) {
options.updateHead(computed(() => {
return {
// Can be static or computed
script: [
{
'type': 'application/ld+json',
'data-id': 'schema-org-graph',
'key': 'schema-org-graph',
'children': schemaRef.value,
'body': true,
},
],
}
}))
}
return options.updateHead(computed(() => {
return {
// Can be static or computed
script: [
{
'type': 'application/ld+json',
'data-id': 'schema-org-graph',
'key': 'schema-org-graph',
'children': schemaRef.value,
'body': true,
},
],
}
}))
},
}
return client
Expand Down

0 comments on commit 40540c9

Please sign in to comment.