Skip to content

Commit 6eaecd2

Browse files
committed
Moves the ?compiler= query param to the expected place for legacy URLs
1 parent 22c619f commit 6eaecd2

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

packages/devtools_app/lib/src/shared/primitives/url_utils.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ String extractCurrentPageFromUrl(String url) {
1414
: uri.path.substring(1);
1515
}
1616

17+
const _jsCompilerParam = '?compiler=js';
18+
const _wasmCompilerParam = '?compiler=wasm';
19+
1720
/// Maps DevTools URLs in the original fragment format onto the equivalent URLs
1821
/// in the new URL format.
1922
///
@@ -25,16 +28,26 @@ String? mapLegacyUrl(String url) {
2528
// http://localhost:123/#/?page=inspector&uri=ws://...
2629
final isRootRequest = uri.path == '/' || uri.path.endsWith('/devtools/');
2730
if (isRootRequest && uri.fragment.isNotEmpty) {
31+
final hasJsParam = url.contains(_jsCompilerParam);
32+
final hasWasmParam = url.contains(_wasmCompilerParam) && !hasJsParam;
2833
final basePath = uri.path;
2934
// Convert the URL by removing the fragment separator.
3035
final newUrl = url
36+
.replaceAll(_wasmCompilerParam, '')
37+
.replaceAll(_jsCompilerParam, '')
3138
// Handle localhost:123/#/inspector?uri=xxx
3239
.replaceFirst('/#/', '/')
3340
// Handle localhost:123/#?page=inspector&uri=xxx
3441
.replaceFirst('/#', '');
3542

3643
// Move page names from the querystring into the path.
3744
var newUri = Uri.parse(newUrl);
45+
final queryParams = {
46+
...newUri.queryParameters,
47+
if (hasWasmParam) 'compiler': 'wasm',
48+
if (hasJsParam) 'compiler': 'js',
49+
};
50+
newUri = newUri.replace(queryParameters: queryParams);
3851
final page = newUri.queryParameters['page'];
3952
if (newUri.path == basePath && page != null) {
4053
final newParams = {...newUri.queryParameters}..remove('page');

packages/devtools_app/test/shared/primitives/url_utils_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,44 @@ void main() {
6565
test('maps legacy URIs with no page names', () {
6666
expect(mapLegacyUrl('$prefix/#/?foo=bar'), '$prefix/?foo=bar');
6767
});
68+
69+
for (final compilerValue in ['js', 'wasm']) {
70+
group('with ?compiler=$compilerValue query param', () {
71+
test(
72+
'moves ?compiler=$compilerValue from before hash to after path',
73+
() {
74+
final newUrl = '$prefix/inspector?compiler=$compilerValue';
75+
expect(
76+
mapLegacyUrl('$prefix/?compiler=$compilerValue#/inspector'),
77+
newUrl,
78+
);
79+
expect(
80+
mapLegacyUrl(
81+
'$prefix/?compiler=$compilerValue#/?page=inspector',
82+
),
83+
newUrl,
84+
);
85+
},
86+
);
87+
88+
test('handles additional query parameters', () {
89+
final newUrl =
90+
'$prefix/inspector?foo=bar&compiler=$compilerValue';
91+
expect(
92+
mapLegacyUrl(
93+
'$prefix/?compiler=$compilerValue#/inspector?foo=bar',
94+
),
95+
newUrl,
96+
);
97+
expect(
98+
mapLegacyUrl(
99+
'$prefix/?compiler=$compilerValue#/?page=inspector&foo=bar',
100+
),
101+
newUrl,
102+
);
103+
});
104+
});
105+
}
68106
});
69107
}
70108
});

packages/devtools_app/web/flutter_bootstrap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ async function shouldUseSkwasm() {
8484

8585
// Sets or removes the 'wasm' query parameter based on whether DevTools should
8686
// be loaded with the skwasm renderer.
87+
//
88+
// Note: In the case of the legacy-formatted URL, this adds the query parameter
89+
// in the wrong place. We fix this in the Dart mapLegacyUrl function.
8790
function updateWasmQueryParameter(useSkwasm) {
8891
const url = new URL(window.location.href);
8992
if (useSkwasm) {

0 commit comments

Comments
 (0)