Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix partial replace head tracking #133

Merged
merged 4 commits into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we can 🔥 the partial parameter from processReponse now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yass

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