Skip to content

Commit 94daeed

Browse files
committed
Reflect the hash of the URI in state.url
* Simplifies hash handling code; * Avoids the extra replaceState; * Ensures full URL (with hash) is in `state.url`.
1 parent d9a10c9 commit 94daeed

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

jquery.pjax.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ function pjax(options) {
261261

262262
var container = extractContainer(data, xhr, options)
263263

264+
var url = parseURL(container.url)
265+
if (hash) {
266+
url.hash = hash
267+
container.url = url.href
268+
}
269+
264270
// If there is a layout version mismatch, hard load the new url
265271
if (currentVersion && latestVersion && currentVersion !== latestVersion) {
266272
locationReplace(container.url)
@@ -317,18 +323,7 @@ function pjax(options) {
317323

318324
// If the URL has a hash in it, make sure the browser
319325
// knows to navigate to the hash.
320-
if ( hash !== '' ) {
321-
// Avoid using simple hash set here. Will add another history
322-
// entry. Replace the url with replaceState and scroll to target
323-
// by hand.
324-
//
325-
// window.location.hash = hash
326-
var url = parseURL(container.url)
327-
url.hash = hash
328-
329-
pjax.state.url = url.href
330-
window.history.replaceState(pjax.state, container.title, url.href)
331-
326+
if (hash) {
332327
var name = decodeURIComponent(hash.slice(1))
333328
var target = document.getElementById(name) || document.getElementsByName(name)[0]
334329
if (target) $(window).scrollTop($(target).offset().top)

test/app.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def title(str)
2121

2222
after do
2323
if pjax?
24-
response.headers['X-PJAX-URL'] = request.url
24+
response.headers['X-PJAX-URL'] ||= request.url
2525
response.headers['X-PJAX-Version'] = 'v1'
2626
end
2727
end
@@ -48,7 +48,17 @@ def title(str)
4848
end
4949

5050
get '/redirect.html' do
51-
redirect "/hello.html"
51+
if params[:anchor]
52+
path = "/hello.html##{params[:anchor]}"
53+
if pjax?
54+
response.headers['X-PJAX-URL'] = uri(path)
55+
status 200
56+
else
57+
redirect path
58+
end
59+
else
60+
redirect "/hello.html"
61+
end
5262
end
5363

5464
get '/timeout.html' do

test/unit/pjax.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,24 +1192,10 @@ if ($.support.pjax) {
11921192
})
11931193
})
11941194

1195-
asyncTest("follows redirect with X-PJAX-URL header", function() {
1196-
var frame = this.frame
1197-
1198-
frame.$('#main').on("pjax:success", function() {
1199-
equal(frame.location.pathname, "/hello.html")
1200-
equal(frame.$("#main > p").html().trim(), "Hello!")
1201-
start()
1202-
})
1203-
frame.$.pjax({
1204-
url: "redirect.html",
1205-
container: "#main"
1206-
})
1207-
})
1208-
12091195
asyncTest("lazily sets initial $.pjax.state", function() {
12101196
var frame = this.frame
12111197

1212-
ok(!frame.$.pjax.state)
1198+
equal(frame.$.pjax.state, null)
12131199

12141200
frame.$('#main').on("pjax:success", function() {
12151201
start()
@@ -1219,24 +1205,28 @@ if ($.support.pjax) {
12191205
container: "#main"
12201206
})
12211207

1222-
ok(frame.$.pjax.state.id)
1223-
ok(frame.$.pjax.state.url.match("/home.html"))
1224-
equal(frame.$.pjax.state.container, "#main")
1208+
var initialState = frame.$.pjax.state
1209+
ok(initialState.id)
1210+
equal(initialState.url, "http://" + frame.location.host + "/home.html")
1211+
equal(initialState.container, "#main")
12251212
})
12261213

12271214
asyncTest("updates $.pjax.state to new page", function() {
12281215
var frame = this.frame
12291216

12301217
frame.$('#main').on("pjax:success", function() {
1231-
ok(frame.$.pjax.state.id)
1232-
ok(frame.$.pjax.state.url.match("/hello.html"))
1233-
equal(frame.$.pjax.state.container, "#main")
1218+
var state = frame.$.pjax.state
1219+
ok(state.id)
1220+
equal(state.url, "http://" + frame.location.host + "/hello.html#new")
1221+
equal(state.container, "#main")
12341222
start()
12351223
})
12361224
frame.$.pjax({
1237-
url: "hello.html",
1225+
url: "hello.html#new",
12381226
container: "#main"
12391227
})
1228+
1229+
var initialState = frame.$.pjax.state
12401230
})
12411231

12421232
asyncTest("new id is generated for new pages", function() {

test/unit/pjax_fallback.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ $.each([true, false], function() {
55
var disabled = this == false
66
var s = disabled ? " (disabled)" : ""
77

8+
var ua = navigator.userAgent
9+
var safari = ua.match("Safari") && !ua.match("Chrome") && !ua.match("Edge")
10+
var chrome = ua.match("Chrome") && !ua.match("Edge")
11+
812
module("$.pjax fallback"+s, {
913
setup: function() {
1014
var self = this
@@ -422,4 +426,36 @@ asyncTest("handle form submit"+s, function() {
422426
frame.$("form").submit()
423427
})
424428

429+
asyncTest("browser URL is correct after redirect"+s, function() {
430+
var frame = this.frame
431+
432+
this.loaded = function() {
433+
equal(frame.location.pathname, "/hello.html")
434+
var expectedHash = safari && disabled ? "" : "#new"
435+
equal(frame.location.hash, expectedHash)
436+
start()
437+
}
438+
439+
frame.$.pjax({
440+
url: "redirect.html#new",
441+
container: "#main"
442+
})
443+
})
444+
445+
asyncTest("server can't affect anchor after redirect"+s, function() {
446+
var frame = this.frame
447+
448+
this.loaded = function() {
449+
equal(frame.location.pathname, "/hello.html")
450+
var expectedHash = safari && disabled ? "" : "#new"
451+
equal(frame.location.hash, expectedHash)
452+
start()
453+
}
454+
455+
frame.$.pjax({
456+
url: "redirect.html?anchor=server#new",
457+
container: "#main"
458+
})
459+
})
460+
425461
})

0 commit comments

Comments
 (0)