Skip to content

Commit

Permalink
refactor(ssr): use ssrPrefetch (#469)
Browse files Browse the repository at this point in the history
* refactor(ssr): use ssrPrefetch

* fix: ssrPrefetch option was renamed to serverPrefetch

* docs(ssr): vue version notice

* fix: skip ssrPrefetch if apollo.$prefetch is false

* docs: new SSR docs

* chore: v3.0.0-beta.28
  • Loading branch information
Akryum authored Feb 4, 2019
2 parents 05dd426 + 455a5e7 commit 3f57cf5
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 724 deletions.
88 changes: 70 additions & 18 deletions dist/vue-apollo.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,6 @@ function () {
this._watchers = [];
this._destroyed = false;

if (this.vm.$isServer) {
this.options.fetchPolicy = 'cache-first';
}

if (autostart) {
this.autostart();
}
Expand Down Expand Up @@ -724,14 +720,23 @@ function (_SmartApollo) {
});
}

_this = _possibleConstructorReturn(this, _getPrototypeOf(SmartQuery).call(this, vm, key, options, autostart));
_this = _possibleConstructorReturn(this, _getPrototypeOf(SmartQuery).call(this, vm, key, options, false));

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "type", 'query');

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "vueApolloSpecialKeys", VUE_APOLLO_QUERY_KEYWORDS);

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_loading", false);

_this.firstRun = new Promise(function (resolve, reject) {
_this._firstRunResolve = resolve;
_this._firstRunReject = reject;
});

if (_this.vm.$isServer) {
_this.options.fetchPolicy = 'network-only';
}

if (!options.manual) {
_this.hasDataField = _this.vm.$data.hasOwnProperty(key);

Expand All @@ -754,6 +759,10 @@ function (_SmartApollo) {
}
}

if (autostart) {
_this.autostart();
}

return _this;
}

