2

I and several others have been getting the following error when testing custom lightning web components in a scratch org:

"The first parameter of loadScript() and loadStyle() must be an LWC component."

While debugging, it is because platformResourceLoader.js (which contains loadScript()) first null-checks cmp.template.host.ownerDocument where in my case, ownerDocument is undefined. This is a documented Salesforce issue and they provide the following workaround:

Current work around is to create another wrapper lwc custom component and use the custom component with platformResourceLoader in the wrapper.

e.g --- c-lwc-wrapper --

<template>

<c-lwc-loader></c-lwc-loader> 

</template>

where c-lwc-loader uses

import { loadStyle, loadScript } from 'lightning/platformResourceLoader';`

Can someone explain to me in more detail what this means? Am I supposed to create an entirely new LWC and nest my original one inside it? The wording of the sentence and my relative unfamiliarity with HTML/JS are causing quite the headache.

2 Answers 2

3

There is currently an issue in LWC where ownerDocument has been removed from host and we're investigating.

Meanwhile, the workaround is to create the script tag manually:

var script = document.createElement('script');
script.src = ''; // code URL
document.head.appendChild(script);

script.onload = function() {
   // init code
}

This will give you similar results.

2
  • Does 'document' in this case refer to the html template? <script> tags are not allowed in salesforce html docs and adding a script dynamically from JS is not working either. this.template.createElement('script') is not a valid function call because SF does not allow templates to include <script>. Or am I misunderstanding your post?
    – Ari Klau
    Commented Jun 25, 2019 at 19:55
  • I think it should work: Locker does support dynamic scripts, as long as it's in JS and not HTML.
    – JF Paradis
    Commented Jun 26, 2019 at 0:22
2

It is a known issue. https://success.salesforce.com/issues_view?id=a1p3A000001SHoDQAW

In the link you have the workaround but if I had been you, i would have waiting for Salesforce Devs team's updates

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .