Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit 35eaf8b

Browse files
authored
Add data-src attribute support (#126)
Add `data-src` attribute support
2 parents 4f713f7 + e9d6d9a commit 35eaf8b

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

Readme.md

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ Some additional steps are required to enable SSL access of your assets by cloudf
104104
2. On the permissions tab, click the add bucket policy button.
105105
* You can use the Policy Generator to generate the appropiate policy with this settings:
106106
- Type: S3 Bucket Policy
107-
- Effect: Allow
108-
- Principal: AWS
109-
- AWS Service: Amazon S3
110-
- Actions: GetObject
111-
- ARN: arn:aws:s3:::<bucket_name>/* (fill in your bucket name)
107+
- Effect: Allow
108+
- Principal: AWS
109+
- AWS Service: Amazon S3
110+
- Actions: GetObject
111+
- ARN: arn:aws:s3:::<bucket_name>/* (fill in your bucket name)
112112
* Click on generate policy and paste the output on the add bucket policy window.
113113
* Save your changes.
114114
3. When you configure express-cdn you must reference cloudfront subdomain directly, since CNAMEs are not supported over ssl.
@@ -183,25 +183,28 @@ app.listen(1337);
183183
// #2 - Load an image with a custom tag attribute
184184
!= CDN('/img/sprite.png', { alt: 'Sprite' })
185185
186-
// #3 - Load a script
186+
// #3 - Load an image with a custom tag attribute and data-src attribute instead src
187+
!= CDN('/img/sprite.png', { alt: 'Sprite', 'data-src': true })
188+
189+
// #4 - Load a script
187190
!= CDN('/js/script.js')
188191
189-
// #4 - Load a script with a custom tag attribute
192+
// #5 - Load a script with a custom tag attribute
190193
!= CDN('/js/script.js', { 'data-message': 'Hello' })
191194
192-
// #5 - Load and concat two scripts
195+
// #6 - Load and concat two scripts
193196
!= CDN([ '/js/plugins.js', '/js/script.js' ])
194197
195-
// #6 - Load and concat two scripts with custom tag attributes
198+
// #7 - Load and concat two scripts with custom tag attributes
196199
!= CDN([ '/js/plugins.js', '/js/script.js' ], { 'data-message': 'Hello' })
197200
198-
// #7 - Load a stylesheet
201+
// #8 - Load a stylesheet
199202
!= CDN('/css/style.css')
200203
201-
// #8 - Load and concat two stylesheets
204+
// #9 - Load and concat two stylesheets
202205
!= CDN([ '/css/style.css', '/css/extra.css' ])
203206
204-
// #9 - Load a favicon
207+
// #10 - Load a favicon
205208
!= CDN('/img/favicon.ico')
206209
```
207210

@@ -214,25 +217,28 @@ app.listen(1337);
214217
<!-- #2 - Load an image with a custom tag attribute -->
215218
<%- CDN('/img/sprite.png', { alt: 'Sprite' }) %>
216219
217-
<!-- #3 - Load a script -->
220+
<!-- #3 - Load an image with a custom tag attribute and data-src attribute instead src -->
221+
<%- CDN('/img/sprite.png', { alt: 'Sprite', 'data-src': true }) %>
222+
223+
<!-- #4 - Load a script -->
218224
<%- CDN('/js/script.js') %>
219225
220-
<!-- #4 - Load a script with a custom tag attribute -->
226+
<!-- #5 - Load a script with a custom tag attribute -->
221227
<%- CDN('/js/script.js', { 'data-message': 'Hello' }) %>
222228
223-
<!-- #5 - Load and concat two scripts -->
229+
<!-- #6 - Load and concat two scripts -->
224230
<%- CDN([ '/js/plugins.js', '/js/script.js' ]) %>
225231
226-
<!-- #6 - Load and concat two scripts with custom tag attributes -->
232+
<!-- #7 - Load and concat two scripts with custom tag attributes -->
227233
<%- CDN([ '/js/plugins.js', '/js/script.js' ], { 'data-message': 'Hello' }) %>
228234
229-
<!-- #7 - Load a stylesheet -->
235+
<!-- #8 - Load a stylesheet -->
230236
<%- CDN('/css/style.css') %>
231237
232-
<!-- #8 - Load and concat two stylesheets -->
238+
<!-- #9 - Load and concat two stylesheets -->
233239
<%- CDN([ '/css/style.css', '/css/extra.css' ]) %>
234240
235-
<!-- #9 - Load a favicon -->
241+
<!-- #10 - Load a favicon -->
236242
<%- CDN('/img/favicon.ico') %>
237243
```
238244

@@ -247,28 +253,31 @@ app.listen(1337);
247253
<!-- #2 - Load an image with a custom tag attribute -->
248254
<img src="/img/sprite.png?v=1341214029" alt="Sprite" />
249255

250-
<!-- #3 - Load a script -->
256+
<!-- #3 - Load an image with a custom tag attribute and data-src attribute instead src -->
257+
<img data-src="/img/sprite.png?v=1341214029" alt="Sprite" data-src="true" />
258+
259+
<!-- #4 - Load a script -->
251260
<script src="/js/script.js?v=1341214029" type="text/javascript"></script>
252261

253-
<!-- #4 - Load a script with a custom tag attribute -->
262+
<!-- #5 - Load a script with a custom tag attribute -->
254263
<script src="/js/script.js?v=1341214029" type="text/javascript" data-message="Hello"></script>
255264

256-
<!-- #5 - Load and concat two scripts -->
265+
<!-- #6 - Load and concat two scripts -->
257266
<script src="/js/plugins.js?v=1341214029" type="text/javascript"></script>
258267
<script src="/js/script.js?v=1341214029" type="text/javascript"></script>
259268

260-
<!-- #6 - Load and concat two scripts with custom tag attributes -->
269+
<!-- #7 - Load and concat two scripts with custom tag attributes -->
261270
<script src="/js/plugins.js?v=1341214029" type="text/javascript" data-message="Hello"></script>
262271
<script src="/js/script.js?v=1341214029" type="text/javascript" data-message="Hello"></script>
263272

264-
<!-- #7 - Load a stylesheet -->
273+
<!-- #8 - Load a stylesheet -->
265274
<link href="/css/style.css?v=1341214029" rel="stylesheet" type="text/css" />
266275

267-
<!-- #8 - Load and concat two stylesheets -->
276+
<!-- #9 - Load and concat two stylesheets -->
268277
<link href="/css/style.css?v=1341214029" rel="stylesheet" type="text/css" />
269278
<link href="/css/extra.css?v=1341214029" rel="stylesheet" type="text/css" />
270279

271-
<!-- #9 - Load a favicon -->
280+
<!-- #10 - Load a favicon -->
272281
<link href="/img/favicon.ico?v=1341214029" rel="shortcut icon" />
273282
```
274283

@@ -288,25 +297,28 @@ timestamps together and checks if the combined asset timestamp on S3 exists!).
288297
<!-- #2 - Load an image with a custom tag attribute -->
289298
<img src="https://cdn.your-site.com/img/sprite.1341382571.png" alt="Sprite" />
290299

291-
<!-- #3 - Load a script -->
300+
<!-- #3 - Load an image with a custom tag attribute -->
301+
<img data-src="https://cdn.your-site.com/img/sprite.1341382571.png" alt="Sprite" data-src="true" />
302+
303+
<!-- #4 - Load a script -->
292304
<script src="https://cdn.your-site.com/js/script.1341382571.js" type="text/javascript"></script>
293305

294-
<!-- #4 - Load a script with a custom tag attribute -->
306+
<!-- #5 - Load a script with a custom tag attribute -->
295307
<script src="https://cdn.your-site.com/js/script.1341382571.js" type="text/javascript" data-message="Hello"></script>
296308

297-
<!-- #5 - Load and concat two scripts -->
309+
<!-- #6 - Load and concat two scripts -->
298310
<script src="https://cdn.your-site.com/plugins%2Bscript.1341382571.js" type="text/javascript"></script>
299311

300-
<!-- #6 - Load and concat two scripts with custom tag attributes -->
312+
<!-- #7 - Load and concat two scripts with custom tag attributes -->
301313
<script src="https://cdn.your-site.com/plugins%2Bscript.1341382571.js" type="text/javascript" data-message="Hello"></script>
302314

303-
<!-- #7 - Load a stylesheet -->
315+
<!-- #8 - Load a stylesheet -->
304316
<link href="https://cdn.your-site.com/css/style.1341382571.css" rel="stylesheet" type="text/css" />
305317

306-
<!-- #8 - Load and concat two stylesheets -->
318+
<!-- #9 - Load and concat two stylesheets -->
307319
<link href="https://cdn.your-site.com/style%2Bextra.1341382571.css" rel="stylesheet" type="text/css" />
308320

309-
<!-- #9 - Load a favicon -->
321+
<!-- #10 - Load a favicon -->
310322
<link href="https://cdn.your-site.com/img/favicon.1341382571.ico" rel="shortcut icon" />
311323
```
312324

lib/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ var renderAttributes = function(attributes) {
5959
return str.sort().join(" ");
6060
};
6161

62+
var handleSrcAttr = function(attributes) {
63+
return attributes['data-src'] ? 'data-src' : 'src';
64+
}
65+
6266
var createTag = function(src, asset, attributes, version) {
67+
var processedSrc = handleSrcAttr(attributes);
6368
// Cachebusting
6469
version = version || '';
6570
// Enable "raw" output
@@ -83,7 +88,7 @@ var createTag = function(src, asset, attributes, version) {
8388
case 'image/pjpeg':
8489
case 'image/gif':
8590
case 'image/svg+xml':
86-
attributes.src = src + asset + version;
91+
attributes[processedSrc] = src + asset + version;
8792
return '<img ' + renderAttributes(attributes) + ' />';
8893
case 'image/x-icon':
8994
case 'image/vnd.microsoft.icon':

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "express-cdn",
33
"description": "Node.js module for delivering optimized, minified, mangled, gzipped, and CDN-hosted assets in Express using S3 and CloudFront.",
4-
"version": "0.3.3",
4+
"version": "0.4.3",
55
"author": {
66
"name": "Nick Baugh",
77
"email": "[email protected]"

0 commit comments

Comments
 (0)