Skip to content

Commit

Permalink
feat(core): add support to change script position from body
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 26, 2022
1 parent 4a2e173 commit 2816003
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/schema-org/src/composables/createSchemaOrg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
import { deepUnref } from '../util'

export interface CreateSchemaOrgInput {
/**
* Whether to inject the scripts at the end of the body or in the head.
*/
position?: 'body' | 'head'
/**
* The meta data used to render the final schema.org graph.
*/
Expand Down Expand Up @@ -55,6 +59,8 @@ export interface SchemaOrgVuePlugin {
}

export const createSchemaOrg = (options: CreateSchemaOrgInput) => {
if (!options.position)
options.position = 'body'
const schemaRef = ref<string>('')

let ctx = createSchemaOrgGraph()
Expand Down Expand Up @@ -95,16 +101,18 @@ export const createSchemaOrg = (options: CreateSchemaOrgInput) => {

setupDOM() {
return options.updateHead(computed(() => {
const script: any = {
'type': 'application/ld+json',
'data-id': 'schema-org-graph',
'key': 'schema-org-graph',
'children': schemaRef.value,
}
if (options.position === 'body')
script.body = true
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,
},
script,
],
}
}))
Expand Down
4 changes: 4 additions & 0 deletions packages/schema-org/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface ResolvedUserConfig {
}

export interface UserConfig {
/**
* Whether to inject the scripts at the end of the body or in the head.
*/
position?: 'body' | 'head'
/**
* Should schema.org only be rendered by the server.
*
Expand Down

0 comments on commit 2816003

Please sign in to comment.