go_router navigation patterns and opening your own links in-app
fluttergo_routerdeep-linksnavigationflutter-weburl
Notes from the TLDR app on go_router: when to go versus push, how to make absolute links to your
own site open inside the app by asking the live router, and how routes for logged-out viewers and
email links fit in.
go vs push
context.go()replaces the location and updates the browser URL on web;push()does not. Usegofor nav-bar destinations and menu links (several menu links once usedpushand left the URL stale).push()keeps the previous screen mounted, so its scroll position and filters survive. TLDR uses push only for list to post detail.- Do not give a list route
key: UniqueKey(): it forces a rebuild and kills state on every visit. Static fields saved indispose()and restored ininitState()cover the cases where the screen really is remounted. - Do not set
initialLocation; it overrides the URL on a web refresh. Redirect/to the home route instead.
Opening your own URLs in the app
Markdown and linkified text produce absolute links like https://tldrnerd.com/posts/1608 (that is
what the share button copies). Before v1.52.2 these opened a browser tab. The fix is one tap handler
used everywhere links are tappable:
void openLink(BuildContext context, String href) {
// relative "/..." -> context.go
// app-host URL -> internal path, if the live router can match it -> context.go
// any other http(s) -> openUrlExternal
}
internalPathForUrl()mapshttp(s)://<app host>/<path>?q#fto/<path>?q#fand returns null for other hosts, including the API host (uploads must stay downloadable).- Ask the router itself instead of keeping an allowlist:
GoRouter.of(context).configuration.findMatch(uri).isError == false. New routes become linkable automatically, while static docroot files (/tldr.apk,/version.json) still open externally. findMatchdoes not run redirects. The site root has no route of its own (it redirects to/posts), so map/explicitly or root links fall through to the browser. A unit test caught it.
Routes for people who are not logged in
A public read-only page (/s/:token) must sit outside the ShellRoute and short-circuit the
login redirect, and its screen must not use any widget that assumes a session (avatars, mention
linkifying, authenticated tiles).
Links from email and other apps
- Pages that cold-load from email (password reset, verify email) were moved out of the Flutter app into plain static HTML pages, which load instantly. See Flutter web (renderers, SEO, performance, when not to use it).
- For "open the app" buttons a custom scheme (
tldr://app/<path>) was chosen over https App Links: App Links would intercept the email link itself and skip the static page. Flutter's default deep linking hands the path to go_router. Test withadb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "tldr://app/login". From a browser, anintent://app/login#Intent;scheme=tldr;package=<pkg>;S.browser_fallback_url=...;endlink opens the app or falls back.
See also
- Android back button in Flutter with go_router (and Android 16 predictive back)
- "Anyone with the link" share links (capability URLs)