Encountering a mismatched anonymous define() error when using RequireJS in conjunction with Algolia Docsearch

Incorporating Algolia Docsearch with MadCap Flare has been my current challenge. MadCap Flare, a technical documentation tool that generates HTML output, is proving to be tricky when it comes to integrating the Docsearch feature. While the underlying crawler of Docsearch functions correctly in codepen, I am encountering difficulties merging it with Flare. Unfortunately, Flare comes equipped with a range of built-in scripts that are inaccessible for modification, including require.js. This restriction is resulting in a string of errors, such as Uncaught Error: Mismatched anonymous define() module.., during the build process.

To tackle this issue, I attempted to load the Docsearch javascript using require.js, but my limited understanding of JavaScript led me down the wrong path.

The search input in HTML looks like this:

<div class="input-group">
    <input id="<YOUR_CSS_SELECTOR>"  type="text" class="form-control" placeholder="Search" aria-label="Search Field" aria-describedby="infoSearch" />
</div>

The original Algolia Docsearch CDN script and snippet typically appear as follows:

<!-- Before the closing </body> -->
    <script src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js">
    </script>
    <script>
    docsearch({
        apiKey: '<API_KEY>',
        indexName: '<INDEX_NAME>',
        inputSelector: '<YOUR_CSS_SELECTOR>',
        debug: false
    });
    </script>

My attempt at loading these scripts using require.js involves the following steps:

<script type="text/javascript">
require.config({
    enforceDefine: true,
    paths: {
        "docSearch": "https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"
    },
    waitSeconds: 40
});
require(['docSearch']);//This is probably not doing anything.
</script>
<script>
docsearch({
    apiKey: '<API_KEY>',
    indexName: '<INDEX_NAME>',
    inputSelector: '<YOUR_CSS_SELECTOR>',
    debug: false
}); //This throws "Uncaught Reference Error docsearch is not defined" error
</script>

Despite consulting various threads and require.js documentation, I have yet to find a solution. Any assistance or guidance would be greatly appreciated!

Answer №1

Incorporating something like the following might be beneficial:

    <script>
/* <![CDATA[ */

require.config( {
    paths: {
        "docsearchLib": "https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min"
    }
}

);
require(["docsearchLib"], function(docsearch) {
    docsearch( {
        apiKey: '<API_KEY>', indexName: '<INDEX_NAME>', inputSelector: '<YOUR_CSS_SELECTOR>', debug: false,
    }
    );
}

);
/* ]]> */

</script>

You may find inspiration from , as they utilize DocSearch and require.js together

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS - Issue: [ng:areq] The 'fn' argument provided is not a function, instead it is a string

I encountered an issue: Error: [ng:areq] Argument 'fn' is not a function, received string Despite following the recommendations of others, I still have not been able to resolve the problem. Below is the code snippet in question: controller. ...

Beginner in html, css, and javascript: "Tips for saving jsfiddle demos?"

Recently, I developed an interest in JavaScript and CSS and came across some impressive examples on jsfiddle. I was wondering if there is a way to "export" these examples to my computer. Simply copying and pasting doesn't seem to work. How can I modi ...

Mastering the art of using either promises or callbacks properly

Attempting to retrieve the result of a variable after completing all asynchronous processes in a function. I've discovered that I need to use promises for this, so I spent today learning about them. I've implemented promises in my function and c ...

No matter how many times I modified the code in the ReactDOM.render() method within my index.js file, the end result remained unchanged

