diff --git a/src/js/theme/navigation.js b/src/js/theme/navigation.js index db0219fd6403ff569e9dad776e343878def989cf..78716c9db77a0ea1cad8903142730a4bf9aba929 100644 --- a/src/js/theme/navigation.js +++ b/src/js/theme/navigation.js @@ -66,72 +66,91 @@ function handleNavigation(relativeUrl, push) { prevUri = uri; - return loading.show($.get(uri) - .then(function (html) { - // Replace html content - html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){ - return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>'; + var promise = $.Deferred(function(deferred) { + $.ajax({ + type: 'GET', + url: uri, + cache: true, + headers:{ + 'Access-Control-Expose-Headers': 'X-Current-Location' + }, + success: function(html, status, xhr) { + // For GitBook.com, we handle redirection signaled by the server + var responseURL = xhr.getResponseHeader('X-Current-Location') || uri; + + // Replace html content + html = html.replace( /<(\/?)(html|head|body)([^>]*)>/ig, function(a,b,c,d){ + return '<' + b + 'div' + ( b ? '' : ' data-element="' + c + '"' ) + d + '>'; + }); + + var $page = $(html); + var $pageHead = $page.find('[data-element=head]'); + var $pageBody = $page.find('.book'); + + // We only use history.pushState for pages generated with GitBook + if ($pageBody.length === 0) { + var err = new Error('Invalid gitbook page, redirecting...'); + return deferred.reject(err); + } + + // Push url to history + if (push) { + history.pushState({ + path: responseURL + }, null, responseURL); + } + + // Merge heads + // !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...) + + // Update title + document.title = $pageHead.find('title').text(); + + // Reference to $('head'); + var $head = $('head'); + + // Update next & prev tags + // Remove old + $head.find('link[rel=prev]').remove(); + $head.find('link[rel=next]').remove(); + + // Add new next * prev tags + $head.append($pageHead.find('link[rel=prev]')); + $head.append($pageHead.find('link[rel=next]')); + + // Merge body + var bodyClass = $('.book').attr('class'); + var scrollPosition = $('.book-summary').scrollTop(); + + $pageBody.toggleClass('with-summary', $('.book').hasClass('with-summary')); + + $('.book').replaceWith($pageBody); + $('.book').attr('class', bodyClass); + $('.book-summary').scrollTop(scrollPosition); + + // Scroll to hashtag position + if (hash) { + scrollToHash(hash); + } + + // Update state + gitbook.state.$book = $('.book'); + preparePage(!hash); + + deferred.resolve(); + } }); + }).promise(); - var $page = $(html); - var $pageHead = $page.find('[data-element=head]'); - var $pageBody = $page.find('.book'); - // We only use history.pushState for pages generated with GitBook - if ($pageBody.length === 0) { - return $.Deferred(function (deferred) { - var err = new Error('Invalid gitbook page, redirecting...'); - deferred.reject(err); - }).promise(); - } - - // Push url to history - if (push) { - history.pushState({ - path: uri - }, null, uri); - } - - // Merge heads - // !! Warning !!: we only update necessary portions to avoid strange behavior (page flickering etc ...) - - // Update title - document.title = $pageHead.find('title').text(); - - // Reference to $('head'); - var $head = $('head'); - - // Update next & prev tags - // Remove old - $head.find('link[rel=prev]').remove(); - $head.find('link[rel=next]').remove(); - // Add new next * prev tags - $head.append($pageHead.find('link[rel=prev]')); - $head.append($pageHead.find('link[rel=next]')); - - // Merge body - var bodyClass = $('.book').attr('class'); - var scrollPosition = $('.book-summary').scrollTop(); - - $pageBody.toggleClass('with-summary', $('.book').hasClass('with-summary')); - - $('.book').replaceWith($pageBody); - $('.book').attr('class', bodyClass); - $('.book-summary').scrollTop(scrollPosition); - - // Scroll to hashtag position - if (hash) { - scrollToHash(hash); - } - - // Update state - gitbook.state.$book = $('.book'); - preparePage(!hash); - }) - .fail(function (e) { - location.href = relativeUrl; - })); + return loading.show( + promise + .fail(function (e) { + console.log(e); + //location.href = relativeUrl; + }) + ); } function updateNavigationPosition() {