Skip to content

Commit 5281305

Browse files
cyijyyx990803
authored andcommitted
feat: add ssrId option for rendering ssr id on client
* add data-vue-ssr-id for client hydration * Update addStylesClient.js
1 parent af61a42 commit 5281305

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Diff for: index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ module.exports.pitch = function (remainingRequest) {
3535
'if(content.locals) module.exports = content.locals;'
3636
]
3737

38+
var options = loaderUtils.getOptions(this)
3839
if (!isServer) {
3940
// on the client: dynamic inject + hot-reload
4041
var code = [
4142
'// add the styles to the DOM',
42-
'var update = require(' + addStylesClientPath + ')(' + id + ', content, ' + isProduction + ');'
43+
'var update = require(' + addStylesClientPath + ')(' + id + ', content, ' + isProduction + ', ' + JSON.stringify(options) + ');'
4344
]
4445
if (!isProduction) {
4546
code = code.concat([

Diff for: lib/addStylesClient.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ var singletonElement = null
4242
var singletonCounter = 0
4343
var isProduction = false
4444
var noop = function () {}
45+
var options = null
46+
var ssrIdKey = 'data-vue-ssr-id'
4547

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

50-
module.exports = function (parentId, list, _isProduction) {
52+
module.exports = function (parentId, list, _isProduction, _options) {
5153
isProduction = _isProduction
5254

55+
options = _options || {}
56+
5357
var styles = listToStyles(parentId, list)
5458
addStylesToDom(styles)
5559

@@ -113,7 +117,7 @@ function createStyleElement () {
113117

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

118122
if (styleElement) {
119123
if (isProduction) {
@@ -195,6 +199,9 @@ function applyToTag (styleElement, obj) {
195199
if (media) {
196200
styleElement.setAttribute('media', media)
197201
}
202+
if (options.ssrId) {
203+
styleElement.setAttribute(ssridKey, obj.id)
204+
}
198205

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

0 commit comments

Comments
 (0)