我有一个Angular 6应用程序,它在浏览器中运行良好,但在Googlebot试图刮掉它时崩溃。
我的理论:这是一个Javascript兼容性问题,而TypeScript正在输出与Googlebot不兼容的JS。
问题:我的Angular应用程序需要什么垫片才能在Googlebot试图刮掉它时不会崩溃?我还能做些什么来最大化兼容性吗?
因为Googlebot不提供console.log
输出,所以我开始尝试使用PhantomJS屏幕显示我的网站,其中(谣言有)是谷歌使用的。
当我这样做时,我得到以下控制台错误(我不确定,但我认为这也是Googlebot正在运行的):
https://www.example.com/ng2/vendor.js:46405 in ./node_modules/@angular/core/fesm5/core.js
TypeError: undefined is not a constructor (evaluating 'new Array(HEADER_OFFSET).fill(null)')
https://www.example.com/ng2/vendor.js:46407 in ./node_modules/@angular/core/fesm5/core.js
https://www.example.com/ng2/runtime.js:82 in __webpack_require__
https://www.example.com/ng2/main.js:1815 in ./src/main.ts
https://www.example.com/ng2/runtime.js:82 in __webpack_require__
https://www.example.com/ng2/main.js:1863
https://www.example.com/ng2/runtime.js:82 in __webpack_require__
https://www.example.com/ng2/runtime.js:44 in checkDeferredModules
https://www.example.com/ng2/runtime.js:31 in webpackJsonpCallback
似乎PhantomJS在vendor.js中遇到了这行代码并且正在挂起:
var HEADER_FILLER = new Array(HEADER_OFFSET).fill(null);
我可以添加任何类型的polyfill,这样就不会发生这种情况吗?
要解决此问题并让Googlebot正确呈现Angular应用,我必须打开应用的polyfills.ts文件并进行以下更改:
/* enable these */
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';
/* ... */
/* and these: */
/**
* Date, currency, decimal and percent pipes.
* Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10
*/
import 'intl'; // Run `npm install --save intl`.
/**
* Need to import at least one locale-data with intl.
*/
import 'intl/locale-data/jsonp/en';