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

Turbolinks 5 Support #264

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ gem "selenium-webdriver"
gem "spring"
gem "sqlite3"
gem "therubyracer"
gem "turbolinks"
gem "turbolinks", "~> 5.0.0.beta"
gem "uglifier", ">= 2.7.2"
gem "web-console", "~> 2.0"
2 changes: 2 additions & 0 deletions node_package/src/clientStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
import createReactElement from './createReactElement';
import handleError from './handleError';
import isRouterResult from './isRouterResult';
import turbolinksShim from './turbolinksShim';

const REACT_ON_RAILS_COMPONENT_CLASS_NAME = 'js-react-on-rails-component';
const REACT_ON_RAILS_STORE_CLASS_NAME = 'js-react-on-rails-store';
Expand Down Expand Up @@ -135,6 +136,7 @@ export default function clientStartup(context) {
' installed.');
document.addEventListener('page:before-unload', reactOnRailsPageUnloaded);
document.addEventListener('page:change', reactOnRailsPageLoaded);
turbolinksShim();
}
});
}
89 changes: 89 additions & 0 deletions node_package/src/turbolinksShim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
function handleEvent(eventName, handler) {
return document.addEventListener(eventName, handler, false);
}

function translateEvent(arg) {
const from = arg.from;
const to = arg.to;
function handler(e) {
const event = arg.dispatch(to, {
target: e.target,
cancelable: e.cancelable,
data: e.data,
});
if (event.defaultPrevented) {
return event.preventDefault();
}
}

return handleEvent(from, handler);
}

export default function turbolinksShim() {
const defer = Turbolinks.defer;
const dispatch = Turbolinks.dispatch;
let loaded = false;

translateEvent({
from: 'turbolinks:click',
to: 'page:before-change',
dispatch,
});

translateEvent({
from: 'turbolinks:request-start',
to: 'page:fetch',
dispatch,
});

translateEvent({
from: 'turbolinks:request-end',
to: 'page:receive',
dispatch,
});

translateEvent({
from: 'turbolinks:before-cache',
to: 'page:before-unload',
dispatch,
});

translateEvent({
from: 'turbolinks:render',
to: 'page:update',
dispatch,
});

translateEvent({
from: 'turbolinks:load',
to: 'page:change',
dispatch,
});

translateEvent({
from: 'turbolinks:load',
to: 'page:update',
dispatch,
});

handleEvent('DOMContentLoaded', () => {
defer(() => {
loaded = true;
return;
});
});

handleEvent('turbolinks:load', () => {
if (loaded) {
return dispatch('page:load');
}
});

if (typeof jQuery === 'function') {
jQuery(document).on('ajaxSuccess', (event, xhr) => {
if (jQuery.trim(xhr.responseText).length > 0) {
return dispatch('page:update');
}
});
}
}
2 changes: 1 addition & 1 deletion spec/dummy/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks' if ENV["DISABLE_TURBOLINKS"].nil? || ENV["DISABLE_TURBOLINKS"].strip.empty?
gem 'turbolinks', '~> 5.0.0.beta' if ENV["DISABLE_TURBOLINKS"].nil? || ENV["DISABLE_TURBOLINKS"].strip.empty?

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
Expand Down
7 changes: 4 additions & 3 deletions spec/dummy/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ GEM
thread_safe (0.3.5)
tilt (2.0.2)
tins (1.6.0)
turbolinks (2.5.3)
coffee-rails
turbolinks (5.0.0.beta1)
turbolinks-source
turbolinks-source (5.0.0.beta1.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.7.2)
Expand Down Expand Up @@ -330,7 +331,7 @@ DEPENDENCIES
spring
sqlite3
therubyracer
turbolinks
turbolinks (~> 5.0.0.beta)
uglifier (>= 2.7.2)
web-console

Expand Down