2023-12-22 00:00:04 +01:00
< ? php
declare ( strict_types = 1 );
/**
2024-06-03 10:23:34 +02:00
* SPDX - FileCopyrightText : 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2023-12-22 00:00:04 +01:00
*/
namespace OCA\Settings\SetupChecks ;
use OCP\Http\Client\IClientService ;
use OCP\IConfig ;
use OCP\IL10N ;
use OCP\IURLGenerator ;
use OCP\SetupCheck\ISetupCheck ;
use OCP\SetupCheck\SetupResult ;
use Psr\Log\LoggerInterface ;
/**
* Checks if the webserver serves '.mjs' files using the correct MIME type
*/
class JavaScriptModules implements ISetupCheck {
2024-02-15 02:07:01 +01:00
use CheckServerResponseTrait ;
2023-12-22 00:00:04 +01:00
public function __construct (
2024-02-15 02:07:01 +01:00
protected IL10N $l10n ,
protected IConfig $config ,
protected IURLGenerator $urlGenerator ,
protected IClientService $clientService ,
protected LoggerInterface $logger ,
2023-12-22 00:00:04 +01:00
) {
}
public function getCategory () : string {
return 'network' ;
}
public function getName () : string {
2024-01-10 11:58:40 +01:00
return $this -> l10n -> t ( 'JavaScript modules support' );
2023-12-22 00:00:04 +01:00
}
public function run () : SetupResult {
$testFile = $this -> urlGenerator -> linkTo ( 'settings' , 'js/esm-test.mjs' );
2024-02-15 02:07:01 +01:00
$noResponse = true ;
foreach ( $this -> runHEAD ( $testFile ) as $response ) {
$noResponse = false ;
if ( preg_match ( '/(text|application)\/javascript/i' , $response -> getHeader ( 'Content-Type' ))) {
return SetupResult :: success ();
2023-12-22 00:00:04 +01:00
}
}
2024-02-15 02:07:01 +01:00
if ( $noResponse ) {
2024-05-10 17:54:41 +02:00
return SetupResult :: warning ( $this -> l10n -> t ( 'Unable to run check for JavaScript support. Please remedy or confirm manually if your webserver serves `.mjs` files using the JavaScript MIME type.' ) . " \n " . $this -> serverConfigHelp ());
2024-02-15 02:07:01 +01:00
}
2023-12-22 00:00:04 +01:00
return SetupResult :: error ( $this -> l10n -> t ( 'Your webserver does not serve `.mjs` files using the JavaScript MIME type. This will break some apps by preventing browsers from executing the JavaScript files. You should configure your webserver to serve `.mjs` files with either the `text/javascript` or `application/javascript` MIME type.' ));
2024-02-15 02:07:01 +01:00
2023-12-22 00:00:04 +01:00
}
}