Problem/Motivation

I tried adding a local cached libary to libraries.xml file and referenced it properly. Per data_types.library.xml this is something that works for sure with CSS, but with JS it seems failing.

google_analytics.cached:
  remote: https://developers.google.com/analytics/devguides/collection/analyticsjs/
  version: VERSION
  license:
    name: © Google
    gpl-compatible: false
  js:
    'public://google_analytics/analytics.js': { async: true, minified: true, preprocess: false }
The website encountered an unexpected error. Please try again later.

Exception: Only local files should be passed to _locale_parse_js_file(). in _locale_parse_js_file() (line 1141 of core\modules\locale\locale.module).

locale_js_translate(Array) (Line: 503)
locale_js_alter(Array, Object, NULL) (Line: 501)
Drupal\Core\Extension\ModuleHandler->alter('js', Array, Object) (Line: 276)
Drupal\Core\Asset\AssetResolver->getJsAssets(Object, ) (Line: 298)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAssetLibraries(Object, Array) (Line: 161)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->processAttachments(Object) (Line: 45)
Drupal\Core\EventSubscriber\HtmlResponseSubscriber->onRespond(Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 179)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 161)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Fix the bug.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Issue fork drupal-2735717

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hass created an issue. See original summary.

hass’s picture

Issue summary: View changes
hass’s picture

Priority: Normal » Major

Since there is no way to workaround this bug by manually adding a library with a render array.

Example I tried, but is not working.

$page['#attached']['library'][] = [
  file_url_transform_relative(file_create_url('public://google_analytics/analytics.js')) => [
    'type' => 'external',
    'minified' => TRUE,
    'preprocess' => FALSE,
    'attributes' => [
      'async' => TRUE,
    ]
  ],
];
hass’s picture

Title: Only local files should be passed to _locale_parse_js_file() » Stream wrapper reference in JS library causes error in _locale_parse_js_file()
wim leers’s picture

Interesting! I don't think it was ever designed to support that. Did that work in D7?

hass’s picture

I wonder as I have seen so many people requested CDN support in past. Here I think it is only local, but still an major issue as I have no chance to reference the file in any way.

In D7 I used a different way and was not forced to use libraries API. With drupal_add_js we haven't had this issues in D7.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

vuejs contrib affected as well

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mxh’s picture

I need this for dynamically generated libraries, whose files should be able for being aggregated by core. Once #1308152: Add stream wrappers to access .json files in extensions is in, would this mean we'd then still need to write a fix for the mentioned bug here?

hass’s picture

_locale_parse_js_file() Still need to be fixed.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

sandboxpl’s picture

for JS external defined in libraries.yml file, declaring library to be "external" does the job , like this

fluffines:
  js:
    'https://example.com/fluffines.min.js': { type: external }
hass’s picture

External files are not the point here.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new1.19 KB

Dug the sources a bit, and it looks like without the locale module, uris with stream wrappers are no problem.
But _locale_parse_js_file() coughs on any stream wrapper:

  // If there is still a protocol component in the path, reject that.
  if (strpos($filepath, ':')) {
    throw new Exception('Only local files should be passed to _locale_parse_js_file().');
  }

Rejecting all uris is clearly wrong and i wonder what to do. Beneath public://, there will be module://, and there are legitimate use cases for s3://, data:, phar:// and whatnot.

Given that, we may either cough on http(s), or remove the check altogether. As we only receive js files marked as 'file', we may do the latter. Otherwise, no harm in being conservative.

Patch flying in that coughs only on http(s) and includes the offending filename in the exception.

lpeabody’s picture

Status: Needs review » Reviewed & tested by the community

I was building a custom animations module (users with trusted permission levels can upload custom css/js on a custom entity type) and the patch from #19 was required in order to build dynamic libraries using those uploaded files. Once applied, everything started working as expected.

RTBC +1 from me.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Is it possible to add test coverage for this?

eiriksm’s picture

Assigned: Unassigned » eiriksm

Adding some tests

eiriksm’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.46 KB
new2.27 KB

Here is a test only patch (with some X files related strings) and an updated patch.

No interdiff, since that is literally the test :)

eiriksm’s picture

Assigned: eiriksm » Unassigned

The last submitted patch, 24: 2735717-test-only.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 24: 2735717.patch, failed testing. View results

eiriksm’s picture

Status: Needs work » Needs review
StatusFileSize
new2.28 KB
new1.46 KB

Huh, did not know about the void typehint in ::setup

Attached the same test only patch here so its easier to review without jumping back and forth.

Status: Needs review » Needs work

The last submitted patch, 28: 2735717-test-only.patch, failed testing. View results

eiriksm’s picture

Status: Needs work » Needs review

Right, that was the test only patch

mxh’s picture

Thanks for the test, I'd suggest to add some more cases to explicitly test for "not allowed" protocols like http(s), ftp(s), and //.

IMO when public:// files are actually resided locally, then _locale_parse_js should try to properly resolve them. Then, the current (unpatched) exception handling also would make more sense to me.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Is #2385069: Locale JS alter breaks on remote JS files a duplicate of this?

The patch over there is using UrlHelper::isExternal - is that a better approach than using preg_match?

The issue over there has no tests, so if that one is indeed a duplicate, we should close it in favour of this one

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.61 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

bladedu’s picture

Rerolled patch #15 to work with version 2.16.0

bladedu’s picture

wrong issue.

informatique clouturier christophe’s picture

#28 work for me, drupal 9.5.9 and asset injector 8.x-2.17.
THX

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

heddn’s picture

Hiding #39 as it veers off course. #28 is the patch to keep pushing along. And this is still NW for adding more test coverage for HTTP(s) urls in the test coverage.

smustgrave credited Berdir.

smustgrave credited nlisgo.

smustgrave credited TuWebO.

smustgrave’s picture

akalam made their first commit to this issue’s fork.

akalam’s picture

andypost’s picture

needs more work to fix tests

tuwebo’s picture

Hello, I've updated the fork with latest code, updated the test adding annotation and fixing some phpcs and, since this issue is considered duplicated of #2385069: Locale JS alter breaks on remote JS files I am adding this piece of code !UrlHelper::isExternal($item['data']), for external js files to not be passed through the locale_js_translate function:

  #[Hook('js_alter')]
  public function jsAlter(&$javascript, AttachedAssetsInterface $assets, LanguageInterface $language): void {
    $files = [];
    foreach ($javascript as $item) {
      if (isset($item['type']) && $item['type'] == 'file' && !UrlHelper::isExternal($item['data'])) {

The only test failing seems not to be related with this issue, but i am not sure about it:

Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testInlineBlocksRevisioning
TypeError: str_starts_with(): Argument #1 ($haystack) must be of type string, array given

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.