Dashboards & Visualizations
Highlighted

RequireJS: load a custom script from the /appserver/static

Path Finder

Hi splunkers,

One of my SimpleXML dashboards loads a JS script specified within <dashboard> tag. I want this script to load and use another shared .js file. I put them both into /appserver/static and tried to use RequireJS (the first calls "define(...)" function, and the second calls "require(['./first'], ...)" function). But all I get is a "Script error" in the JS console.

Is this possible at all? What should I do?

P.S. I know I can put the shared JS file name into the "script" attribute of the toplevel "dashboard" tag alongside with the main script, but is there any guarantee they'll be loaded sequentially and in the right order?

Highlighted

Re: RequireJS: load a custom script from the /appserver/static

Splunk Employee
Splunk Employee

require.js can be a real bear to debug. The first thing to keep in mind is that there is a core require profile that's in $SPLUNKHOME/share/splunk/searchmrsparkle/exposed/js/profiles/shared.js. Relative pathing might be difficult because of this profile.

Can you share the specific error/errors? Something about a mismatched anonymous define()?

0 Karma
Highlighted

Re: RequireJS: load a custom script from the /appserver/static

Path Finder

Hi araitz,
The exact error text I see in a browser console is:

Error: Script error
http://requirejs.org/docs/errors.html#scripterror

And this is reported on "config.js:6" -- definitely not my script.
The manual page that is referenced by this URL suggests that there may be a syntax error somewhere. I'm not sure if this is the case, though, as I just copy-pasted the 100% working piece of code and wrapped it in a define() call.
So, the error message itself is not of great help.

Highlighted

Re: RequireJS: load a custom script from the /appserver/static

Path Finder

That's what I did: I used the Firefox built-in inspector to see what actual scripts are being loaded.


GET http://myhost:8000/en-US/static/@182037/js/first.js
GET http://myhost:8000/en-US/static/@182037:8/app/myapp/second.js

The first one, of course, fails with 404. So I modified the second.js a bit: changed the "./first" to "../app/myapp/first".

Now it works, but feels like a fragile hack a bit. I'm not sure if this will work in future versions of Splunk at all. Any suggestions and comments are welcome.

Highlighted

Re: RequireJS: load a custom script from the /appserver/static

Splunk Employee
Splunk Employee

I'm glad you got it to work. I couldn't agree more: require is hard to configure and debug, and it's hard to feel too confident about those paths staying consistent in the future. I'll ask the core team what we can do about it.

Highlighted

Re: RequireJS: load a custom script from the /appserver/static

Builder

I can but agree. Having the app name in the URL is ugly and calling for future errors that are difficult to diagnose. There should be better support for building custom modules using the mechanism chose by Splunk (RequireJS).

Highlighted

Re: RequireJS: load a custom script from the /appserver/static

If You Just want to load two JS files to a simple-XML...
Give two script file name separated by comma...

script="yourUtil.js,yourPrimary.js"

yourUtil.js will be loader first and yuorPrimary.js will be loaded...
i.e.. in the same order as you define 🙂

0 Karma