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

feat: support preloadedBundles in WebF #500

Merged
merged 3 commits into from
Nov 6, 2023
Merged

feat: support preloadedBundles in WebF #500

merged 3 commits into from
Nov 6, 2023

Conversation

andycall
Copy link
Member

@andycall andycall commented Oct 3, 2023

This PR adds a new preloadedBundles property to the WebF widget, allowing users to preload external resources (HTML, CSS, JavaScript, or Images) before WebF is mounted in Flutter.

This feature can save a lot of time if all resources are preloaded before WebF is mounted.

Here is a example to use this feature:

Future<List<WebFBundle>> preloadWebFBundles(List<String> entryPoints) async {
    List<Future<WebFBundle>> bundles = entryPoints.map((e) async {
      WebFBundle bundle = WebFBundle.fromUrl(e);
      await bundle.resolve();
      await bundle.obtainData();
      return bundle;
    }).toList();
    return await Future.wait(bundles);
  }

@override
  Widget build(BuildContext context) {
    final MediaQueryData queryData = MediaQuery.of(context);
    final Size viewportSize = queryData.size;
    return Scaffold(
        body: Center(
          child: Column(
            children: [
              FutureBuilder<List<WebFBundle>>(future: preloadWebFBundles([
                // The lists with absolute path for remote resources
                'assets:assets/bundle.html',
                'assets:assets/bundle.js',
                'assets:assets/webf.png',
              ]), builder: (BuildContext context, AsyncSnapshot<List<WebFBundle>> snapshot) {
                if (snapshot.data != null) {
                  return WebF(
                    devToolsService: ChromeDevToolsService(),
                    viewportWidth: viewportSize.width - queryData.padding.horizontal,
                    viewportHeight: viewportSize.height - appBar.preferredSize.height - queryData.padding.vertical,
                    preloadedBundles: snapshot.data,
                    bundle: snapshot.data![0]
                  );
                }

                return Text('Load error');
              })
            ],
          ),
        ));
  }

Copy link
Member

@XGHeaven XGHeaven left a comment

Choose a reason for hiding this comment

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

Update the docs by the way.

@andycall andycall added this pull request to the merge queue Nov 6, 2023
Merged via the queue into main with commit e4efa00 Nov 6, 2023
12 of 13 checks passed
@andycall andycall deleted the feat/pre_load branch November 6, 2023 10:26
andycall added a commit that referenced this pull request Nov 18, 2023
This PR adds a new preloadedBundles property to the WebF widget,
allowing users to preload external resources (HTML, CSS, JavaScript, or
Images) before WebF is mounted in Flutter.

This feature can save a lot of time if all resources are preloaded
before WebF is mounted.

Here is a example to use this feature:


```dart
Future<List<WebFBundle>> preloadWebFBundles(List<String> entryPoints) async {
    List<Future<WebFBundle>> bundles = entryPoints.map((e) async {
      WebFBundle bundle = WebFBundle.fromUrl(e);
      await bundle.resolve();
      await bundle.obtainData();
      return bundle;
    }).toList();
    return await Future.wait(bundles);
  }

@OverRide
  Widget build(BuildContext context) {
    final MediaQueryData queryData = MediaQuery.of(context);
    final Size viewportSize = queryData.size;
    return Scaffold(
        body: Center(
          child: Column(
            children: [
              FutureBuilder<List<WebFBundle>>(future: preloadWebFBundles([
                // The lists with absolute path for remote resources
                'assets:assets/bundle.html',
                'assets:assets/bundle.js',
                'assets:assets/webf.png',
              ]), builder: (BuildContext context, AsyncSnapshot<List<WebFBundle>> snapshot) {
                if (snapshot.data != null) {
                  return WebF(
                    devToolsService: ChromeDevToolsService(),
                    viewportWidth: viewportSize.width - queryData.padding.horizontal,
                    viewportHeight: viewportSize.height - appBar.preferredSize.height - queryData.padding.vertical,
                    preloadedBundles: snapshot.data,
                    bundle: snapshot.data![0]
                  );
                }

                return Text('Load error');
              })
            ],
          ),
        ));
  }

```
andycall added a commit that referenced this pull request Mar 20, 2024
1. The HTTP cache relies on the contextId to function. However, this
[PR](#500) introduced a bug that
make the entire HTTP cache mode ineffective.
2. According to profile data, we found that the
`getCrc32(bytes.toList())` takes lots of time with unexpected and can be
optimized with `getCrc32(bytes)` instead.


<img width="761" alt="image"
src="https://github.com/openwebf/webf/assets/4409743/7f934923-894d-431d-b43d-8e4c9e3d4baa">

3. The`CookieJar.setCookieString` also take lots of times unexpected and
can be optimized by making the file write async.
<img width="650" alt="image"
src="https://github.com/openwebf/webf/assets/4409743/a103bb0c-a95b-4387-be08-bdcd694cf73e">
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