From abb80792d98c911e223a8507b880551077686054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 3 Mar 2015 11:53:11 +1300 Subject: [PATCH] Strip jQuery's cache-busting parameter If jQuery `cache: false` ajax parameter is set, the `&_=` param will have been appended to the request URL. Because this URL will eventually be used to replace the page URL, we need to ensure that we're stripping the cache-busting parameter before that, like we do with the "_pjax" internal parameter as well. --- jquery.pjax.js | 23 +++++++++++++++-------- test/unit/pjax.js | 4 +++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/jquery.pjax.js b/jquery.pjax.js index 59d18705..ae05636a 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -225,7 +225,7 @@ function pjax(options) { settings.timeout = 0 } - options.requestUrl = parseURL(settings.url).href + options.requestUrl = stripInternalParams(parseURL(settings.url).href) } options.complete = function(xhr, textStatus) { @@ -366,7 +366,7 @@ function pjax(options) { // Cache current container element before replacing it cachePush(pjax.state.id, context.clone().contents()) - window.history.pushState(null, "", stripPjaxParam(options.requestUrl)) + window.history.pushState(null, "", options.requestUrl) } fire('pjax:start', [xhr, options]) @@ -544,16 +544,22 @@ function uniqueId() { return (new Date).getTime() } -// Internal: Strips _pjax param from url +// Internal: Strips named query param from url // // url - String // // Returns String. -function stripPjaxParam(url) { +function stripParam(url, name) { + return url + .replace(new RegExp('[?&]' + name + '=[^&]*'), '') + .replace(/[?&]$/, '') + .replace(/[?&]/, '?') +} + +function stripInternalParams(url) { + url = stripParam(url, '_pjax') + url = stripParam(url, '_') return url - .replace(/\?_pjax=[^&]+&?/, '?') - .replace(/_pjax=[^&]+&?/, '') - .replace(/[\?&]$/, '') } // Internal: Parse URL components and returns a Locationish object. @@ -669,7 +675,8 @@ function extractContainer(data, xhr, options) { // Prefer X-PJAX-URL header if it was set, otherwise fallback to // using the original requested url. - obj.url = stripPjaxParam(xhr.getResponseHeader('X-PJAX-URL') || options.requestUrl) + var serverUrl = xhr.getResponseHeader('X-PJAX-URL') + obj.url = serverUrl ? stripInternalParams(serverUrl) : options.requestUrl // Attempt to parse response html into elements if (fullDocument) { diff --git a/test/unit/pjax.js b/test/unit/pjax.js index a1992918..f137a821 100644 --- a/test/unit/pjax.js +++ b/test/unit/pjax.js @@ -22,11 +22,13 @@ if ($.support.pjax) { frame.$('#main').on('pjax:success', function() { equal(frame.location.pathname, "/hello.html") + equal(frame.location.search, "") start() }) frame.$.pjax({ url: "hello.html", - container: "#main" + container: "#main", + cache: false }) })