simplemap icon indicating copy to clipboard operation
simplemap copied to clipboard

[Craft 5] Error "..registerScript(): Argument #1 ($script) must be of type string" in production

Open naboo opened this issue 1 year ago • 3 comments

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

naboo avatar Dec 03 '24 16:12 naboo

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?

naboo avatar Dec 04 '24 07:12 naboo

Thanks for bringing this up, @naboo. We're going to discuss it internally and come back to you.

alexjcollins avatar Dec 04 '24 07:12 alexjcollins

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>

naboo avatar Dec 04 '24 08:12 naboo