JavaScriptModules.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\Http\Client\IClientService;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. use OCP\SetupCheck\CheckServerResponseTrait;
  13. use OCP\SetupCheck\ISetupCheck;
  14. use OCP\SetupCheck\SetupResult;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * Checks if the webserver serves '.mjs' files using the correct MIME type
  18. */
  19. class JavaScriptModules implements ISetupCheck {
  20. use CheckServerResponseTrait;
  21. public function __construct(
  22. protected IL10N $l10n,
  23. protected IConfig $config,
  24. protected IURLGenerator $urlGenerator,
  25. protected IClientService $clientService,
  26. protected LoggerInterface $logger,
  27. ) {
  28. }
  29. public function getCategory(): string {
  30. return 'network';
  31. }
  32. public function getName(): string {
  33. return $this->l10n->t('JavaScript modules support');
  34. }
  35. public function run(): SetupResult {
  36. $testFile = $this->urlGenerator->linkTo('settings', 'js/esm-test.mjs');
  37. $noResponse = true;
  38. foreach ($this->runRequest('HEAD', $testFile) as $response) {
  39. $noResponse = false;
  40. if (preg_match('/(text|application)\/javascript/i', $response->getHeader('Content-Type'))) {
  41. return SetupResult::success();
  42. }
  43. }
  44. if ($noResponse) {
  45. 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());
  46. }
  47. 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.'));
  48. }
  49. }