ember-fastboot / ember-fastboot/ember-cli-fastboot

accessing fastboot service's "request" can throw error

Open
#256 1 comment 2 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
850
Forks
161
Avg merge
17h 45m
Merged PRs (30d)
19

Description

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('.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 `request` and uses it to [construct a `FastBootInfo`](https://github.com/ember-fastboot/fastboot/blob/b62e795c8c21c4a5dca09f2cf20e4367c843fc7b/src/ember-app.js#L197-L202)
- The FastbootInfo constructor [ignores the missing `request` and never sets `this.request` property](https://github.com/ember-fastboot/fastboot/blob/b62e795c8c21c4a5dca09f2cf20e4367c843fc7b/src/fastboot-info.js#L12-L14)
- The FastbootInfo instance is [injected into the fastboot service](https://github.com/ember-fastboot/fastboot/blob/3dbc026be449c9cd7e53f4c2864cccccab944725/src/fastboot-info.js#L32)
- When the ember app accesses `fastbootService.request`, the fastboot services builds a `RequestObject`, [passing it the "request" property](https://github.com/ember-fastboot/ember-cli-fastboot/blob/32d40b29ea2e66a5a1c997be8f511caf757c9cbb/app/services/fastboot.js#L88) that the FastbootInfo was instantiated with (which is undefined)
- The `RequestObject` [reads the `cookies` property on the undefined request](https://github.com/ember-fastboot/ember-cli-fastboot/blob/32d40b29ea2e66a5a1c997be8f511caf757c9cbb/app/services/fastboot.js#L14), 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`](https://github.com/ember-fastboot/fastboot-express-middleware/blob/f1124ef67c32fd2900e43bd22083068ab818d8db/src/index.js#L32).

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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.