Skip to content

Commit

Permalink
feat: update title & description during dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 30, 2020
1 parent bdbbdd5 commit 0b9bf27
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
40 changes: 29 additions & 11 deletions lib/app/composables/head.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { watchEffect } from 'vue'
import { useSiteData } from './siteData'
import { siteDataRef } from './siteData'

/**
* @param {import('./pageData').PageDataRef} pageData
*/
export function useUpdateHead(pageData) {
const siteData = useSiteData()
const descriptionTag = createHeadElement(['meta', {
name: 'description',
content: siteDataRef.value.description
}])
document.head.appendChild(descriptionTag)

const updateTitleAndDescription = () => {
document.title = siteDataRef.value.title
descriptionTag.setAttribute('content', siteDataRef.value.description)
}

/**
* @type {HTMLElement[]}
Expand All @@ -24,22 +33,17 @@ export function useUpdateHead(pageData) {
tags.forEach((el) => document.head.removeChild(el))
tags.length = 0
if (newTags && newTags.length) {
newTags.forEach(([tag, attrs, innerHTML]) => {
const el = document.createElement(tag)
for (const key in attrs) {
el.setAttribute(key, attrs[key])
}
if (innerHTML) {
el.innerHTML = innerHTML
}
newTags.forEach((headConfig) => {
const el = createHeadElement(headConfig)
document.head.appendChild(el)
tags.push(el)
})
}
}

watchEffect(() => {
updateHeadTags(siteHeadTags, siteData.value.head)
updateTitleAndDescription()
updateHeadTags(siteHeadTags, siteDataRef.value.head)
})

watchEffect(() => {
Expand All @@ -49,3 +53,17 @@ export function useUpdateHead(pageData) {
)
})
}

/**
* @param {import('src').HeadConfig} item
*/
function createHeadElement([tag, attrs, innerHTML]) {
const el = document.createElement(tag)
for (const key in attrs) {
el.setAttribute(key, attrs[key])
}
if (innerHTML) {
el.innerHTML = innerHTML
}
return el
}
2 changes: 1 addition & 1 deletion lib/app/composables/siteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const parse = (data) => readonly(JSON.parse(data))
/**
* @type {import('vue').Ref<import('src').SiteData>}
*/
const siteDataRef = ref(parse(serialized))
export const siteDataRef = ref(parse(serialized))

export function useSiteData() {
return siteDataRef
Expand Down
37 changes: 16 additions & 21 deletions lib/app/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import {
createApp as createClientApp,
createSSRApp,
ref,
readonly
} from 'vue'
import { createApp as createClientApp, createSSRApp, ref, readonly } from 'vue'
import { Content } from './components/Content'
import { createRouter, RouterSymbol } from './router'
import { useUpdateHead } from './composables/head'
import { useSiteData } from './composables/siteData'
import { usePageData, pageDataSymbol } from './composables/pageData'
import { siteDataRef } from './composables/siteData'
import { pageDataSymbol } from './composables/pageData'
import Theme from '/@theme/index'
import { hot } from '@hmr'

Expand All @@ -17,6 +12,8 @@ const inBrowser = typeof window !== 'undefined'
const NotFound = Theme.NotFound || (() => '404 Not Found')

export function createApp() {
// unlike site data which is static across all requests, page data is
// distinct per-request.
const pageDataRef = ref()

if (__DEV__ && inBrowser) {
Expand Down Expand Up @@ -74,26 +71,24 @@ export function createApp() {

app.component('Content', Content)

app.mixin({
beforeCreate() {
const siteRef = useSiteData()
const pageRef = usePageData()
Object.defineProperties(this, {
$site: {
get: () => siteRef.value
},
$page: {
get: () => pageRef.value
}
})
Object.defineProperties(app.config.globalProperties, {
$site: {
get() {
return siteDataRef.value
}
},
$page: {
get() {
return pageDataRef.value
}
}
})

if (Theme.enhanceApp) {
Theme.enhanceApp({
app,
router,
siteData: useSiteData()
siteData: siteDataRef
})
}

Expand Down

0 comments on commit 0b9bf27

Please sign in to comment.