[Craft 5] Error "..registerScript(): Argument #1 ($script) must be of type string" in production
Description
When pushing code to PRODUCTION this error happens whenever I try to display a Map:
craft\web\View::registerScript(): Argument #1 ($script) must be of type string, null given, called in /home/craft/www.domain.com/vendor/craftcms/cms/src/services/TemplateCaches.php on line 323
The code works as it should in DEV.
If I just switch my environment to "production" in my dev area the same error appears.
Additional info
- Craft version: Craft CMS 5.5.4
- Maps version: 5.0.4
- PHP version: 8.2
So the issue is with Crafts cache tag. If you wrap code within the cache tag (and not the entire full page) the scripts injected by the tag will not be registered (since Craft is outputting cached content and not injecting the embeded map field).
Is there a workaround for this? Like injecting the script code inline in the HTML or similar?
Thanks for bringing this up, @naboo. We're going to discuss it internally and come back to you.
For now I've wrapped all my map fields in iframes with a custom route so a blank un-cached page that just displayes the map. If anyone runs in the same issue. Like this:
<iframe src="{{ url('template/goes-here' ~ entry.id) }}" style="width: 100%; height: 600px;" scrolling="no"></iframe>
<html>
<head>{{ head() }}</head>
<body>
{{ beginBody() }}
{% set entry = craft.entries.section('section').id(craft.app.request.segments|last).one() %}
{{ entry.mapField.embed({id: 'map', markers: [{label: entry.title}]})|attr({
style: 'width: 100%; height: 600px;',
}) }}
{{ endBody() }}
</body>
</html>