Skip to content

Commit

Permalink
feat: add ssrId option for rendering ssr id on client
Browse files Browse the repository at this point in the history
* add data-vue-ssr-id for client hydration

* Update addStylesClient.js
  • Loading branch information
cyij authored and yyx990803 committed Jan 24, 2018
1 parent af61a42 commit 5281305
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ module.exports.pitch = function (remainingRequest) {
'if(content.locals) module.exports = content.locals;'
]

var options = loaderUtils.getOptions(this)
if (!isServer) {
// on the client: dynamic inject + hot-reload
var code = [
'// add the styles to the DOM',
'var update = require(' + addStylesClientPath + ')(' + id + ', content, ' + isProduction + ');'
'var update = require(' + addStylesClientPath + ')(' + id + ', content, ' + isProduction + ', ' + JSON.stringify(options) + ');'
]
if (!isProduction) {
code = code.concat([
Expand Down
11 changes: 9 additions & 2 deletions lib/addStylesClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ var singletonElement = null
var singletonCounter = 0
var isProduction = false
var noop = function () {}
var options = null
var ssrIdKey = 'data-vue-ssr-id'

// Force single-tag solution on IE6-9, which has a hard limit on the # of <style>
// tags it will allow on a page
var isOldIE = typeof navigator !== 'undefined' && /msie [6-9]\b/.test(navigator.userAgent.toLowerCase())

module.exports = function (parentId, list, _isProduction) {
module.exports = function (parentId, list, _isProduction, _options) {
isProduction = _isProduction

options = _options || {}

var styles = listToStyles(parentId, list)
addStylesToDom(styles)

Expand Down Expand Up @@ -113,7 +117,7 @@ function createStyleElement () {

function addStyle (obj /* StyleObjectPart */) {
var update, remove
var styleElement = document.querySelector('style[data-vue-ssr-id~="' + obj.id + '"]')
var styleElement = document.querySelector('style[' + ssrIdKey + '~="' + obj.id + '"]')

if (styleElement) {
if (isProduction) {
Expand Down Expand Up @@ -195,6 +199,9 @@ function applyToTag (styleElement, obj) {
if (media) {
styleElement.setAttribute('media', media)
}
if (options.ssrId) {
styleElement.setAttribute(ssridKey, obj.id)
}

if (sourceMap) {
// https://developer.chrome.com/devtools/docs/javascript-debugging
Expand Down

0 comments on commit 5281305

Please sign in to comment.