When I ran npx create-react-app my-app, and then proceeded to cd my-app and npm start, a browser opened on localhost:3000. While looking at the index.js file, I noticed that the ReactDOM.render() method included the following code: ReactDOM.render( <Rea ...

Await the resolution of multiple individual promises

I am working with Angular promises in multiple controllers, and I need to trigger a function once all of them have completed. Is there a way to achieve this using events or promises? ...

Modifying specific attributes of an object within the $scope: A step-by-step guide

When working with Angular, if you have code in the view that looks like this: <span ng-model="foo.bar1"></span> <span ng-model="foo.bar2"></span> <span ng-model="foo.bar3"></span> Due to how Angular maps objects, you c ...

The response from the ajax call is still pending

I am currently working on integrating MySQL data (select query) using Spring and MyBatis. I have a controller function that is called via ajax in JavaScript to retrieve the database data. For example: ajax url: /testmysql controller requestmapp ...

Making a RESTful API call using JavaScript

Can someone help me with making a call to a REST API using JavaScript, Ajax, or jQuery? curl -v -X PUT -H "Content-Type: application/json" -H "Accept: application/json" -X PUT --user user:password http://url -d "{\"name\": \"Marcus0.2\ ...

Disable Three.js when WebGL is not supported: Tips and tricks

I read about how to switch to canvasrenderer if webgl is not supported. renderer = Detector.webgl? new THREE.WebGLRenderer(): new THREE.CanvasRenderer(); I'm not sure how to completely remove everything if webgl is not detected. I have an image in t ...

Two separate projects targeting Admin and User interfaces versus a unified application featuring distinct themes for each user role

In React, there is a scenario that needs to be implemented. An admin panel with various functionalities like Auth, charts, analytics, and user management has already been pre-built. The goal now is to connect this admin panel to another website as the back ...

Issues with utilizing Jquery datepicker within a Vue.js component's v-for loop in Laravel

I am facing an issue with the Jquery date picker inside a v-for loop in my vue.js component. The date picker works fine outside of the loop, but inside the loop it does not behave as expected. Here is a snippet of code where the date picker is working cor ...

Acquire the top-scoring item using React

I retrieved data from the backend Below is the code snippet where I display the data: <div className="pr-0 mt-20" style={{ display: 'flex', justifyContent: 'center' }}> <MuiCardWithAnimation component={AnimateS ...

I am interested in displaying all the items on the list instead of just one

<html> <head> <link rel="stylesheet" type="text/css" href="mango.css"> <script> function showItems(){ var items = document.querySelectorAll("#main li"); items.forEach(i ...

Top method to gather all necessary inputs

I am dealing with a form that contains several required fields. You can find an example here <div class="form-group"> <label class="col-md-3 control-label" for="mpassword">Password<span class="required">* </span>< ...

Navigating the missing "length" property when dealing with partial functions generated using lodash's partialRight

I've been utilizing MomentTimezone for time manipulation within the browser. My development stack includes TypeScript and Lodash. In my application, there is an accountTimezone variable set on the window object which stores the user's preferred ...

"Utilizing the mongodb deleteOne function to remove an email within a document

In my collection, I have the following document model: { "user":{ "name":string, "email":string }, "address":{ "direction":string, "country":string }, "id":string } If you want to delete the document by id, there&apo ...

What is preventing me from directly assigning properties to my data object?

Struggling to access my question props in order to assign its property directly into my data properties. Although I can use the property directly from my template, I am unable to assign it into the data. Currently, I can only retrieve the value of props f ...

`Trigger a page reload when redirecting`

Currently, I am tackling some bug fixes on an older Zend Framework 1.10 project and encountering difficulties with redirection and page refresh. The issue: The task at hand is to make an AJAX call, verify if a person has insurance assigned, and prevent de ...

the angular variable scope has not been defined

Currently, I am developing an angular controller that is configured to utilize the "controller as" syntax: angular.module('app', []).controller('ctrl1', ctrl1); ctrl1.$inject = ['$http', '$compile']; function ctrl ...

What is the process for having HTML open a hyperlink in a new window?

I am facing the following issue: <p class="bigfont"> <img width="4%" src="images/Youtube.png" class="float-left"/ > <a href="\\OC2-RMGFS\Public\Safety\runhidefight-eng.wmv" target="_blank" title="Response to ...