Skip to content

[Proposal] Disable the browser navigation stack in the Flutter web app  #108114

Open

Description

@kodejack

Disable the browser navigation stack in the Flutter web app to allow a Flutter app to act more like a Single Page Application (SPA) and manage its navigation internally

Use case

We have an application for iOS, Android and Web. The app works perfectly on mobile platforms, but the browser back button causes mayhem for the web application. The application has many dynamically generated pages, which makes the browser history very difficult to manage. If the user uses the navigation in the flutter app, everything is good. If the user starts using the browser's back and forward navigation, The app will regularly fall over due to this being wrong. We have no requirement for deep linking and would use the app as if it was SPA and let Flutter manage the navigation separately from the browser. i.e. if you hit back on the browser, you leave the page.

We have looked at various ways to manage or override this behaviour, including route guards, but have failed to find a reasonable solution.
We also tried using w iFrame, which caused cross-site scripting issues with our authentication.

We have written several web apps in Flutter, and all of them have experienced this issue in some way.

Proposal

What we like is the ability to switch a flutter web app from a website with full navigation controlled by the browser and Flutter to a web app that handles its own navigation. Similar to it being contained in an iframe.

If there is already a way to do this, I would love to know. Alas, I have not found a way to this as yet.

Activity

  1. changed the title [-]Disbale the browser navigaiton stack in flutter web app[/-] [+]Disable the browser navigation stack in the Flutter web app [/+] on Jul 21, 2022
  2. changed the title [-]Disable the browser navigation stack in the Flutter web app [/-] [+][FEATURE REQUEST] Disable the browser navigation stack in the Flutter web app [/+] on Jul 21, 2022
  3. added
    in triage
    on Jul 22, 2022
  4. changed the title [-][FEATURE REQUEST] Disable the browser navigation stack in the Flutter web app [/-] [+][Proposal] Disable the browser navigation stack in the Flutter web app [/+] on Jul 22, 2022
  5. maheshj01 commented on Jul 22, 2022

    @maheshj01
    Member

    Hi @kodejack, Thanks for filing the issue. This looks like a valid feature request. Perhaps you can try using WillPopScope and always return false. Labeling this issue for further insights from the team.

  6. added
    c: new feature
    platform-web
    c: proposal
    and removed
    in triage
    on Jul 22, 2022
  7. maheshj01 commented on Jul 22, 2022

    @maheshj01
    Member
  8. added
    framework
    on Jul 22, 2022
  9. kodejack commented on Jul 27, 2022

    @kodejack
    Author

    We looked at WillPop, but it doesn't seem to work correctly on web and didn't always suit our scenario.

  10. added
    waiting for response
    on Jul 28, 2022
  11. kodejack commented on Aug 2, 2022

    @kodejack
    Author

    I dont think this will fix the issue as we have a number of pages that are dynamic. All call "FormStack". These are generated on they fly depedning the form branching logic. I will have a closer look at this as it might some of the issue but not the dynamic pages.

  12. 6 remaining items

  13. kodejack commented on Sep 3, 2022

    @kodejack
    Author

    @mdebbar this seems to be working as desired, The browser nav stack and hence the buttons are being ignored. This looks like we could be onto a winner. I'll try a few other things and let you know if this is pretty solid.

  14. kodejack commented on Sep 4, 2022

    @kodejack
    Author

    After trying a few things this seems to fix the web browser nav stack but it makes it incompatible with the iOS and Android apps. So to work around it I created a file for web and mobile.

    mobile_url_strategy.dart

    void configureUrlStrategy() {
      // No strategy for mobile
    }

    web_url_strategy.dart

    import 'package:flutter_web_plugins/flutter_web_plugins.dart';
    
    void configureUrlStrategy() {
      setUrlStrategy(null);
    }

    Then optional includes

    import 'package:my_app/mobile_url_strategy.dart'
        if (dart.library.html) 'package:my_app/web_url_strategy.dart';
    
    void main() {
      configureUrlStrategy();
    }

    Is this a recommended approach and should it be part of the framework?

  15. mdebbar commented on Sep 8, 2022

    @mdebbar
    Contributor

    @kodejack that's the right way to do it, yes. But we already do this for you in:

    export 'src/navigation_non_web/url_strategy.dart'
    if (dart.library.html) 'src/navigation/url_strategy.dart';

    If you wanna take advantage of that, and keep your code simple, you can import url_strategy.dart directly instead of importing all of flutter_web_plugins.dart.

    import 'package:flutter_web_plugins/url_strategy.dart';

    This should work in mobile and web. On mobile, it's implemented as a no-op, similar to what you did in your code sample.

  16. janknips commented on Dec 15, 2022

    @janknips

    If anyone else comes across this, and needs to have the URL update, but no back button or history.

    Just call setUrlStrategy() with the following:

    class NoHistoryUrlStrategy extends PathUrlStrategy {
      @override
      void pushState(Object? state, String title, String url) =>
          replaceState(state, title, url);
    }
    
  17. azambuilds commented on Feb 2, 2023

    @azambuilds

    @janknips how did you figure this out? This is exactly what I was looking for! Thanks!

  18. PriyaranjanEuphotic commented on Jun 1, 2023

    @PriyaranjanEuphotic

    @janknips how did you figure this out? This is exactly what I was looking for! Thanks!

    If anyone else comes across this, and needs to have the URL update, but no back button or history.

    Just call setUrlStrategy() with the following:

    class NoHistoryUrlStrategy extends PathUrlStrategy {
      @override
      void pushState(Object? state, String title, String url) =>
          replaceState(state, title, url);
    }
    

    its working, but one problem, when I am pressing back button its completely exiting from app, is there any way to display a confirmation dialog

  19. added
    team-web
    triaged-web
    on Jul 8, 2023
  20. bobosette commented on Jul 19, 2023

    @bobosette

    In order to disable all browser history, you could do something like this:

    import 'package:flutter_web_plugins/url_strategy.dart';
    
    void main() {
      // Before running your app:
      setUrlStrategy(null);
    
      runApp(...);
    }

    Of course, you'll need to add the flutter_web_plugins package to your pubspec.yaml:

    dependencies:
      flutter_web_plugins:
        sdk: flutter

    @kodejack is this what you're looking for?

    you saved my life!!!

  21. millarboy commented on Dec 28, 2023

    @millarboy

    Yo, I am definitely under qualified to be commenting here, However I did have some issues with hiding the safari bar in my flutter mobile web app so in case I understood the issue i fixed it by wrapping everything on the page with a non scrolling container.... it made my system nav go.... sorry if this is completely out of context

  22. adamcastleman commented on May 11, 2024

    @adamcastleman

    Insert this script into you index.html file in your web folder to disable mobile browser gesture swiping

    `<script>
    window.addEventListener('touchstart', handleTouchStart, false);
    window.addEventListener('touchmove', handleTouchMove, {passive: false});

    let xDown = null;
    let yDown = null;

    function getTouches(evt) {
    return evt.touches || evt.originalEvent.touches;
    }

    function handleTouchStart(evt) {
    const firstTouch = getTouches(evt)[0];
    xDown = firstTouch.clientX;
    yDown = firstTouch.clientY;
    };

    function handleTouchMove(evt) {
    if (!xDown || !yDown) {
    return;
    }

      let xUp = evt.touches[0].clientX;                                    
      let yUp = evt.touches[0].clientY;
    
      let xDiff = xDown - xUp;
      let yDiff = yDown - yUp;
    
      if (Math.abs(xDiff) > Math.abs(yDiff)) {
          evt.preventDefault();
      } 
    
      // Reset values
      xDown = null;
      yDown = null;                                             
    

    };
    </script>
    `

  23. yuhao-nyc commented on Nov 5, 2025

    @yuhao-nyc

    I have a similar case where my Flutter web app is embedded inside a React app as an iframe.

    And the Flutter web app is somehow messing the parent React app's navigation. Whenever I launch the React app (with Flutter web app embedded as iframe) in Chrome I can't navigation back or forward with Chrome

    What's interesting, on Safari everything is fine

    @mdebbar @kodejack @darshankawar I tried this solution you guys suggested here it did not help me 🤔

    Any insights in appreciated

    import 'package:flutter_web_plugins/url_strategy.dart';
    
    void main() {
      // Before running your app:
      setUrlStrategy(null);
    
      runApp(...);
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3c: new featurec: proposalcustomer: quake (g3)frameworkplatform-webteam-webtriaged-web

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions