Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] FastBoot compatibility #819

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import Ember from 'ember';
import FastbootUtils from '../mixins/fastboot-utils';

export default Ember.Controller.extend({
export default Ember.Controller.extend(FastbootUtils, {
searchController: Ember.inject.controller('search'),
search: Ember.computed.oneWay('searchController.q'),

init() {
this._super(...arguments);
Ember.$(document).on('keypress', this.handleKeyPress.bind(this));
Ember.$(document).on('keydown', this.handleKeyPress.bind(this));
if (this.get('isNotFastBoot')) {
Ember.$(window.document).on('keypress', this.handleKeyPress.bind(this));
Ember.$(window.document).on('keydown', this.handleKeyPress.bind(this));
}
},

// Gets the human-readable string for the virtual-key code of the
Expand Down Expand Up @@ -47,8 +50,10 @@ export default Ember.Controller.extend({
},

willDestroy() {
Ember.$(document).off('keypress');
Ember.$(document).off('keydown');
if (this.get('isNotFastBoot')) {
Ember.$(window.document).off('keypress');
Ember.$(window.document).off('keydown');
}
},

actions: {
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const TO_SHOW = 5;
const { computed, inject: { service } } = Ember;

export default Ember.Controller.extend({
export default Ember.Controller.extend(FastBootUtils, {

ajax: service(),

Expand Down Expand Up @@ -44,7 +45,7 @@ export default Ember.Controller.extend({
this.set('loadingMore', true);
let page = (this.get('myFeed').length / 10) + 1;

this.get('ajax').request(`/me/updates?page=${page}`).then((data) => {
this.get('ajax').request(`${this.get('appURL')}/me/updates?page=${page}`).then((data) => {
let versions = data.versions.map(version =>
this.store.push(this.store.normalize('version', version)));

Expand Down
8 changes: 3 additions & 5 deletions app/controllers/me/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;
export default Ember.Controller.extend(FastBootUtils, {

export default Ember.Controller.extend({
tokenSort: ['created_at:desc'],

sortedTokens: Ember.computed.sort('model.api_tokens', 'tokenSort'),

ajax: service(),

flashMessages: service(),
flashMessages: Ember.inject.service(),

isResetting: false,

Expand Down
7 changes: 0 additions & 7 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
</head>
<body>
{{content-for 'body'}}
<noscript>
<div id="main">
<div class='noscript'>
This site requires that JavaScript to be enabled.
</div>
</div>
</noscript>

<script src="{{rootURL}}assets/vendor.js"></script>
<script src="{{rootURL}}assets/cargo.js"></script>
Expand Down
26 changes: 26 additions & 0 deletions app/mixins/fastboot-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Ember from 'ember';

const { computed, inject: { service } } = Ember;

export default Ember.Mixin.create({

fastboot: service(),

isNotFastBoot: computed.not('fastboot.isFastBoot'),

/**
* When there's a need to raise network requests from the server
* during Server Side Rendering(SSR) via FastBoot,
* the JS Fetch API doesn't work with relative urls.
* This property gives the URL prefix for those network calls.
* */
appURL: computed(function() {
let url = '';
if (this.get('fastboot.isFastBoot')) {
let protocol = this.get('fastboot.request.protocol');
let host = this.get('fastboot.request.host');
url = `${protocol}://${host}`;
}
return url;
}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment here might be valuable to understand why we need this

});
5 changes: 3 additions & 2 deletions app/routes/application.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;

export default Ember.Route.extend({
export default Ember.Route.extend(FastBootUtils, {

ajax: service(),

Expand All @@ -11,7 +12,7 @@ export default Ember.Route.extend({
beforeModel() {
if (this.session.get('isLoggedIn') &&
this.session.get('currentUser') === null) {
this.get('ajax').request('/me').then((response) => {
this.get('ajax').request(`${this.get('appURL')}/me`).then((response) => {
this.session.set('currentUser', this.store.push(this.store.normalize('user', response.user)));
}).catch(() => this.session.logoutUser()).finally(() => {
window.currentUserDetected = true;
Expand Down
5 changes: 3 additions & 2 deletions app/routes/crate/version.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;

export default Ember.Route.extend({
export default Ember.Route.extend(FastBootUtils, {

ajax: service(),

Expand Down Expand Up @@ -85,7 +86,7 @@ export default Ember.Route.extend({
controller.set('fetchingFollowing', true);

if (this.session.get('currentUser')) {
this.get('ajax').request(`/api/v1/crates/${crate.get('name')}/following`)
this.get('ajax').request(`${this.get('appURL')}/api/v1/crates/${crate.get('name')}/following`)
.then((d) => controller.set('following', d.following))
.finally(() => controller.set('fetchingFollowing', false));
}
Expand Down
5 changes: 3 additions & 2 deletions app/routes/github-authorize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;

Expand All @@ -15,12 +16,12 @@ const { inject: { service } } = Ember;
* @see https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site
* @see `/login` route
*/
export default Ember.Route.extend({
export default Ember.Route.extend(FastBootUtils, {

ajax: service(),

beforeModel(transition) {
return this.get('ajax').request(`/authorize`, { data: transition.queryParams }).then((d) => {
return this.get('ajax').request(`${this.get('appURL')}/authorize`, { data: transition.queryParams }).then((d) => {
let item = JSON.stringify({ ok: true, data: d });
if (window.opener) {
window.opener.github_response = item;
Expand Down
5 changes: 3 additions & 2 deletions app/routes/github-login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;

Expand All @@ -15,12 +16,12 @@ const { inject: { service } } = Ember;
* @see https://developer.github.com/v3/oauth/#redirect-users-to-request-github-access
* @see `/github_authorize` route
*/
export default Ember.Route.extend({
export default Ember.Route.extend(FastBootUtils, {

ajax: service(),

beforeModel() {
return this.get('ajax').request(`/authorize_url`).then((url) => {
return this.get('ajax').request(`${this.get('appURL')}/authorize_url`).then((url) => {
window.location = url.url;
});
},
Expand Down
7 changes: 5 additions & 2 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;

export default Ember.Route.extend({
export default Ember.Route.extend(FastBootUtils, {

ajax: service(),

Expand All @@ -21,7 +22,9 @@ export default Ember.Route.extend({
}
}

return this.get('ajax').request('/summary').then((data) => {
let summaryURL = `${this.get('appURL')}/summary`;

return this.get('ajax').request(summaryURL).then((data) => {
addCrates(this.store, data.new_crates);
addCrates(this.store, data.most_downloaded);
addCrates(this.store, data.just_updated);
Expand Down
5 changes: 3 additions & 2 deletions app/routes/logout.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Ember from 'ember';
import FastBootUtils from 'cargo/mixins/fastboot-utils';

const { inject: { service } } = Ember;

export default Ember.Route.extend({
export default Ember.Route.extend(FastBootUtils, {

ajax: service(),

activate() {
this.get('ajax').request(`/logout`).then(() => {
this.get('ajax').request(`${this.get('appURL')}/logout`).then(() => {
Ember.run(() => {
this.session.logoutUser();
this.transitionTo('index');
Expand Down
6 changes: 6 additions & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module.exports = function(environment) {
// Here you can pass flags/options to your application instance
// when it is created
},

fastboot: {
hostWhitelist: ['crates.io', /^[\w\-]+\.herokuapp\.com$/]
},

metricsAdapters: [{
name: 'GoogleAnalytics',
environments: ['production'],
Expand All @@ -39,6 +44,7 @@ module.exports = function(environment) {
// ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
ENV.fastboot.hostWhitelist = ['crates.io', /^[\w\-]+\.herokuapp\.com$/, /^localhost:\d+$/];
}

if (environment === 'test') {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
"ember-ajax": "^3.0.0",
"ember-cli": "^2.13.3",
"ember-cli-app-version": "^3.0.0",
"ember-cli-babel": "^6.4.1",
"ember-cli-babel": "^6.4.2",
"ember-cli-clipboard": "^0.7.0",
"ember-cli-dependency-checker": "^2.0.1",
"ember-cli-eslint": "^4.0.0",
"ember-cli-eslint": "^4.1.0",
"ember-cli-fastboot": "^1.0.0-rc.5",
"ember-cli-htmlbars": "^2.0.2",
"ember-cli-htmlbars-inline-precompile": "^0.4.3",
"ember-cli-inject-live-reload": "1.6.1",
Expand All @@ -53,7 +54,7 @@
"ember-moment": "^7.3.1",
"ember-normalize": "^1.0.0",
"ember-page-title": "^3.2.1",
"ember-resolver": "^4.1.0",
"ember-resolver": "^4.2.1",
"ember-router-scroll": "^0.2.0",
"ember-source": "^2.13.3",
"ember-svg-jar": "^0.10.3",
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/mixins/fastboot-utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';
import FastbootUtilsMixin from 'cargo/mixins/fastboot-utils';
import { module, test } from 'qunit';

module('Unit | Mixin | fastboot utils');

// Replace this with your real tests.
test('it works', function(assert) {
let FastbootUtilsObject = Ember.Object.extend(FastbootUtilsMixin);
let subject = FastbootUtilsObject.create();
assert.ok(subject);
});
Loading