Skip to content

Commit

Permalink
Merge pull request #133 from Shopify/fix-partial-replace-head-tracking
Browse files Browse the repository at this point in the history
Fix partial replace head tracking
  • Loading branch information
Mathew Allen authored Aug 29, 2016
2 parents 34352cf + 96c69ce commit 5ac5d58
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/assets/javascripts/turbograft/turbolinks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,34 @@ class window.Turbolinks
@loadPage: (url, xhr, options = {}) ->
triggerEvent 'page:receive'
options.updatePushState ?= true
if upstreamDocument = processResponse(xhr, options.partialReplace)
if upstreamDocument = processResponse(xhr)
reflectNewUrl url if options.updatePushState

new TurboHead(document, upstreamDocument).update(
onHeadUpdateSuccess = ->
nodes = changePage(
upstreamDocument.querySelector('title')?.textContent,
removeNoscriptTags(upstreamDocument.querySelector('body')),
CSRFToken.get(upstreamDocument).token,
'runScripts',
options
)
reflectRedirectedUrl(xhr) if options.updatePushState
options.onLoadFunction?()
triggerEvent 'page:load', nodes
,
onHeadUpdateError = ->
Turbolinks.fullPageNavigate(url.absolute)
)
if options.partialReplace
updateBody(upstreamDocument, xhr, options)
else
new TurboHead(document, upstreamDocument).update(
onHeadUpdateSuccess = ->
updateBody(upstreamDocument, xhr, options)
,
onHeadUpdateError = ->
Turbolinks.fullPageNavigate(url.absolute)
)
else
triggerEvent 'page:error', xhr
Turbolinks.fullPageNavigate(url.absolute) if url?

updateBody = (upstreamDocument, xhr, options) ->
nodes = changePage(
upstreamDocument.querySelector('title')?.textContent,
removeNoscriptTags(upstreamDocument.querySelector('body')),
CSRFToken.get(upstreamDocument).token,
'runScripts',
options
)
reflectRedirectedUrl(xhr) if options.updatePushState
options.onLoadFunction?()
triggerEvent 'page:load', nodes

changePage = (title, body, csrfToken, runScripts, options = {}) ->
document.title = title if title
options.onlyKeys ?= []
Expand Down Expand Up @@ -323,7 +328,7 @@ class window.Turbolinks
pageChangePrevented = (url) ->
!triggerEvent('page:before-change', url)

processResponse = (xhr, partial = false) ->
processResponse = (xhr) ->
clientOrServerError = ->
return false if xhr.status == 422 # we want to render form validations
400 <= xhr.status < 600
Expand Down
12 changes: 12 additions & 0 deletions test/javascripts/turbolinks_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,18 @@ describe 'Turbolinks', ->
describe 'with partial page replacement', ->
beforeEach -> window.globalStub = stub()

it 'head assets are not inserted during partial replace', (done) ->
visit url: 'singleScriptInHead', options: {partialReplace: true, onlyKeys: ['turbo-area']}, ->
assertScripts([])
done()

it 'head assets are not removed during partial replace', (done) ->
visit url: 'singleLinkInHead', ->
assertLinks(['foo.css'])
visit url: 'twoLinksInHead', options: {partialReplace: true, onlyKeys: ['turbo-area']}, ->
assertLinks(['foo.css'])
done()

it 'script tags are evaluated when they are the subject of a partial replace', (done) ->
visit url: 'inlineScriptInBody', options: {partialReplace: true, onlyKeys: ['turbo-area']}, ->
assert(globalStub.calledOnce, 'Script tag was not evaluated :(')
Expand Down

0 comments on commit 5ac5d58

Please sign in to comment.