ember-fastboot / ember-cli-fastboot

Server-side rendering for Ember.js apps
http://ember-fastboot.com/
MIT License
851 stars 160 forks source link

accessing fastboot service's "request" can throw error #256

Open bantic opened 8 years ago

bantic commented 8 years ago

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:

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?

hoIIer commented 4 years ago

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?