Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileLoader: Add response headers to onProgress callback #23896

Conversation

JordyvanDortmont
Copy link
Contributor

Description

Since #22510 the response headers can't be accessed anymore in the progress callback. A custom response header can be used to send the uncompressed content length of large compressed files (gzip, brotli) to report loading progress to the user. The total content length currently reported by the ProgressEvent, is the content length of the compressed file and the loaded bytes are currently reported for the uncompressed file loading progress of the ReadableStream. Even with the new Fetch API, this is still an unresolved issue: whatwg/fetch#1358.

Before the Fetch API was introduced, the onProgress callback received the XMLHttpRequest object as the event target, which gave access to the response headers.

This PR adds the response headers as an argument to the onProgress callback to provide access to the response headers again.

@JordyvanDortmont JordyvanDortmont changed the title File loader response headers FileLoader: Add response headers to onProgress callback Apr 13, 2022
Comment on lines 125 to 129
for ( let i = 0, il = callbacks.length; i < il; i ++ ) {

const callback = callbacks[ i ];
if ( callback.onProgress ) callback.onProgress( event );
if ( callback.onProgress ) callback.onProgress( event, response.headers );

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking instead of creating a ProgressEvent instance we just create a custom object with the information we want in the object. Something like:

const event = {
  lengthComputable,
  loaded,
  total,
  response,
};

There's no reason the passed object has to be a ProgressEvent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be const event = new ProgressEvent( 'progress', { lengthComputable, loaded, total, response } );?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No from the docs "response" is not a valid field for ProgressEvent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes... Then the object approach is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be great as well. Let me know if I should make the change, then I can change the docs accordingly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to defer to @mrdoob and @Mugen87 on whether or not the object approach is preferred.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can start with a custom object and if someone complains we fallback to the initial approach of the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to a custom object including the response and I also updated the docs.

@JordyvanDortmont JordyvanDortmont force-pushed the file-loader-response-headers branch from c53e97e to 31558d0 Compare April 19, 2022 19:57
@Mugen87 Mugen87 added this to the r140 milestone Apr 20, 2022
@mrdoob mrdoob modified the milestones: r140, r141 Apr 30, 2022
@mrdoob mrdoob modified the milestones: r141, r142 May 26, 2022
@mrdoob mrdoob modified the milestones: r142, r143 Jun 29, 2022
@mrdoob mrdoob modified the milestones: r143, r144 Jul 28, 2022
@mrdoob mrdoob modified the milestones: r144, r145 Aug 31, 2022
@mrdoob mrdoob modified the milestones: r145, r146 Sep 29, 2022
@mrdoob mrdoob modified the milestones: r146, r147 Oct 27, 2022
@mrdoob mrdoob modified the milestones: r147, r148 Nov 30, 2022
@JordyvanDortmont
Copy link
Contributor Author

This has been fixed by #24971.

@JordyvanDortmont
Copy link
Contributor Author

Unfortunately my previous comment was a bit too soon. For compressed files the compressed file size is sent with the Content-Length header, which takes precedence over the X-File-Size header currently, which could be used to specify the uncompressed file size.

To solve our issue, either the X-File-Size header needs to take precedence, or we would still need this PR.

@mrdoob mrdoob modified the milestones: r148, r149 Dec 22, 2022
@mrdoob mrdoob removed this from the r149 milestone Jan 26, 2023
@mrdoob mrdoob modified the milestones: r150, r151 Feb 23, 2023
@mrdoob mrdoob modified the milestones: r151, r152 Mar 30, 2023
@mrdoob mrdoob modified the milestones: r152, r153 Apr 27, 2023
@Mugen87 Mugen87 modified the milestones: r153, r154 Jun 1, 2023
@mrdoob mrdoob modified the milestones: r154, r155 Jun 29, 2023
@mrdoob mrdoob modified the milestones: r155, r156 Jul 27, 2023
@mrdoob mrdoob modified the milestones: r156, r157 Aug 31, 2023
@mrdoob mrdoob modified the milestones: r157, r158 Sep 28, 2023
@mrdoob mrdoob modified the milestones: r158, r159 Oct 27, 2023
@mrdoob mrdoob modified the milestones: r159, r160 Nov 30, 2023
@mrdoob mrdoob modified the milestones: r160, r161 Dec 22, 2023
@mrdoob mrdoob modified the milestones: r161, r162 Jan 31, 2024
@mrdoob mrdoob modified the milestones: r162, r163 Feb 29, 2024
@mrdoob mrdoob modified the milestones: r163, r164 Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants