Skip to content

Commit 5118e16

Browse files
committed
feat(config): serve speedscope notification JS via RL
Also remove the setTimeout hack.
1 parent 513f7da commit 5118e16

1 file changed

Lines changed: 45 additions & 37 deletions

File tree

config/MWCProfiling.php

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use MediaWiki\Context\RequestContext;
1111
use MediaWiki\Deferred\DeferredUpdates;
1212
use MediaWiki\Exception\MWExceptionHandler;
13-
use MediaWiki\Html\Html;
1413
use MediaWiki\Output\OutputPage;
14+
use MediaWiki\ResourceLoader\Context;
15+
use MediaWiki\ResourceLoader\Module;
1516
use Throwable;
1617

1718
trait MWCProfiling {
@@ -71,6 +72,44 @@ public function logToSpeedscopeService(
7172
?string $requiredParameter = 'forceflame',
7273
?string $publicEndpoint = null
7374
): self {
75+
// Always register the module, since it's loaded in a separate request
76+
global $wgResourceModules;
77+
$wgResourceModules['mwc.speedscopeNotification'] = [
78+
'class' => new class extends Module {
79+
/** @inheritDoc */
80+
public function getScript( Context $context ): string {
81+
return <<<JS
82+
$( () => {
83+
const profileId = mw.config.get( 'speedscopeProfileId' );
84+
const endpoint = mw.config.get( 'speedscopeEndpoint' );
85+
const speedscopeUrl = new URL( 'https://speedscope.app' );
86+
speedscopeUrl.hash = `profileURL=\${endpoint}/profile/\${profileId}`;
87+
const speedscopeLink = $( '<a>' )
88+
.attr( 'href', speedscopeUrl.toString() )
89+
.attr( 'target', '_blank' )
90+
.text( 'Speedscope' );
91+
const jsonLink = $( '<a>' )
92+
.attr( 'href', `\${endpoint}/profile/\${profileId}` )
93+
.attr( 'target', '_blank' )
94+
.text( 'JSON' );
95+
const metadataLink = $( '<a>' )
96+
.attr( 'href', `\${endpoint}/metadata/\${profileId}` )
97+
.attr( 'target', '_blank' )
98+
.text( 'Metadata' );
99+
mw.notify(
100+
$( '<div>' ).append( speedscopeLink, ' (', jsonLink, ', ', metadataLink, ')' ),
101+
{
102+
autoHide: false,
103+
title: 'Profile recorded successfully',
104+
type: 'success'
105+
}
106+
);
107+
} );
108+
JS;
109+
}
110+
},
111+
];
112+
74113
// phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
75114
$forced = $requiredParameter !== null && isset( $_GET[$requiredParameter] );
76115
if ( extension_loaded( 'excimer' ) && (
@@ -156,42 +195,11 @@ private function doLogToSpeedscopeService(
156195
};
157196
$wgHooks['BeforePageDisplay'][] = static function ( $out, $skin ) use ( $id, $publicEndpoint ) {
158197
/** @var OutputPage $out */
159-
$out->addJsConfigVars( [ 'speedscopeProfileId' => $id ] );
160-
$endpointJs = Html::encodeJsVar( $publicEndpoint );
161-
$profileIdJs = Html::encodeJsVar( $id );
162-
$out->addHTML( Html::rawElement(
163-
'script',
164-
[],
165-
<<<JS
166-
(function() {
167-
'use strict';
168-
169-
setTimeout(() => {
170-
const profileId = $profileIdJs;
171-
const endpoint = $endpointJs;
172-
const speedscopeUrl = new URL( 'https://speedscope.app' );
173-
speedscopeUrl.hash = `profileURL=\${endpoint}/profile/\${profileId}`;
174-
const speedscopeLink = $( '<a>' )
175-
.attr( 'href', speedscopeUrl.toString() )
176-
.attr( 'target', '_blank' )
177-
.text( 'Speedscope' );
178-
const jsonLink = $( '<a>' )
179-
.attr( 'href', `\${endpoint}/profile/\${profileId}` )
180-
.attr( 'target', '_blank' )
181-
.text( 'JSON' );
182-
mw.notify(
183-
$( '<div>' ).append( speedscopeLink, ' (', jsonLink, ')' ),
184-
{
185-
autoHide: false,
186-
title: 'Profile recorded successfully',
187-
type: 'success'
188-
}
189-
);
190-
}, 1000);
191-
192-
})();
193-
JS
194-
) );
198+
$out->addJsConfigVars( [
199+
'speedscopeEndpoint' => $publicEndpoint,
200+
'speedscopeProfileId' => $id,
201+
] );
202+
$out->addModules( 'mwc.speedscopeNotification' );
195203
};
196204
}
197205
$wgHooks['ParserBeforeInternalParse'][] = static function (

0 commit comments

Comments
 (0)