accessing fastboot service's "request" can throw error
This might be better as an issue at ember-fastboot instead. Happy to move it over there if necessary.
In environments where the Fastboot app renderer/visiter visits a path and doesn't provide a request option, accessing this.get('<fastbootService>.request') will throw the error "cannot read property 'cookies' of undefined". E.g., if the app is visited via code that looks like this:
const FastBoot = require('fastboot');
let app = new FastBoot({distPath: 'path/to/dist'});
app.visit('/some-url'); // no options object with "request" property passed
And the ember app has code like the following, it will result in the error:
export default Ember.Component.extend({
fastboot: injectService(),
init() {
this._super.apply(...arguments);
if (this.get('fastboot.isFastboot')) {
let request = this.get('fastboot.request'); // -> error: "cannot read property 'cookies' of undefined
}
}
});
This is caused by the fact that there is no {request} passed as a second argument to app.visit:
- Fastboot reads off the undefined
requestand uses it to construct aFastBootInfo - The FastbootInfo constructor ignores the missing
requestand never setsthis.requestproperty - The FastbootInfo instance is injected into the fastboot service
- When the ember app accesses
fastbootService.request, the fastboot services builds aRequestObject, passing it the "request" property that the FastbootInfo was instantiated with (which is undefined) - The
RequestObjectreads thecookiesproperty on the undefined request, which leads to the error
This problem is nonexistent when using the fastboot-express-middleware because it always passes the express request to app.visit.
In certain use cases there may not be an express request to pass in to app.visit (for example, when using a lambda function to render the app).
Should the RequestObject's constructor be changed to throw a meaningful error if it is accessed without a native/raw request that it can refer to? Or perhaps the Fastboot visit method should warn when it is called without a request object?
fyi I just hit this exact issue while playing around/learning fastboot. I wanted to simply render a page in my app directly within node w/o setting up express and discovered this error. If I pass in a stub e.g. app.visit('/mypage', {request: {}}), then I get TypeError: Cannot read property 'cookie' of undefined. Is it possible to render a page outside of an express server?