Skip to content

Commit

Permalink
Core:Ajax: Align nonce & global with master, fix an AMD issue
Browse files Browse the repository at this point in the history
This commit aligns the `3.x-stable` branch with `master` in two aspects:
1. It migrates the nonce module to return an object instead of a primitive
variable. This had to be changed on `master` as in ES modules you export
live read-only bindings to variables, meaning you can't increment the nonce
directly. Also, the way it was done so far was working differently in AMD & the
single built file - in the built file one nonce variable was declared, accessed
and incremented. In AMD mode separate instances were create for each module
that depend on the nonce module, creating unintended nonce clashes.
2. Whether the `noGlobal` parameter was set to `true` is now checked using the
typeof operator to align with `master`.

Ref jquerygh-4541
Ref d0ce00c
  • Loading branch information
mgol committed Feb 24, 2020
1 parent 3dedc3f commit f2a09c7
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ jQuery.extend( {
// Add or update anti-cache param if needed
if ( s.cache === false ) {
cacheURL = cacheURL.replace( rantiCache, "$1" );
uncached = ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + ( nonce++ ) + uncached;
uncached = ( rquery.test( cacheURL ) ? "&" : "?" ) + "_=" + ( nonce.guid++ ) +
uncached;
}

// Put hash and anti-cache on the URL that will be requested (gh-1732)
Expand Down
2 changes: 1 addition & 1 deletion src/ajax/jsonp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var oldCallbacks = [],
jQuery.ajaxSetup( {
jsonp: "callback",
jsonpCallback: function() {
var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) );
var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce.guid++ ) );
this[ callback ] = true;
return callback;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ajax/var/nonce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define( function() {
"use strict";

return Date.now();
return { guid: Date.now() };
} );
4 changes: 2 additions & 2 deletions src/exports/global.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define( [
"../core"
], function( jQuery, noGlobal ) {
], function( jQuery ) {

"use strict";

Expand All @@ -27,7 +27,7 @@ jQuery.noConflict = function( deep ) {
// Expose jQuery and $ identifiers, even in AMD
// (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if ( !noGlobal ) {
if ( typeof noGlobal === "undefined" ) {
window.jQuery = window.$ = jQuery;
}

Expand Down

0 comments on commit f2a09c7

Please sign in to comment.