Skip to content

Commit 82155f7

Browse files
authored
feat: 支持 ssr (#29)
1 parent 1e1d456 commit 82155f7

9 files changed

+40
-13
lines changed

README-zh.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- 支持云服务
2727
- [x] 阿里云
2828
- [x] 七牛
29+
- 支持 SSR
2930

3031
## Install
3132

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This component aims to replace native img element and use webp!
2828
- Support cloud image service
2929
- [x] Alibaba
3030
- [x] Qiniu
31+
- Support SSR
3132

3233
[⬆ Back to Top](#table-of-contents)
3334

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
"lint": "eslint \"**/*.@(js|vue)\" --fix",
3636
"release": "gren release --override"
3737
},
38-
"dependencies": {
39-
"lazysizes": "^5.1.1"
40-
},
38+
"dependencies": {},
4139
"devDependencies": {
4240
"@babel/core": "^7.4.3",
4341
"@babel/plugin-transform-runtime": "^7.4.3",

src/background.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'lazysizes/plugins/bgset/ls.bgset'
21
import getImageSrc from './provider-config'
32

43
function getSrc(config) {

src/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22
import Component from './v-img.vue'
33
import background from './background'
44
import placeholder from './spinner.svg'
5+
import loadScript from './loadScript.js'
56

67
const defaultOptions = {
78
placeholder,
89
error:
910
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAABSCAMAAADw8nOpAAAArlBMVEUAAAAAAAD///9paWkyMjL////////////29vb////09PTn5+fh4eGvr6/////6+vqZmZm8vLz39/fj4+P8/PyBgYH////////////////Gxsb////////////v7+/MzMzr6+v///+4uLj////o6OhNTU3Y2NjQ0ND9/f35+fn////////////t7e3////////////////z8/Pb29v////y8vLw8PDU1NT////////ym0LiAAAAOXRSTlMaAPooH+3z2LwFtYZ5QvXUNkvDgOAul49vV1RHGRKfWZThSPiM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sa8VkoHSfmiG7dSxkgrWoPpRS7eGlQ1MG6b/c+uss1iFWsaH5fYZtU0g6XrxUzj/xKawbqW1Sx+mhX4dlNoWesba321lvvAIA8Lb5qExrq2/b/thee7vy2O2FTa/tdvUoLVzFMOdwkjvvvjcRY2KRdspZZ6g63UFRu+fc2Gr4NtaQeb5w62+B0cRHR0XHL9oU73R0/XH5B1c4zX/WEj5/f1R6LdH9xef7x8/SF410f1139Q+P+fPa74ONPBJ6Q/+TfzjGYmPq8AAAAASUVORK5CYII='
1011
}
1112

13+
const lazysizes = 'https://cdn.jsdelivr.net/npm/lazysizes/lazysizes.min.js'
14+
const bgset =
15+
'https://cdn.jsdelivr.net/npm/lazysizes/plugins/bgset/ls.bgset.min.js'
16+
1217
// `Vue.use` automatically prevents you from using
1318
// the same plugin more than once,
1419
// so calling it multiple times on the same plugin
1520
// will install the plugin only once
1621
Component.install = (Vue, options = {}) => {
1722
Vue.prototype.$vImg = {...defaultOptions, ...options}
1823

24+
if (typeof window !== 'undefined' && !window.lazySizes) {
25+
Promise.all([loadScript(lazysizes), loadScript(bgset)]).catch(console.error)
26+
}
27+
1928
Vue.component(Component.name, Component)
2029

2130
Vue.directive('img', {

src/loadScript.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const loaded = new Set()
2+
3+
export default function(script) {
4+
return new Promise((resolve, reject) => {
5+
if (!script) {
6+
reject(new Error('Script is not found'))
7+
}
8+
if (loaded.has(script)) {
9+
resolve(true)
10+
} else {
11+
const loadScript = document.createElement('script')
12+
loadScript.onload = function() {
13+
loaded.add(script)
14+
resolve(true)
15+
}
16+
loadScript.onerror = function(err) {
17+
loadScript.remove()
18+
reject(err)
19+
}
20+
loadScript.src = script
21+
document.body.appendChild(loadScript)
22+
}
23+
})
24+
}

src/provider-config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ export const providerConfig = {
4444

4545
if (!autocrop || is(svg, src) || !src) return vm
4646
const DPR = 2
47-
let dpr = (window && window.devicePixelRatio) || DPR
47+
let dpr =
48+
(typeof window !== 'undefined' && window.devicePixelRatio) || DPR
4849
if (dpr === 1) {
4950
dpr = DPR
5051
}
5152
const actions = ['/resize']
52-
const WIDTH = `w_${width * dpr}`
53-
const HEIGHT = `h_${height * dpr}`
53+
const WIDTH = `w_${parseInt(width * dpr)}`
54+
const HEIGHT = `h_${parseInt(height * dpr)}`
5455
const AUTOCROP = `m_fill`
5556

5657
if (isNaN(width) && isNaN(height)) {

src/v-img.vue

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {providerConfig, default as getSrc} from './provider-config'
3030
* 如果顺序是1 => 2,则是理想的
3131
* 如果顺序是2 => 1,则图片会被请求两次:一是原始src;二是转换为webp的src
3232
*/
33-
import 'lazysizes'
3433
3534
const STATUS_IDLE = 0
3635
// 目前没有必要区分 idle 和 loading,暂且保留标识符

yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -6749,11 +6749,6 @@ lazy-property@~1.0.0:
67496749
resolved "https://registry.npmjs.org/lazy-property/-/lazy-property-1.0.0.tgz#84ddc4b370679ba8bd4cdcfa4c06b43d57111147"
67506750
integrity sha1-hN3Es3Bnm6i9TNz6TAa0PVcREUc=
67516751

6752-
lazysizes@^5.1.1:
6753-
version "5.1.1"
6754-
resolved "https://registry.npm.taobao.org/lazysizes/download/lazysizes-5.1.1.tgz#7ca0266519d531ce740d6782e42faf700883646c"
6755-
integrity sha1-fKAmZRnVMc50DWeC5C+vcAiDZGw=
6756-
67576752
lcid@^2.0.0:
67586753
version "2.0.0"
67596754
resolved "https://registry.npm.taobao.org/lcid/download/lcid-2.0.0.tgz#6ef5d2df60e52f82eb228a4c373e8d1f397253cf"

0 commit comments

Comments
 (0)