twreporter-react-index-page
Version:
twreporter index page version 2
199 lines (165 loc) • 8.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _document = require('next/document');
var _document2 = _interopRequireDefault(_document);
var _DOMProperty = require('react-dom/lib/DOMProperty');
var _ReactInjection = require('react-dom/lib/ReactInjection');
var _styledComponents = require('styled-components');
var _appConfig = require('../conf/app-config.json');
var _appConfig2 = _interopRequireDefault(_appConfig);
var _lodash = require('lodash');
var _index = require('../styles/index.scss');
var _index2 = _interopRequireDefault(_index);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _ = {
get: _lodash.get
// By default React limit the set of valid DOM elements and attributes
// (https://github.com/facebook/react/issues/140) this config whitelist
// Amp elements/attributes
};if (typeof _DOMProperty.properties.amp === 'undefined') {
_ReactInjection.DOMProperty.injectDOMPropertyConfig({
Properties: { amp: _ReactInjection.DOMProperty.MUST_USE_ATTRIBUTE },
isCustomAttribute: function isCustomAttribute(attributeName) {
return attributeName.startsWith('amp-');
}
});
}
// normal HTML renderer
var CanonicalHTML = function CanonicalHTML(props) {
var commonHeaders = props.commonHeaders,
styleTags = props.styleTags,
main = props.main,
pathname = props.pathname;
return React.createElement(
'html',
{ lang: 'zh-TW' },
React.createElement(
_document.Head,
null,
commonHeaders,
React.createElement('link', { rel: 'amphtml', href: _appConfig2.default.url + '/amphtml' + pathname }),
React.createElement('style', { dangerouslySetInnerHTML: { __html: _index2.default } }),
React.createElement('script', { dangerouslySetInnerHTML: { __html: '(function(d) {var config = {kitId: \'lwr8ggq\',scriptTimeout: 3000,async: true},h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src=\'https://use.typekit.net/\'+config.kitId+\'.js\';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)})(document);' } }),
styleTags
),
React.createElement(
'body',
null,
React.createElement(
'div',
{ className: 'root' },
main
),
React.createElement(_document.NextScript, null)
)
);
};
// Google AMP HTML renderer
var AmpHTML = function AmpHTML(props) {
var commonHeaders = props.commonHeaders,
styleTags = props.styleTags,
main = props.main,
pathname = props.pathname;
return React.createElement(
'html',
{ lang: 'zh-TW', amp: '' },
React.createElement(
_document.Head,
null,
commonHeaders,
React.createElement('link', { rel: 'canonical', href: _appConfig2.default.url + pathname }),
React.createElement('link', { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Roboto' }),
React.createElement(
'style',
{ 'amp-boilerplate': '' },
'body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}'
),
React.createElement(
'noscript',
null,
React.createElement(
'style',
{ 'amp-boilerplate': '' },
'body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}'
)
),
styleTags,
React.createElement('script', { async: true, src: 'https://cdn.ampproject.org/v0.js' })
),
React.createElement(
'body',
null,
React.createElement(
'div',
{ className: 'root' },
main
),
React.createElement(_document.NextScript, null)
)
);
};
var MyDocument = function (_Document) {
_inherits(MyDocument, _Document);
function MyDocument() {
_classCallCheck(this, MyDocument);
return _possibleConstructorReturn(this, (MyDocument.__proto__ || Object.getPrototypeOf(MyDocument)).apply(this, arguments));
}
_createClass(MyDocument, [{
key: 'render',
value: function render() {
var sheet = new _styledComponents.ServerStyleSheet();
var main = sheet.collectStyles(React.createElement(_document.Main, null));
var styleTags = sheet.getStyleElement();
var _props = this.props,
isAmp = _props.isAmp,
pathname = _props.pathname;
var commonHeaders = [React.createElement('meta', { charSet: 'utf-8' }), React.createElement('meta', { name: 'viewport', content: 'width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no' }), React.createElement('link', { href: 'https://www.twreporter.org/asset/favicon.png', rel: 'shortcut icon' }), React.createElement(
'title',
null,
'Reporter'
)];
var props = { main: main, styleTags: styleTags, commonHeaders: commonHeaders, pathname: pathname };
if (isAmp) {
return React.createElement(AmpHTML, props);
}
return React.createElement(CanonicalHTML, props);
}
}], [{
key: 'getInitialProps',
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(_ref2) {
var query = _ref2.query,
pathname = _ref2.pathname;
var isAmp;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
isAmp = _.get(query, 'isAmp', false);
return _context.abrupt('return', {
isAmp: isAmp,
pathname: pathname
});
case 2:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function getInitialProps(_x) {
return _ref.apply(this, arguments);
}
return getInitialProps;
}()
}]);
return MyDocument;
}(_document2.default);
exports.default = MyDocument;