public static UIComponent addScriptResourceToTarget(FacesContext context, String type, String libraryName, String resourceName, String target) {
        UIOutput outputResource = createResource(type);

        if (libraryName != null) {
            outputResource.getAttributes().put("library", libraryName);
        }

        outputResource.getAttributes().put("name", resourceName);
        context.getViewRoot().addComponentResource(context, outputResource, target);
        return outputResource;
    }
Adds a resource component to the specified target, avoiding duplicates based on ID.

@param context The FacesContext
@param type The renderer type
@param libraryName The library name
@param resourceName The resource name
@param target The target location ("head" or "body")
@return The added or existing UIComponent
primefaces/src/main/java/org/primefaces/util/ResourceUtils.java