-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plugin): add plugin for head; clean mixin
- add $speedkitHead for apply head in custom definition
- Loading branch information
1 parent
eb16b21
commit bffb95d
Showing
4 changed files
with
58 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Vue from 'vue'; | ||
const pageStyles = { prev: new Map(), current: new Map() }; | ||
|
||
export default { | ||
install (Vue) { | ||
Vue.mixin({ | ||
beforeRouteEnter (to, from, next) { | ||
pageStyles.prev = new Map(pageStyles.current); | ||
pageStyles.current.clear(); | ||
next(); | ||
} | ||
}) | ||
} | ||
}; | ||
|
||
const head = function () { | ||
const head = this.fontCollection.getHeadDescription ? this.fontCollection.getHeadDescription(this.isCritical, this.$speedkit.crossorigin) : {}; | ||
let style = head.style || []; | ||
|
||
// add critical fonts to ssr context. | ||
if (process.static && process.server && this.isCritical) { | ||
head.style.forEach(style => this.$addCriticalFontStyle(style)); | ||
} | ||
|
||
// get styles from ssr context, important for initial load | ||
const nuxtStateData = (this.$nuxt?.context || this.context)?.nuxtState?.data; | ||
if (nuxtStateData && nuxtStateData.length) { | ||
style = [].concat(style, Object.values(nuxtStateData[0]._criticalFontStyles || {})); | ||
} | ||
|
||
// keeping styles in head when changing page | ||
style = style.reduce((result, style) => { | ||
if (!style.hid) { | ||
result.push(style); | ||
} else { | ||
pageStyles.current.set(style.hid, style); | ||
} | ||
return result; | ||
}, []); | ||
style = style.concat(Array.from(pageStyles.prev.values()), Array.from(pageStyles.current.values())); | ||
|
||
return { ...head, style }; | ||
}; | ||
|
||
!('$speedkitHead' in Vue.prototype) && Object.defineProperty(Vue.prototype, '$speedkitHead', { | ||
get () { | ||
return head.bind(this); | ||
} | ||
}); | ||
|
||
Vue.config.optionMergeStrategies.head = Vue.config.optionMergeStrategies.data; |