-
Notifications
You must be signed in to change notification settings - Fork 16
/
turbo-react.js
66 lines (53 loc) · 2.17 KB
/
turbo-react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"use strict";
if (window.Turbolinks === undefined) {
throw "Missing Turbolinks dependency. TurboReact requires Turbolinks be included before it.";
}
var Babel = require("@babel/standalone");
var HTMLtoJSX = require("htmltojsx");
var React = require("react");
var ReactDOM = require("react-dom");
// Disable the Turbolinks page cache to prevent Tlinks from storing versions of
// pages with `react-id` attributes in them. When popping off the history, the
// `react-id` attributes cause React to treat the old page like a pre-rendered
// page and breaks diffing.
Turbolinks.controller.cache.size = 0;
// `documentElement.replaceChild` must be called in the context of the
// `documentElement`. Keep a bound reference to use later.
var originalReplaceChild =
global.document.documentElement.replaceChild.bind(
global.document.documentElement);
var converter = new HTMLtoJSX({createClass: false});
var TurboReact = {
version: TURBO_REACT_VERSION,
applyDiff: function(replacementElement, targetElement) {
try {
var bod = TurboReact.reactize(replacementElement);
ReactDOM.render(bod, targetElement);
} catch(e) {
// If any problem occurs when updating content, let Turbolinks replace
// the page normally. That means no transitions, but it also means no
// broken pages.
originalReplaceChild(replacementElement, targetElement);
}
},
reactize: function(element) {
var code = Babel.transform(
converter.convert(element.innerHTML),
{presets: ['es2015', 'react']}
).code;
return eval(code);
}
};
Turbolinks.SnapshotRenderer.prototype.assignNewBody = function() {
TurboReact.applyDiff(this.newBody, document.body);
return this.newBody;
};
// Turbolinks calls `replaceChild` on the document element when an update should
// occur. Monkeypatch the method so Turbolinks can be used without modification.
global.document.documentElement.replaceChild = TurboReact.applyDiff;
function applyBodyDiff() {
TurboReact.applyDiff(document.body, document.body);
global.document.removeEventListener("DOMContentLoaded", applyBodyDiff);
}
global.document.addEventListener("DOMContentLoaded", applyBodyDiff);
module.exports = TurboReact;