Support Angular Universal (with CI job to test all components)
There's no automated verification that all material components are safe to use in node, i.e. pre-rendering with Angular Universal. See #306.
Proposal
Create a step in the CI process to pre-compile a single root component which contains an instance of each material component. Use one of the Angular Universal pre-render libraries to build: https://github.com/angular/universal/tree/master/modules
Note: I'm planning to submit a Broccoli plugin to that repo which would work well with Angular CLI.
I'm willing to help set this up.
To report one problem unsolved in #774 and #883, Material is not working with Universal for now because of many reference to window in Material umd bundles.
As proposed by @orangesoup, adding var window = undefined; in Material umd bundles do the trick for now, and then Universal and Material work very well together in my test app.
I looked at Material TypeScript source files, and found very few direct reference to window (and it's not the same as in umd bundles). So I think it is just a problem with the umd packaging system (maybe configured to target browsers). It's beyond my skills, but I think it's just a bundling option to change, and Material would already work with Universal.
Well I've been too optimist, the var window = undefined; is causing problems on client side, so I suppose it's not just a bundling problem.
But I'm quite surprise to see so many references to window, as we're supposed to avoid native dom and browser things inside Angular 2.
I confirm that window references causing problems with Universal are just in umd bundles, which all starts by this kind of code :
var __extends = (window && window.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (window && window.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (window && window.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
I suppose this is polyfills, which are incorrectly checking on window, supposing a browser environment.
I don't know how the bundling is done so I can't help, but I think it's not a big deal to correct that. It's really blocking to not be able to use Universal with Material just because of this.
if you're using webpack you can use a string-replace-loader to fix the material2 package by replacing the string value of each file. I'll update the universal-starter to include it as an example
@gdi2290 Problem is server-side, so I don't use webpack, I directly launch the TypeScript code with ts-node.
if you use ts-node directly then you have to wait for an update or just include a fake window object for now
var window = global
global.window = window
I have the universal-starter updated to fix the material package during build https://github.com/angular/universal-starter
@gdi2290 Thanks, global patching is working as a workaround for now.
Now I have another issue I already met before, and I have no idea where it comes from :
Error: This method is not implemented in Parse5DomAdapter: Parse5DomAdapter#getCookie
at _notImplemented (/app/node_modules/angular2-platform-node/parse5-adapter.js:22:12)
at Parse5DomAdapter.getCookie (/app/node_modules/angular2-platform-node/parse5-adapter.js:625:15)
can you make an issue about the Parse5DomAdapter#getCookie in the universal repo so not to spam this one. We can fix that one now with the newest Universal release
@gdi2290 But I'm not sure this is Universal or Material related, because with no Material, my app works just fine with Universal.
Issue created in Universal repo.
For the global patch workaround, just a note for other people interested, you need to do that :
var window = global;
global['window'] = window;
Doing global.window directly causes an error.
@gdi2290 @cyrilletuzi How did you manage to get material work though? I'm getting an error like this:
.../node_modules/@angular2-material/core/ripple/ripple.js:10
import { NgModule, Directive, ElementRef, HostBinding, Input } from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
It seems like there's no umd for rippe (or anything under core).
@orangesoup -- I removed ripple from my application...
@JanStureNielsen Yeah well, that's an option as well. No other option yet I guess? Also... does that mean that I have to manually disable it for every single button/whatever that uses it?
@gdi2290
Any solution to lack of methods, when refering global in window variable, or we need for finalizing work on this issue?
EXCEPTION: window.requestAnimationFrame is not a function
ORIGINAL STACKTRACE:
TypeError: window.requestAnimationFrame is not a function
at /path/to/my/app/dist/server/index.js:10238:20
at ZoneDelegate.invoke (/path/to/my/app/node_modules/zone.js/dist/zone-node.js:203:28)
at Zone.run (/path/to/my/app/node_modules/zone.js/dist/zone-node.js:96:43)
at NgZoneImpl.runOuter (/path/to/my/app/node_modules/@angular/core/bundles/core.umd.js:6273:75)
at NgZone.runOutsideAngular (/path/to/my/app/node_modules/@angular/core/bundles/core.umd.js:6517:84)
at MdTabGroup.ngAfterViewChecked (/path/to/my/app/dist/server/index.js:10237:20)
at AppView._View_ProductsSettingsComponent0.detectChangesInternal (ProductsSettingsComponent.ngfactory.js:145:46)
at AppView.detectChanges (/path/to/my/app/node_modules/@angular/core/bundles/core.umd.js:9566:18)
at AppView.detectViewChildrenChanges (/path/to/my/app/node_modules/@angular/core/bundles/core.umd.js:9592:23)
at AppView.detectChangesInternal (/path/to/my/app/node_modules/@angular/core/bundles/core.umd.js:9577:18)
[TypeError: window.requestAnimationFrame is not a function]
@maciejsobala I had the same error as you. Try using https://github.com/chrisdickinson/raf
require('raf/polyfill'); in your server.ts
Material is broken again with Universal since alpha 9 (was working (with the window patch) with alpha 8).
Same (unhelpful) error as pre-alpha-8 :
.filter(function (task) { return task.data.eventName === eventName; })
TypeError: Cannot read property 'filter' of undefined
@gdi2290 mentions that RippleRenderer needs to use Renderer. This means https://github.com/angular/material2/blob/master/src/lib/core/ripple/ripple-renderer.ts#L50 and https://github.com/angular/material2/blob/master/src/lib/core/ripple/ripple-renderer.ts#L113 need to use renderer.createElement instead of document.createElement ??
See here: https://github.com/angular/universal-starter/issues/185#issuecomment-250864200
any eta on this patch?
Found issues with the following code in md-input:
this._elementType = elementRef.nativeElement.nodeName.toLowerCase() === 'md-input' ? 'input' : 'textarea';
@jelbourn should I create a pull request fixing this issue or you have someone already focusing on these issues? thanks
@krigton the important thing here is adding a job to the CI that runs over everything with Universal, otherwise it will keep breaking.
As for the input specifically, #1858 is changing the input quite a bit so that particular problem will likely soon be obsolete.
