[web] Assert route names start with / if using PathUrlStrategy (#127986)

To avoid confusion and unpredictable errors (e.g. https://github.com/flutter/flutter/issues/127462), I'm adding this assert to disallow this pattern.

Fixes https://github.com/flutter/flutter/issues/127462
This commit is contained in:
Mouad Debbar
2023-06-01 11:31:24 -04:00
committed by GitHub
parent 13db2e4a76
commit 764f4cc8c2
2 changed files with 33 additions and 7 deletions

View File

@@ -63,9 +63,15 @@ class PathUrlStrategy extends ui_web.HashUrlStrategy {
@override
String prepareExternalUrl(String internalUrl) {
if (internalUrl.isNotEmpty && !internalUrl.startsWith('/')) {
internalUrl = '/$internalUrl';
if (internalUrl.isEmpty) {
internalUrl = '/';
}
assert(
internalUrl.startsWith('/'),
"When using PathUrlStrategy, all route names must start with '/' because "
"the browser's pathname always starts with '/'. "
"Found route name: '$internalUrl'",
);
return '$_basePath$internalUrl';
}
}