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

Loading of no_sleep.js does not respect assetBase #19

Open
Blutchie opened this issue Feb 15, 2024 · 6 comments
Open

Loading of no_sleep.js does not respect assetBase #19

Blutchie opened this issue Feb 15, 2024 · 6 comments

Comments

@Blutchie
Copy link

I'm using a package that depends on wakelock_plus, and on the first attempt to make a web build it seemed to work fine. However, our app is designed to be embedded into other web applications and when doing so it broke because it returned a 404 not found when trying to load no_sleep.js. This is because it tries to load it from the root of the domain instead of the assetBase.

I analyzed the generated main.dart.js and fixed it locally after the built. I first thought it was a bug in Flutter and have been searching in existing issues. I found something similar and posted a comment. But then a member of the Flutter team pointed to me that this was not their code, but part of this package, and advised to use the getAssetUrl API.

Below the link of his comment with some additional details: flutter/flutter#121417 (comment)

@diegotori
Copy link
Collaborator

@Blutchie do you have an angle on what needs to be modified in no_sleep.js in order to make this work?

@Blutchie
Copy link
Author

Hi @diegotori, I just read your question. Sadly I don't know except for what you already changed based on the other comment. I'm pretty new to Flutter, I just started to work on my first project, normally I work on .NET projects..

@gabrielginter
Copy link

gabrielginter commented Aug 21, 2024

Hi, I'm having the same issue, sadly this prevents use cases like "embedded mode" (multi-view mode) or loading assets through a CDN.

I believe the method importJsLibrary in file wakelock_plus/lib/src/web_impl/import_js_library.dart should use rootBundle.load('assets....

rootBundle should take care of pointing to the correct baseUrl set with assetBase.

If you release a version, I'll be happy to test it.

@gabrielginter
Copy link

gabrielginter commented Aug 21, 2024

I'm gonna correct myself here, what you need is to resolve the URL, not load the asset, so rootBundle won't help.

After looking at other plugins that perform a similar action (fluttertoast here), it seems that removing the ./ from the url in _libraryUrl and using assetManager.getAssetUrl would fix the issue, so the function would look something like:

String _libraryUrl(String url, String pluginName) {
  if (url.startsWith('./')) {
    url = url.replaceFirst('./', '');
  }
  
  if (url.startsWith('assets/')) {
    return ui.assetManager.getAssetUrl(
      'packages/$pluginName/$url',
    );
  }
  
  return url;
}

I tested this in release mode (with a CDN as assetBase) and debug mode, both work correctly.

@sabifa
Copy link

sabifa commented Dec 2, 2024

any update?

@diegotori
Copy link
Collaborator

@sabifa Currently trying to finish off #75, which will add AssetManager based JS script loading.

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

No branches or pull requests

4 participants