#1737·requirejs

Check if an AMD module exists and load fallback otherwise

Author: jslegersCreated May 18, 2018Updated Sep 22, 2024

What I'm trying to achieve is this :

  1. I want to check if module namespace/A is available on the server
  2. If it is available, I want to load namespace/A
  3. If it is not available, I want to load namespace/B as a fallback

Now...

  • For non-bundled modules, I could use an Ajax call to check for the availability of path/to/A.
  • For bundled modules, I could use require.specified() to check if namespace/A has been specified already.
  • I guess I could first use require.specified() and then use an Ajax call if I want to support both non-bundled and bundled modules... but I can't really say I like this approach.

Is there a simpler way to conditionally load namespace/A if namespace/A is available and fall back to namespace/B otherwise, using require.js?