diff --git a/packages/package_info_plus/package_info_plus/example/integration_test/package_info_plus_web_test.dart b/packages/package_info_plus/package_info_plus/example/integration_test/package_info_plus_web_test.dart index c78ba2aadb..294b8a9a45 100644 --- a/packages/package_info_plus/package_info_plus/example/integration_test/package_info_plus_web_test.dart +++ b/packages/package_info_plus/package_info_plus/example/integration_test/package_info_plus_web_test.dart @@ -104,9 +104,10 @@ void main() { Uri.parse('https://example.com/a/b/c/version.json?cachebuster=1'), ); expect( + // 'c' here is to be considered as a page, not a folder plugin.versionJsonUrl( 'https://example.com/a/b/c?hello_world=true#/my-page', 1), - Uri.parse('https://example.com/a/b/c/version.json?cachebuster=1'), + Uri.parse('https://example.com/a/b/version.json?cachebuster=1'), ); }, ); diff --git a/packages/package_info_plus/package_info_plus/lib/src/package_info_plus_web.dart b/packages/package_info_plus/package_info_plus/lib/src/package_info_plus_web.dart index f11542abb4..21a4a3b793 100644 --- a/packages/package_info_plus/package_info_plus/lib/src/package_info_plus_web.dart +++ b/packages/package_info_plus/package_info_plus/lib/src/package_info_plus_web.dart @@ -24,12 +24,28 @@ class PackageInfoPlusWebPlugin extends PackageInfoPlatform { Uri versionJsonUrl(String baseUrl, int cacheBuster) { final baseUri = Uri.parse(baseUrl); final regExp = RegExp(r'[^/]+\.html.*'); - final originPath = + final String originPath = '${baseUri._origin}${baseUri.path.replaceAll(regExp, '')}'; - final versionJson = 'version.json?cachebuster=$cacheBuster'; - return Uri.parse(originPath.endsWith('/') - ? '$originPath$versionJson' - : '$originPath/$versionJson'); + + // Clean uri: remove fragment and query + Uri uri = Uri.parse(originPath).removeFragment().replace(query: ''); + + // In http/s, if the last part has no file extension and not trailing slash, + // we can safely remove it since it's not considered as a folder + if (uri.path.length > 1 && + !uri.path.endsWith('/') && + (uri.isScheme('http') || uri.isScheme('https'))) { + uri = uri.replace(path: uri.path.substring(0, uri.path.lastIndexOf('/'))); + } + + // Clean uri: remove empty path segments + final List segments = [...uri.pathSegments] + ..removeWhere((element) => element == ''); + + // Add file and cachebuster query + return uri.replace( + query: 'cachebuster=$cacheBuster', + pathSegments: [...segments, 'version.json']); } @override