auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

Allow code splitting

Open dochan-consultis opened this issue 3 years ago • 7 comments

It would be helpful if we could split code based on routes. It would mean that first page can load drastically faster because other routes and their dependecies wouldn't be loaded yet. This would affect only web version.

Since flutter has already support for it make this happen would be easy with old generator.

Instead of this:


import 'package:auto_route/auto_route.dart' as _i1;
import 'home/view/home_page.dart' as _i3;

@override
final Map<String, _i1.PageFactory> pagesMap = {
  HomeRoute.name: (routeData) {
    return _i1.MaterialPageX<Object>(
      routeData: routeData,
      child: const _i3.HomePage(),
    );
  }
};

We could do this:


import 'package:auto_route/auto_route.dart' as _i1;
import 'package:flutter/material.dart' as _i2;
import 'home/view/home_page.dart' deferred as _i3; //deferred is the secret sauce

@override
final Map<String, _i1.PageFactory> pagesMap = {
  HomeRoute.name: (routeData) {
    return _i1.MaterialPageX<Object>(
      routeData: routeData,
      child: FutureBuilder(
        future: _i3.loadLibrary(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return _i3.HomePage();
          } else {
            return _i2.Center(child: _i2.CircularProgressIndicator());
          }
        },
      ),
    );
  }
};

I'm not sure if this can be achieved when router is used with part of though.

dochan-consultis avatar Oct 22 '21 09:10 dochan-consultis

@dochan-consultis this looks interesting, have you benchmarked it? how can I measure the build time?

Milad-Akarie avatar Oct 22 '21 12:10 Milad-Akarie

I've tried it on my app and it saved around 500kb. I'll do more benchmarks (also regarding build and load time) on monday.

dochan-consultis avatar Oct 22 '21 12:10 dochan-consultis

Awesome. It works with GetX router. I don't see any reasons why it shouldn't work with others too. Cheers!!!

tyagiprince2010 avatar Oct 22 '21 18:10 tyagiprince2010

This comparison contains 18 pages of various complexity. Unfortunately I can't share code of this test. I've tried to create this comparison on example included in this project, but pages included in example are so simple that it resulted in bigger size when code splitting was used.

Here are test results.

Files:

Without splitting:

1 without_splitting_canvaskit

With splitting:

1 with_splitting_canvaskit

Load times:

Without splitting:

3 load without_splitting_canvaskit

With splitting:

3 load with_splitting_canvaskit

dochan-consultis avatar Nov 02 '21 08:11 dochan-consultis

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Aug 15 '22 08:08 github-actions[bot]

I have the problem with deferredLoading and AutoRouteWrapper - wrappedRoute() method is never called!

CustomRoute(page: HomePage, deferredLoading: true, )

class HomePage extends StatelessWidget implements AutoRouteWrapper {
  @override
  Widget wrappedRoute(BuildContext context) {}
}

slavap avatar Aug 19 '22 03:08 slavap

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Oct 18 '22 08:10 github-actions[bot]