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
Comments
Comment #2
hass commentedComment #3
hass commentedSince there is no way to workaround this bug by manually adding a library with a render array.
Example I tried, but is not working.
Comment #4
hass commentedComment #5
wim leersInteresting! I don't think it was ever designed to support that. Did that work in D7?
Comment #6
hass commentedI 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.
Comment #9
andypostvuejs contrib affected as well
Comment #12
mxh commentedI 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?
Comment #13
hass commented_locale_parse_js_file() Still need to be fixed.
Comment #15
sandboxplfor JS external defined in libraries.yml file, declaring library to be "external" does the job , like this
Comment #16
hass commentedExternal files are not the point here.
Comment #19
geek-merlinDug 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:
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.
Comment #20
lpeabody commentedI 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.
Comment #22
xjmIs it possible to add test coverage for this?
Comment #23
eiriksmAdding some tests
Comment #24
eiriksmHere is a test only patch (with some X files related strings) and an updated patch.
No interdiff, since that is literally the test :)
Comment #25
eiriksmComment #28
eiriksmHuh, 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.
Comment #30
eiriksmRight, that was the test only patch
Comment #31
mxh commentedThanks 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.
Comment #37
larowlanIs #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
Comment #38
needs-review-queue-bot commentedThe 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.
Comment #39
bladeduRerolled patch #15 to work with version 2.16.0
Comment #40
bladeduwrong issue.
Comment #41
informatique clouturier christophe commented#28 work for me, drupal 9.5.9 and asset injector 8.x-2.17.
THX
Comment #43
heddnHiding #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.
Comment #50
smustgrave commented#2385069: Locale JS alter breaks on remote JS files was closed a duplicate, moving over credit
Comment #53
akalam commentedI've created a MR based on patch #2735717-28: Stream wrapper reference in JS library causes error in _locale_parse_js_file()
Comment #54
andypostneeds more work to fix tests
Comment #55
tuwebo commentedHello, 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:The only test failing seems not to be related with this issue, but i am not sure about it: