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: Support environments without ReadableStream #23032

Merged
merged 1 commit into from
Dec 21, 2021

Conversation

gkjohnson
Copy link
Collaborator

Related issue: #23015

Description

Adds a check to FileLoader to skip using ReadableStream and response.body.getReader if they are not supported by the current environment. Both of these were only added to Firefox within the last couple years and it seems most polyfills do not provide support for these, either.

With this change you can use the node-fetch package to enable FileLoader in node environments:

import { OBJLoader } from './examples/jsm/loaders/OBJLoader.js';
import fetch, { Request, Response, Headers } from 'node-fetch';

global.fetch = fetch;
global.Request = Request;
global.Response = Response;
global.Headers = Headers;

new OBJLoader()
  .loadAsync( 'https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/models/obj/tree.obj' ).then( res => {

    console.log(res);

  } );

This doesn't completely address the react-native case, though. In order for that to be fixed FileReader.readAsArrayBuffer must be implemented in react native because it's used by the fetch polyfill they're depending on. facebook/react-native#30769 has implemented the function but unfortunately it's gotten no attention from the project.

cc @CodyJasonBennett

@CodyJasonBennett
Copy link
Contributor

Thanks for the follow-up.

Yes, there's nothing more to do on our side for react-native, but it'll work OOTB with a few pending fixes. They can use r133 in the meantime.

I tested this locally on Node with a few setups, and it works nicely. Users should still try to avoid SSR with loaders that require browser APIs, so nothing's new there.

@mrdoob mrdoob added this to the r136 milestone Dec 21, 2021
@mrdoob mrdoob merged commit 483cf30 into mrdoob:dev Dec 21, 2021
@mrdoob
Copy link
Owner

mrdoob commented Dec 21, 2021

Thanks!

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.

None yet

3 participants