Skip to content

Commit cec51cb

Browse files
committed
[dart2wasm] Legacy URL format is correctly mapped to new format when compiler query parameter is present (#9617)
1 parent d9cb750 commit cec51cb

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

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

Lines changed: 16 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,29 @@ 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+
// Note: If there is a ?compiler= query parameter, we remove it from before
32+
// the hash then add it back in as a query parameter at the end.
33+
// See https://github.com/flutter/devtools/issues/9612 for details.
34+
final hasJsParam = url.contains(_jsCompilerParam);
35+
final hasWasmParam = url.contains(_wasmCompilerParam) && !hasJsParam;
2836
final basePath = uri.path;
2937
// Convert the URL by removing the fragment separator.
3038
final newUrl = url
39+
.replaceAll(_jsCompilerParam, '')
40+
.replaceAll(_wasmCompilerParam, '')
3141
// Handle localhost:123/#/inspector?uri=xxx
3242
.replaceFirst('/#/', '/')
3343
// Handle localhost:123/#?page=inspector&uri=xxx
3444
.replaceFirst('/#', '');
3545

3646
// Move page names from the querystring into the path.
3747
var newUri = Uri.parse(newUrl);
48+
final queryParams = {
49+
...newUri.queryParameters,
50+
if (hasJsParam) 'compiler': 'js',
51+
if (hasWasmParam) 'compiler': 'wasm',
52+
};
53+
newUri = newUri.replace(queryParameters: queryParams);
3854
final page = newUri.queryParameters['page'];
3955
if (newUri.path == basePath && page != null) {
4056
final newParams = {...newUri.queryParameters}..remove('page');

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

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

packages/devtools_app/web/flutter_bootstrap.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ 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. Details:
90+
// https://github.com/flutter/devtools/issues/9612
8791
function updateWasmQueryParameter(useSkwasm) {
8892
const url = new URL(window.location.href);
8993
if (useSkwasm) {

0 commit comments

Comments
 (0)