Repository navigation
Description
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
[-]Disbale the browser navigaiton stack in flutter web app[/-][+]Disable the browser navigation stack in the Flutter web app [/+][-]Disable the browser navigation stack in the Flutter web app [/-][+][FEATURE REQUEST] Disable the browser navigation stack in the Flutter web app [/+][-][FEATURE REQUEST] Disable the browser navigation stack in the Flutter web app [/-][+][Proposal] Disable the browser navigation stack in the Flutter web app [/+]maheshj01 commentedon Jul 22, 2022
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.
maheshj01 commentedon Jul 22, 2022
cc: @mdebbar
kodejack commentedon Jul 27, 2022
We looked at WillPop, but it doesn't seem to work correctly on web and didn't always suit our scenario.
yjbanov commentedon Jul 28, 2022
Have you tried using a custom
UrlStrategy?https://api.flutter.dev/flutter/package-flutter_web_plugins_url_strategy/UrlStrategy-class.html
kodejack commentedon Aug 2, 2022
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.
6 remaining items
kodejack commentedon Sep 3, 2022
@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.
kodejack commentedon Sep 4, 2022
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
web_url_strategy.dart
Then optional includes
Is this a recommended approach and should it be part of the framework?
mdebbar commentedon Sep 8, 2022
@kodejack that's the right way to do it, yes. But we already do this for you in:
flutter/packages/flutter_web_plugins/lib/url_strategy.dart
Lines 7 to 8 in fc444ae
If you wanna take advantage of that, and keep your code simple, you can import
url_strategy.dartdirectly instead of importing all offlutter_web_plugins.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.
janknips commentedon Dec 15, 2022
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:
azambuilds commentedon Feb 2, 2023
@janknips how did you figure this out? This is exactly what I was looking for! Thanks!
PriyaranjanEuphotic commentedon Jun 1, 2023
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
bobosette commentedon Jul 19, 2023
you saved my life!!!
millarboy commentedon Dec 28, 2023
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
adamcastleman commentedon May 11, 2024
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;
}
};
</script>
`
yuhao-nyc commentedon Nov 5, 2025
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