2

If I try and create a ltng:require component for a script dynamically, I get a 503 server error:

component.cmp

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <div>This component checks static resource loading on Init</div>
                         {!v.body}
</aura:component>

componentController.js

({
doInit : function(cmp, event, helper) {
        $A.createComponent(
        "ltng:require",
        {
            "scripts": "/community/resource/loadMe.js"

        },
        function(newButton, status, errorMessage){
            //Add the new button to the body array
            if (status === "SUCCESS") {
                var body = cmp.get("v.body");
                body.push(newButton);
                cmp.set("v.body", body);
            }
            else if (status === "INCOMPLETE") {
                console.log("No response from server or client is offline.")
                // Show offline error
            }
            else if (status === "ERROR") {
                console.log("Error: " + errorMessage);
                // Show error message
            }
        }
    );
    }
}
})

Error Message

GET https://customdomain-developer-edition.na53.force.com/community/resource/loadMe 503 (Service Unavailable)

However, If i add the component in the markup, it seems to load fine.

Working component.cmp

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" >
    <ltng:require scripts="{!$Resource.loadMe}"/>

    <div>This component checks static resource loading on Init</div>

    {!v.body}
</aura:component>

I am trying to check certain conditions when my page loads, and depending on x,y factors, I am trying to load a different script, should I attempt to achieve this using aura:if 's to conditionally load different static resources or should my current approach work?

Based on my comprehension of what a 503 status code is, my current approach should work, however, for some reason it wont process my request to load the static resource if I try to create it dynamically.

3

Consider using the standard translation service for this:

    $A.createComponent(
    "ltng:require",
    {
        "scripts": $A.get("$Resource.loadMe")
    },

Also, note that your resource string was technically incorrect; the bare /resource/filename syntax does not normally include the file extension.

  • indeed, i would have expected a 404 if the string/filepath would have been incorrect. Thanks for the heads up. – glls Jul 16 '18 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.