Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require.js bug random Failed to load resource

My application use require.js, I have a random bug (happens 1 time for 50 reload) Require.js write in the console :

Failed to load resource: the server responded with a status of 404 (Not Found)

Indeed, require.js try to include jquery from a wrong directory... I don't know why, most of the time the application works fine...

My config is pretty simple :

require.config({
  shim: {
    underscore: {
      exports: '_'
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    animate_from_to: {
      deps: ['jquery']
    },
    bootstrap: {
      deps: ['jquery']
    },
    zoom: {
      deps: ['jquery']
    },
    shop_util: {
      deps: ['jquery']
    },
    pricer: {
      deps: ['jquery']
    },
    filter: {
      deps: ['jquery']
    },
    paginator: {
      deps: ['jquery']
    },
  },
  paths: {
    bootstrap:        'lib/bootstrap',
    jquery:           'lib/jquery-1.9.1',
    zoom:             'lib/jquery.zoom.min',
    animate_from_to:  'lib/jquery.animate_from_to-1.0.min',
    backbone:         'lib/backbone.min',
    underscore:       'lib/underscore.min',
    text:             'lib/require-text',
    shop_util:  'lib/shop_util',
    pricer:           'lib/pricer',
    filter:           'lib/filter',
    paginator:        'lib/paginator',
  }

});

Thank you

like image 446
Thomas K Avatar asked Jun 10 '13 14:06

Thomas K


People also ask

What is require () in JavaScript?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

How do you fix mismatched anonymous definition module?

To fix the problem, you must update your JavaScript define methods within RequireJS, according to following documentation: https://requirejs.org/docs/api.html#define.

How add RequireJS to HTML?

To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.

What does Failed to load resource the server responded with a status of 404 Not Found mean?

The HTTP 404 Not Found response status code indicates that the server cannot find the requested resource. Links that lead to a 404 page are often called broken or dead links and can be subject to link rot.


1 Answers

It seems you have another entry point into your application somewhere other than your data-main script (js/main.js). Even if it's a subsequent script in the same page, you cannot depend on your data-main script being completed before the next script runs, since it's loaded with async attribute.

<script data-main="js/main" src="js/lib/require.js"></script>
<!-- foo.js might run before js/main.js !!! -->
<script src="js/foo.js"></script>

You can prove this by adding a console.log statement at the end of js/main.js and one in foo.js (or whatever). Normally you will see the one from js/main.js and then foo.js , but in that 1 out of 50 case you'll see them happen in the other order.

There are several strategies to deal with this:

1 - Do all your app initiation and subsequent require's from your data-main script

Applies to single-page apps, of course. All in one file:

require.config({
  // snip
});
require(['mymodule'], function( mymodule ) {
  // do stuff
});

2 - Use an inline script right after the require.js script tag

Instead of having the above script inside a separate file referenced by data-main, just have a 2nd script tag right below. This is the first example listed in the docs.

Applies mostly to single-page-apps

3 - Load your require config into global variable prior to the require.js script tag

Second example listed in the docs.

<script>
    var require = {
        paths: { // define them}
        shim: { // define them }
    };
</script>
<script src="scripts/require.js"></script>

Applies mostly to single-page-apps

4 - Nest your require calls to load the the config first

This works best for multi-page apps and is the one recommended in the multi-page shim app example

<script src="js/lib/require.js"></script>
<script>
    //Load common code that includes config, then load the app
    //logic for this page. Do the require calls here instead of
    //a separate file so after a build there are only 2 HTTP
    //requests instead of three.
    require(['./js/common'], function (common) {
        //js/common sets the baseUrl to be js/ so
        //can just ask for 'app/main1' here instead
        //of 'js/app/main1'
        require(['app/main1']);
    });
</script>

Related questions here, here, and here

like image 84
explunit Avatar answered Oct 21 '22 13:10

explunit