JavaScriptModules.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Checks if the webserver serves '.mjs' files using the correct MIME type
  17. */
  18. class JavaScriptModules implements ISetupCheck {
  19. use CheckServerResponseTrait;
  20. public function __construct(
  21. protected IL10N $l10n,
  22. protected IConfig $config,
  23. protected IURLGenerator $urlGenerator,
  24. protected IClientService $clientService,
  25. protected LoggerInterface $logger,
  26. ) {
  27. }
  28. public function getCategory(): string {
  29. return 'network';
  30. }
  31. public function getName(): string {
  32. return $this->l10n->t('JavaScript modules support');
  33. }
  34. public function run(): SetupResult {
  35. $testFile = $this->urlGenerator->linkTo('settings', 'js/esm-test.mjs');
  36. $noResponse = true;
  37. foreach ($this->runHEAD($testFile) as $response) {
  38. $noResponse = false;
  39. if (preg_match('/(text|application)\/javascript/i', $response->getHeader('Content-Type'))) {
  40. return SetupResult::success();
  41. }
  42. }
  43. if ($noResponse) {
  44. 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());
  45. }
  46. 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.'));
  47. }
  48. }