11/*!
2- * Infinite Scroll PACKAGED v4.0.0-beta.0
2+ * Infinite Scroll PACKAGED v4.0.0
33 * Automatically add next page
44 *
55 * Licensed GPLv3 for open source use
@@ -844,11 +844,10 @@ proto.loadNextPage = function() {
844844 this . isLoading = true ;
845845 if ( typeof fetchOptions == 'function' ) fetchOptions = fetchOptions ( ) ;
846846
847- // TODO add fetch options
848847 let fetchPromise = fetch ( path , fetchOptions ) . then ( ( response ) => {
849848 if ( ! response . ok ) {
850849 let error = new Error ( response . statusText ) ;
851- this . onPageError ( error , path ) ;
850+ this . onPageError ( error , path , response ) ;
852851 return { response } ;
853852 }
854853
@@ -866,7 +865,7 @@ proto.loadNextPage = function() {
866865 } ) ;
867866 } ) ;
868867
869- this . dispatchEvent ( 'request' , null , [ path ] ) ;
868+ this . dispatchEvent ( 'request' , null , [ path , fetchPromise ] ) ;
870869
871870 return fetchPromise ;
872871} ;
@@ -883,18 +882,25 @@ proto.onPageLoad = function( body, path, response ) {
883882} ;
884883
885884proto . appendNextPage = function ( body , path , response ) {
886- let { append, responseBody } = this . options ;
885+ let { append, responseBody, domParseResponse } = this . options ;
887886 // do not append json
888- let isDocument = responseBody == 'text' ;
887+ let isDocument = responseBody == 'text' && domParseResponse ;
889888 if ( ! isDocument || ! append ) return { body, response } ;
890889
891890 let items = body . querySelectorAll ( append ) ;
891+ let promiseValue = { body, response, items } ;
892+ // last page hit if no items. #840
893+ if ( ! items || ! items . length ) {
894+ this . lastPageReached ( body , path ) ;
895+ return promiseValue ;
896+ }
897+
892898 let fragment = getItemsFragment ( items ) ;
893899 let appendReady = ( ) => {
894900 this . appendItems ( items , fragment ) ;
895901 this . isLoading = false ;
896- this . dispatchEvent ( 'append' , null , [ body , path , items ] ) ;
897- return { body , response , items } ;
902+ this . dispatchEvent ( 'append' , null , [ body , path , items , response ] ) ;
903+ return promiseValue ;
898904 } ;
899905
900906 // TODO add hook for option to trigger appendReady
@@ -1000,10 +1006,10 @@ proto.lastPageReached = function( body, path ) {
10001006
10011007// ----- error ----- //
10021008
1003- proto . onPageError = function ( error , path ) {
1009+ proto . onPageError = function ( error , path , response ) {
10041010 this . isLoading = false ;
10051011 this . canLoad = false ;
1006- this . dispatchEvent ( 'error' , null , [ error , path ] ) ;
1012+ this . dispatchEvent ( 'error' , null , [ error , path , response ] ) ;
10071013 return error ;
10081014} ;
10091015
0 commit comments