Expand Down Expand Up @@ -827,7 +836,12 @@ function (_SmartApollo) {
_get(_getPrototypeOf(SmartQuery.prototype), "nextResult", this).call(this, result);

var data = result.data,
loading = result.loading;
loading = result.loading,
error = result.error;

if (error) {
this.firstRunReject();
}

if (!loading) {
this.loadingDone();
Expand Down Expand Up @@ -861,7 +875,8 @@ function (_SmartApollo) {
value: function catchError(error) {
_get(_getPrototypeOf(SmartQuery.prototype), "catchError", this).call(this, error);

this.loadingDone();
this.firstRunReject();
this.loadingDone(error);
this.nextResult(this.observer.currentResult()); // The observable closes the sub if an error occurs

this.resubscribeToQuery();
Expand Down Expand Up @@ -901,11 +916,17 @@ function (_SmartApollo) {
}, {
key: "loadingDone",
value: function loadingDone() {
var error = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

if (this.loading) {
this.applyLoadingModifier(-1);
}

this.loading = false;

if (!error) {
this.firstRunResolve();
}
}
}, {
key: "fetchMore",
Expand Down Expand Up @@ -995,6 +1016,24 @@ function (_SmartApollo) {
return (_this$observer4 = this.observer).stopPolling.apply(_this$observer4, arguments);
}
}
}, {
key: "firstRunResolve",
value: function firstRunResolve() {
if (this._firstRunResolve) {
this._firstRunResolve();

this._firstRunResolve = null;
}
}
}, {
key: "firstRunReject",
value: function firstRunReject() {
if (this._firstRunReject) {
this._firstRunReject();

this._firstRunReject = null;
}
}
}, {
key: "destroy",
value: function destroy() {
Expand Down Expand Up @@ -1733,7 +1772,7 @@ function hasProperty(holder, key) {
return typeof holder !== 'undefined' && Object.prototype.hasOwnProperty.call(holder, key);
}

function initDollarApollo() {
function initProvider() {
var options = this.$options; // ApolloProvider injection

var optionValue = options.apolloProvider;
Expand Down Expand Up @@ -1797,6 +1836,8 @@ function launch() {
var apollo = this.$options.apollo;

if (apollo) {
this.$_apolloPromises = [];

if (!apollo.$init) {
apollo.$init = true; // Default options applied to `apollo` options

Expand Down Expand Up @@ -1824,7 +1865,11 @@ function launch() {
for (var key in apollo) {
if (key.charAt(0) !== '$') {
var options = apollo[key];
this.$apollo.addSmartQuery(key, options);
var smart = this.$apollo.addSmartQuery(key, options);

if (options.prefetch !== false && apollo.$prefetch !== false) {
this.$_apolloPromises.push(smart.firstRun);
}
}
}

Expand All @@ -1850,9 +1895,16 @@ function defineReactiveSetter($apollo, key, value, deep) {
}
}

function destroy() {
if (this.$_apollo) {
this.$_apollo.destroy();
this.$_apollo = null;
}
}

function installMixin(Vue, vueVersion) {
Vue.mixin(_objectSpread({}, vueVersion === '1' ? {
init: initDollarApollo
init: initProvider
} : {}, vueVersion === '2' ? {
data: function data() {
return {
Expand All @@ -1864,17 +1916,17 @@ function installMixin(Vue, vueVersion) {
};
},
beforeCreate: function beforeCreate() {
initDollarApollo.call(this);
initProvider.call(this);
proxyData.call(this);
},
serverPrefetch: function serverPrefetch() {
if (this.$_apolloPromises) {
return Promise.all(this.$_apolloPromises);
}
}
} : {}, {
created: launch,
destroyed: function destroyed() {
if (this.$_apollo) {
this.$_apollo.destroy();
this.$_apollo = null;
}
}
destroyed: destroy
}));
}

Expand Down Expand Up @@ -1925,7 +1977,7 @@ function install(Vue, options) {
}
ApolloProvider.install = install; // eslint-disable-next-line no-undef

ApolloProvider.version = "3.0.0-beta.27"; // Apollo provider
ApolloProvider.version = "3.0.0-beta.28"; // Apollo provider

var ApolloProvider$1 = ApolloProvider; // Components

Expand Down
2 changes: 1 addition & 1 deletion dist/vue-apollo.min.js

Large diffs are not rendered by default.

88 changes: 70 additions & 18 deletions dist/vue-apollo.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,6 @@
this._watchers = [];
this._destroyed = false;

if (this.vm.$isServer) {
this.options.fetchPolicy = 'cache-first';
}

if (autostart) {
this.autostart();
}
Expand Down Expand Up @@ -730,14 +726,23 @@
});
}

_this = _possibleConstructorReturn(this, _getPrototypeOf(SmartQuery).call(this, vm, key, options, autostart));
_this = _possibleConstructorReturn(this, _getPrototypeOf(SmartQuery).call(this, vm, key, options, false));

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "type", 'query');

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "vueApolloSpecialKeys", VUE_APOLLO_QUERY_KEYWORDS);

_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_loading", false);

_this.firstRun = new Promise(function (resolve, reject) {
_this._firstRunResolve = resolve;
_this._firstRunReject = reject;
});

if (_this.vm.$isServer) {
_this.options.fetchPolicy = 'network-only';
}

if (!options.manual) {
_this.hasDataField = _this.vm.$data.hasOwnProperty(key);

Expand All @@ -760,6 +765,10 @@
}
}

if (autostart) {
_this.autostart();
}

return _this;
}

Expand Down Expand Up @@ -833,7 +842,12 @@
_get(_getPrototypeOf(SmartQuery.prototype), "nextResult", this).call(this, result);

var data = result.data,
loading = result.loading;
loading = result.loading,
error = result.error;

if (error) {
this.firstRunReject();
}

if (!loading) {
this.loadingDone();
Expand Down Expand Up @@ -867,7 +881,8 @@
value: function catchError(error) {
_get(_getPrototypeOf(SmartQuery.prototype), "catchError", this).call(this, error);

this.loadingDone();
this.firstRunReject();
this.loadingDone(error);
this.nextResult(this.observer.currentResult()); // The observable closes the sub if an error occurs

this.resubscribeToQuery();
Expand Down Expand Up @@ -907,11 +922,17 @@
}, {
key: "loadingDone",
value: function loadingDone() {
var error = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

if (this.loading) {
this.applyLoadingModifier(-1);
}

this.loading = false;

if (!error) {
this.firstRunResolve();
}
}
}, {
key: "fetchMore",
Expand Down Expand Up @@ -1001,6 +1022,24 @@
return (_this$observer4 = this.observer).stopPolling.apply(_this$observer4, arguments);
}
}
}, {
key: "firstRunResolve",
value: function firstRunResolve() {
if (this._firstRunResolve) {
this._firstRunResolve();

this._firstRunResolve = null;
}
}
}, {
key: "firstRunReject",
value: function firstRunReject() {
if (this._firstRunReject) {
this._firstRunReject();

this._firstRunReject = null;
}
}
}, {
key: "destroy",
value: function destroy() {
Expand Down Expand Up @@ -1739,7 +1778,7 @@
return typeof holder !== 'undefined' && Object.prototype.hasOwnProperty.call(holder, key);
}

function initDollarApollo() {
function initProvider() {
var options = this.$options; // ApolloProvider injection

var optionValue = options.apolloProvider;
Expand Down Expand Up @@ -1803,6 +1842,8 @@
var apollo = this.$options.apollo;

if (apollo) {
this.$_apolloPromises = [];

if (!apollo.$init) {
apollo.$init = true; // Default options applied to `apollo` options

Expand Down Expand Up @@ -1830,7 +1871,11 @@
for (var key in apollo) {
if (key.charAt(0) !== '$') {
var options = apollo[key];
this.$apollo.addSmartQuery(key, options);
var smart = this.$apollo.addSmartQuery(key, options);

if (options.prefetch !== false && apollo.$prefetch !== false) {
this.$_apolloPromises.push(smart.firstRun);
}
}
}

Expand All @@ -1856,9 +1901,16 @@
}
}

function destroy() {
if (this.$_apollo) {
this.$_apollo.destroy();
this.$_apollo = null;
}
}

function installMixin(Vue, vueVersion) {
Vue.mixin(_objectSpread({}, vueVersion === '1' ? {
init: initDollarApollo
init: initProvider
} : {}, vueVersion === '2' ? {
data: function data() {
return {
Expand All @@ -1870,17 +1922,17 @@
};
},
beforeCreate: function beforeCreate() {
initDollarApollo.call(this);
initProvider.call(this);
proxyData.call(this);
},
serverPrefetch: function serverPrefetch() {
if (this.$_apolloPromises) {
return Promise.all(this.$_apolloPromises);
}
}
} : {}, {
created: launch,
destroyed: function destroyed() {
if (this.$_apollo) {
this.$_apollo.destroy();
this.$_apollo = null;
}
}
destroyed: destroy
}));
}

Expand Down Expand Up @@ -1931,7 +1983,7 @@
}
ApolloProvider.install = install; // eslint-disable-next-line no-undef

ApolloProvider.version = "3.0.0-beta.27"; // Apollo provider
ApolloProvider.version = "3.0.0-beta.28"; // Apollo provider

var ApolloProvider$1 = ApolloProvider; // Components

Expand Down
Loading

0 comments on commit 3f57cf5

Please sign in to comment.