Skip to content

Commit

Permalink
fix(package_info_plus)!: allow no page extension in versionJsonUrl on…
Browse files Browse the repository at this point in the history
… web (#2381)

Co-authored-by: Nicolas Dion Bouchard <nicolas.dionbouchard@sviesolutions.com>
  • Loading branch information
NicolasDionB and Nicolas Dion Bouchard authored Nov 20, 2023
1 parent 64b9931 commit 32652b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> segments = [...uri.pathSegments]
..removeWhere((element) => element == '');

// Add file and cachebuster query
return uri.replace(
query: 'cachebuster=$cacheBuster',
pathSegments: [...segments, 'version.json']);
}

@override
Expand Down

0 comments on commit 32652b8

Please sign in to comment.