Skip to content

Commit 9970438

Browse files
committed
Merge pull request #496 from defunkt/strip-params
Strip jQuery's cache-busting parameter
2 parents e34a213 + abb8079 commit 9970438

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

jquery.pjax.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ function pjax(options) {
225225
settings.timeout = 0
226226
}
227227

228-
options.requestUrl = parseURL(settings.url).href
228+
options.requestUrl = stripInternalParams(parseURL(settings.url).href)
229229
}
230230

231231
options.complete = function(xhr, textStatus) {
@@ -366,7 +366,7 @@ function pjax(options) {
366366
// Cache current container element before replacing it
367367
cachePush(pjax.state.id, context.clone().contents())
368368

369-
window.history.pushState(null, "", stripPjaxParam(options.requestUrl))
369+
window.history.pushState(null, "", options.requestUrl)
370370
}
371371

372372
fire('pjax:start', [xhr, options])
@@ -544,16 +544,22 @@ function uniqueId() {
544544
return (new Date).getTime()
545545
}
546546

547-
// Internal: Strips _pjax param from url
547+
// Internal: Strips named query param from url
548548
//
549549
// url - String
550550
//
551551
// Returns String.
552-
function stripPjaxParam(url) {
552+
function stripParam(url, name) {
553+
return url
554+
.replace(new RegExp('[?&]' + name + '=[^&]*'), '')
555+
.replace(/[?&]$/, '')
556+
.replace(/[?&]/, '?')
557+
}
558+
559+
function stripInternalParams(url) {
560+
url = stripParam(url, '_pjax')
561+
url = stripParam(url, '_')
553562
return url
554-
.replace(/\?_pjax=[^&]+&?/, '?')
555-
.replace(/_pjax=[^&]+&?/, '')
556-
.replace(/[\?&]$/, '')
557563
}
558564

559565
// Internal: Parse URL components and returns a Locationish object.
@@ -669,7 +675,8 @@ function extractContainer(data, xhr, options) {
669675

670676
// Prefer X-PJAX-URL header if it was set, otherwise fallback to
671677
// using the original requested url.
672-
obj.url = stripPjaxParam(xhr.getResponseHeader('X-PJAX-URL') || options.requestUrl)
678+
var serverUrl = xhr.getResponseHeader('X-PJAX-URL')
679+
obj.url = serverUrl ? stripInternalParams(serverUrl) : options.requestUrl
673680

674681
// Attempt to parse response html into elements
675682
if (fullDocument) {

test/unit/pjax.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ if ($.support.pjax) {
2222

2323
frame.$('#main').on('pjax:success', function() {
2424
equal(frame.location.pathname, "/hello.html")
25+
equal(frame.location.search, "")
2526
start()
2627
})
2728
frame.$.pjax({
2829
url: "hello.html",
29-
container: "#main"
30+
container: "#main",
31+
cache: false
3032
})
3133
})
3234

0 commit comments

Comments
 (0)