From 5d81ed45531b1c897fbd12f89c3cc6e7053c858c Mon Sep 17 00:00:00 2001 From: Nicolas Dion Bouchard Date: Fri, 17 Nov 2023 06:44:18 -0500 Subject: [PATCH 1/2] fix: allow no page extension in versionJsonUrl on web --- .../lib/src/package_info_plus_web.dart | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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..69205a05a5 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,17 @@ class PackageInfoPlusWebPlugin extends PackageInfoPlatform { Uri versionJsonUrl(String baseUrl, int cacheBuster) { final baseUri = Uri.parse(baseUrl); final regExp = RegExp(r'[^/]+\.html.*'); - final originPath = - '${baseUri._origin}${baseUri.path.replaceAll(regExp, '')}'; - final versionJson = 'version.json?cachebuster=$cacheBuster'; - return Uri.parse(originPath.endsWith('/') - ? '$originPath$versionJson' - : '$originPath/$versionJson'); + final String originPath = '${baseUri._origin}${baseUri.path.replaceAll(regExp, '')}'; + + // Remove fragment and query + Uri uri = Uri.parse(originPath).replace(fragment: '', query: ''); + + // Remove file or last part since it's not a folder + final String basePath = uri.toString(); + if (!basePath.endsWith('/')) uri = Uri.parse(basePath.substring(0, basePath.lastIndexOf('/'))); + + // Add file and cachebuster query + return uri.replace(query: 'cachebuster=$cacheBuster', pathSegments: [...uri.pathSegments, 'version.json']); } @override From 7d2a5418a31d87bc60d8fcafc0b16b3729819fb2 Mon Sep 17 00:00:00 2001 From: Nicolas Dion Bouchard Date: Fri, 17 Nov 2023 08:23:00 -0500 Subject: [PATCH 2/2] minor adjustments to match tests --- .../package_info_plus_web_test.dart | 3 ++- .../lib/src/package_info_plus_web.dart | 25 +++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) 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 69205a05a5..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,17 +24,28 @@ class PackageInfoPlusWebPlugin extends PackageInfoPlatform { Uri versionJsonUrl(String baseUrl, int cacheBuster) { final baseUri = Uri.parse(baseUrl); final regExp = RegExp(r'[^/]+\.html.*'); - final String originPath = '${baseUri._origin}${baseUri.path.replaceAll(regExp, '')}'; + final String originPath = + '${baseUri._origin}${baseUri.path.replaceAll(regExp, '')}'; - // Remove fragment and query - Uri uri = Uri.parse(originPath).replace(fragment: '', query: ''); + // Clean uri: remove fragment and query + Uri uri = Uri.parse(originPath).removeFragment().replace(query: ''); - // Remove file or last part since it's not a folder - final String basePath = uri.toString(); - if (!basePath.endsWith('/')) uri = Uri.parse(basePath.substring(0, basePath.lastIndexOf('/'))); + // 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: [...uri.pathSegments, 'version.json']); + return uri.replace( + query: 'cachebuster=$cacheBuster', + pathSegments: [...segments, 'version.json']); } @override