33var isUrl = require ( 'is-url' ) , // module use to verify the sourceUrl is an actual URL
44 cheerio = require ( 'cheerio' ) , // used to convert raw html into jQuery style object to we can use css selectors
55 request = require ( 'request-promise' ) , // promise based request object
6- NodeCache = require ( " node-cache" ) , // auto expiring in memory caching collection
6+ NodeCache = require ( ' node-cache' ) , // auto expiring in memory caching collection
77 cache = new NodeCache ( { stdTTL : 300 } ) ; // cache source HTML for 5 minutes, after which we fetch a new version
88
9- module . exports = function ( req , res , next ) {
9+ module . exports = function ( req , res , next ) {
1010
11- // add res.merge to response object to allow us to fetch template url or use html template string
12- res . merge = function ( view , model , callback ) {
13-
14- if ( ! model . sourceUrl ) {
11+ // add res.merge to response object to allow us to fetch template url or use html template string
12+ res . merge = function ( view , model , callback ) {
13+
14+ if ( ! model . sourceUrl ) {
1515 // no .sourceUrl, therefore process this as normal
1616 res . render ( view , model , callback ) ;
17- }
17+ }
1818 else {
1919
2020 // if no template selector provided, use body tag
2121 var sourcePlaceholder = model . sourcePlaceholder || 'body' ;
22-
22+
2323 // resolve the template, either url to jquery object or html
24- resolveTemplate ( model . sourceUrl ) . then ( function ( $template ) {
25-
24+ resolveTemplate ( model . sourceUrl ) . then ( function ( $template ) {
25+
2626 // if a transform method is provided, execute
27- if ( model . transform ) {
27+ if ( model . transform ) {
2828 // pass a jquery version of the html along with a copy of the model
2929 model . transform ( $template , model ) ;
3030 }
31-
31+
3232 // convert view into html to be inserted
3333 res . render ( view , model , function ( err , html ) {
3434 if ( err ) next ( err ) ;
35-
35+
3636 // convert view into jquery object
3737 var $view = cheerio . load ( html ) ;
38-
38+
3939 // if the view contains a head, append its contents to the original
4040 var $viewHead = $view ( 'head' ) ;
41- if ( $viewHead && $viewHead . length > 0 ) {
41+ if ( $viewHead && $viewHead . length > 0 ) {
4242 // append meta, link, script and noscript to head
4343 $template ( 'head' ) . append ( $viewHead . html ( ) ) ;
4444 }
45-
46- // if the view has a body, use its contents otherwise use the entire content
45+
46+ // if the view has a body, use its contents otherwise use the entire content
4747 var $viewBody = $view ( 'body' ) ;
4848
49- if ( $viewBody && $viewBody . length > 0 ) {
49+ if ( $viewBody && $viewBody . length > 0 ) {
5050 // inject content into selector or body
5151 $template ( sourcePlaceholder ) . html ( $viewBody . html ( ) ) ;
5252 }
5353 else {
5454 // no body tag in view, insert all content
5555 $template ( sourcePlaceholder ) . html ( $view . html ( ) ) ;
5656 }
57-
57+
5858 // return merged content
5959 res . status ( 200 ) . send ( $template . html ( ) ) ;
6060 } ) ;
6161 } )
62- . catch ( function ( err ) {
62+ . catch ( function ( err ) {
6363 // request failed, inform the user
6464 res . status ( 500 ) . send ( err ) . end ( ) ;
6565 } ) ;
6666 }
6767 } ;
68-
68+
6969 next ( ) ;
70- }
70+ } ;
7171
7272/**
73- * Converts source url to $ object
73+ * @description Converts source url to $ object
7474 * @param {string } sourceUrl - url to external html content
75+ * @returns {Promise } returns promise of html source from sourceUrl
7576 */
76- function resolveTemplate ( sourceUrl ) {
77-
78- return new Promise ( function ( resolve , reject ) {
79-
77+ function resolveTemplate ( sourceUrl ) {
78+
79+ return new Promise ( function ( resolve , reject ) {
80+
8081 // if its a url and we have the contents in cache
81- if ( isUrl ( sourceUrl ) && cache . get ( sourceUrl ) ) {
82-
82+ if ( isUrl ( sourceUrl ) && cache . get ( sourceUrl ) ) {
83+
8384 // get source html from cache
8485 var html = cache . get ( sourceUrl ) ;
85-
86+
8687 // covert html into jquery object
8788 var $ = cheerio . load ( html ) ;
88-
89+
8990 // return source as a jquery style object
9091 resolve ( $ ) ;
9192 }
92- else if ( isUrl ( sourceUrl ) ) {
93-
93+ else if ( isUrl ( sourceUrl ) ) {
94+
9495 // request the source url
9596 return request ( { uri : sourceUrl } ) . then ( function ( html ) {
96-
97+
9798 // convert html into jquery style object so we can use selectors
9899 var $ = cheerio . load ( html ) ;
99-
100+
100101 // insert base tag to ensure links/scripts/styles load correctly
101102 $ ( 'head' ) . prepend ( '<base href="' + sourceUrl + '">' ) ;
102-
103+
103104 // cache result as HTML so we dont have to keep getting it for future requests and it remains clean
104105 cache . set ( sourceUrl , $ . html ( ) ) ;
105-
106+
106107 // resolve with jquery object containing content
107108 resolve ( $ ) ;
108109 } )
109110 . catch ( function ( err ) {
110-
111+
111112 // request failed
112113 reject ( 'Unable to retrieve ' + sourceUrl ) ;
113114 } ) ;
@@ -117,4 +118,4 @@ function resolveTemplate(sourceUrl){
117118 resolve ( sourceUrl ) ;
118119 }
119120 } ) ;
120- } ;
121+ }
0 commit comments