metafizzy / infinite-scroll

📜 Automatically add next page
https://infinite-scroll.com
7.41k stars 1.74k forks source link

"Last" event does not work with an 204 status response #947

Open GrupoDO opened 2 years ago

GrupoDO commented 2 years ago

Hello!

The last event does not work with HTTP 204 status response. You can see in https://conocien.do/hallstatt/actividades the loading text remains visible.

Checking the source code the problem seems to be in the block:

let fetchPromise = fetch( path, fetchOptions )
    .then( ( response ) => {
      if ( !response.ok ) {
        let error = new Error( response.statusText );
        this.onPageError( error, path, response );
        return { response };
      }
      return response[ responseBody ]().then( ( body ) => {
          console.log(body);
        let canDomParse = responseBody == 'text' && domParseResponse;
        if ( canDomParse ) {
          body = domParser.parseFromString( body, 'text/html' );
        }

        if ( response.status == 204 ) {
          this.lastPageReached( body, path );
          return { body, response };
        } else {
          return this.onPageLoad( body, path, response );
        }
      } );
    } )
    .catch( ( error ) => {
        console.log(error);
      this.onPageError( error, path );
    } );

A status 204 never returns content, it's an empty body response, so the code never enters into "response[ responseBody ]().then" and it's catched with the error "SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data"

Can you check this bug, please?

Thank you!

temuri416 commented 2 years ago

same issue here. by definition, if HTTP response code is 204, it cannot contain body. the code in fetchPromise is incorrect.