Skip to content

Commit d5a4407

Browse files
author
Guillaume Chau
committed
Subscriptions are no longer run on server + SSR Fixes
1 parent 36e71ca commit d5a4407

File tree

8 files changed

+112
-74
lines changed

8 files changed

+112
-74
lines changed

dist/vue-apollo.esm.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ function reapply(options, context) {
23342334
return options;
23352335
}
23362336

2337-
var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore'];
2337+
var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore', 'prefetch', 'manual'];
23382338

23392339
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
23402340
return typeof obj;
@@ -2530,6 +2530,10 @@ var SmartApollo = function () {
25302530
}));
25312531
}
25322532

2533+
if (this.vm.$isServer) {
2534+
this.options.fetchPolicy = 'cache-first';
2535+
}
2536+
25332537
if (autostart) {
25342538
this.autostart();
25352539
}
@@ -3073,14 +3077,16 @@ var DollarApollo = function () {
30733077
value: function subscribe(options) {
30743078
var _this2 = this;
30753079

3076-
var observable = this.getClient(options).subscribe(options);
3077-
var _subscribe = observable.subscribe.bind(observable);
3078-
observable.subscribe = function (options) {
3079-
var sub = _subscribe(options);
3080-
_this2._apolloSubscriptions.push(sub);
3081-
return sub;
3082-
};
3083-
return observable;
3080+
if (!this.vm.$isServer) {
3081+
var observable = this.getClient(options).subscribe(options);
3082+
var _subscribe = observable.subscribe.bind(observable);
3083+
observable.subscribe = function (options) {
3084+
var sub = _subscribe(options);
3085+
_this2._apolloSubscriptions.push(sub);
3086+
return sub;
3087+
};
3088+
return observable;
3089+
}
30843090
}
30853091
}, {
30863092
key: 'addSmartQuery',
@@ -3092,18 +3098,20 @@ var DollarApollo = function () {
30923098
var smart = this.queries[key] = new SmartQuery(this.vm, key, options, false);
30933099
smart.autostart();
30943100

3095-
var subs = options.subscribeToMore;
3096-
if (subs) {
3097-
if (Array.isArray(subs)) {
3098-
subs.forEach(function (sub, index) {
3099-
_this3.addSmartSubscription('' + key + index, _extends({}, sub, {
3101+
if (!this.vm.$isServer) {
3102+
var subs = options.subscribeToMore;
3103+
if (subs) {
3104+
if (Array.isArray(subs)) {
3105+
subs.forEach(function (sub, index) {
3106+
_this3.addSmartSubscription('' + key + index, _extends({}, sub, {
3107+
linkedQuery: smart
3108+
}));
3109+
});
3110+
} else {
3111+
this.addSmartSubscription(key, _extends({}, subs, {
31003112
linkedQuery: smart
31013113
}));
3102-
});
3103-
} else {
3104-
this.addSmartSubscription(key, _extends({}, subs, {
3105-
linkedQuery: smart
3106-
}));
3114+
}
31073115
}
31083116
}
31093117

@@ -3112,12 +3120,14 @@ var DollarApollo = function () {
31123120
}, {
31133121
key: 'addSmartSubscription',
31143122
value: function addSmartSubscription(key, options) {
3115-
options = reapply(options, this.vm);
3123+
if (!this.vm.$isServer) {
3124+
options = reapply(options, this.vm);
31163125

3117-
var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false);
3118-
smart.autostart();
3126+
var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false);
3127+
smart.autostart();
31193128

3120-
return smart;
3129+
return smart;
3130+
}
31213131
}
31223132
}, {
31233133
key: 'defineReactiveSetter',
@@ -3351,8 +3361,9 @@ var ApolloProvider$1 = function () {
33513361

33523362
// Query
33533363
return new Promise(function (resolve, reject) {
3354-
var options = lodash_omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS);
3364+
var options = lodash_omit(queryOptions, [].concat(toConsumableArray(VUE_APOLLO_QUERY_KEYWORDS), ['fetchPolicy']));
33553365
options.variables = variables;
3366+
options.fetchPolicy = 'network-only';
33563367
client.query(options).then(resolve, reject);
33573368
});
33583369
}

dist/vue-apollo.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vue-apollo.umd.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ function reapply(options, context) {
23402340
return options;
23412341
}
23422342

2343-
var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore'];
2343+
var VUE_APOLLO_QUERY_KEYWORDS = ['variables', 'watch', 'update', 'result', 'error', 'loadingKey', 'watchLoading', 'skip', 'throttle', 'debounce', 'subscribeToMore', 'prefetch', 'manual'];
23442344

23452345
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
23462346
return typeof obj;
@@ -2536,6 +2536,10 @@ var SmartApollo = function () {
25362536
}));
25372537
}
25382538

2539+
if (this.vm.$isServer) {
2540+
this.options.fetchPolicy = 'cache-first';
2541+
}
2542+
25392543
if (autostart) {
25402544
this.autostart();
25412545
}
@@ -3079,14 +3083,16 @@ var DollarApollo = function () {
30793083
value: function subscribe(options) {
30803084
var _this2 = this;
30813085

3082-
var observable = this.getClient(options).subscribe(options);
3083-
var _subscribe = observable.subscribe.bind(observable);
3084-
observable.subscribe = function (options) {
3085-
var sub = _subscribe(options);
3086-
_this2._apolloSubscriptions.push(sub);
3087-
return sub;
3088-
};
3089-
return observable;
3086+
if (!this.vm.$isServer) {
3087+
var observable = this.getClient(options).subscribe(options);
3088+
var _subscribe = observable.subscribe.bind(observable);
3089+
observable.subscribe = function (options) {
3090+
var sub = _subscribe(options);
3091+
_this2._apolloSubscriptions.push(sub);
3092+
return sub;
3093+
};
3094+
return observable;
3095+
}
30903096
}
30913097
}, {
30923098
key: 'addSmartQuery',
@@ -3098,18 +3104,20 @@ var DollarApollo = function () {
30983104
var smart = this.queries[key] = new SmartQuery(this.vm, key, options, false);
30993105
smart.autostart();
31003106

3101-
var subs = options.subscribeToMore;
3102-
if (subs) {
3103-
if (Array.isArray(subs)) {
3104-
subs.forEach(function (sub, index) {
3105-
_this3.addSmartSubscription('' + key + index, _extends({}, sub, {
3107+
if (!this.vm.$isServer) {
3108+
var subs = options.subscribeToMore;
3109+
if (subs) {
3110+
if (Array.isArray(subs)) {
3111+
subs.forEach(function (sub, index) {
3112+
_this3.addSmartSubscription('' + key + index, _extends({}, sub, {
3113+
linkedQuery: smart
3114+
}));
3115+
});
3116+
} else {
3117+
this.addSmartSubscription(key, _extends({}, subs, {
31063118
linkedQuery: smart
31073119
}));
3108-
});
3109-
} else {
3110-
this.addSmartSubscription(key, _extends({}, subs, {
3111-
linkedQuery: smart
3112-
}));
3120+
}
31133121
}
31143122
}
31153123

@@ -3118,12 +3126,14 @@ var DollarApollo = function () {
31183126
}, {
31193127
key: 'addSmartSubscription',
31203128
value: function addSmartSubscription(key, options) {
3121-
options = reapply(options, this.vm);
3129+
if (!this.vm.$isServer) {
3130+
options = reapply(options, this.vm);
31223131

3123-
var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false);
3124-
smart.autostart();
3132+
var smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false);
3133+
smart.autostart();
31253134

3126-
return smart;
3135+
return smart;
3136+
}
31273137
}
31283138
}, {
31293139
key: 'defineReactiveSetter',
@@ -3357,8 +3367,9 @@ var ApolloProvider$1 = function () {
33573367

33583368
// Query
33593369
return new Promise(function (resolve, reject) {
3360-
var options = lodash_omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS);
3370+
var options = lodash_omit(queryOptions, [].concat(toConsumableArray(VUE_APOLLO_QUERY_KEYWORDS), ['fetchPolicy']));
33613371
options.variables = variables;
3372+
options.fetchPolicy = 'network-only';
33623373
client.query(options).then(resolve, reject);
33633374
});
33643375
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-apollo",
3-
"version": "2.1.0-rc.5",
3+
"version": "2.1.0-rc.8",
44
"description": "Vue apollo integration",
55
"main": "dist/vue-apollo.umd.js",
66
"module": "dist/vue-apollo.esm.js",

src/apollo-provider.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ export class ApolloProvider {
130130

131131
// Query
132132
return new Promise((resolve, reject) => {
133-
const options = omit(queryOptions, VUE_APOLLO_QUERY_KEYWORDS)
133+
const options = omit(queryOptions, [
134+
...VUE_APOLLO_QUERY_KEYWORDS,
135+
'fetchPolicy',
136+
])
134137
options.variables = variables
138+
options.fetchPolicy = 'network-only'
135139
client.query(options).then(resolve, reject)
136140
})
137141
}

src/consts.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export const VUE_APOLLO_QUERY_KEYWORDS = [
1010
'throttle',
1111
'debounce',
1212
'subscribeToMore',
13+
'prefetch',
14+
'manual',
1315
]

src/dollar-apollo.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ export class DollarApollo {
6363
}
6464

6565
subscribe (options) {
66-
const observable = this.getClient(options).subscribe(options)
67-
const _subscribe = observable.subscribe.bind(observable)
68-
observable.subscribe = (options) => {
69-
let sub = _subscribe(options)
70-
this._apolloSubscriptions.push(sub)
71-
return sub
66+
if (!this.vm.$isServer) {
67+
const observable = this.getClient(options).subscribe(options)
68+
const _subscribe = observable.subscribe.bind(observable)
69+
observable.subscribe = (options) => {
70+
let sub = _subscribe(options)
71+
this._apolloSubscriptions.push(sub)
72+
return sub
73+
}
74+
return observable
7275
}
73-
return observable
7476
}
7577

7678
addSmartQuery (key, options) {
@@ -79,33 +81,37 @@ export class DollarApollo {
7981
const smart = this.queries[key] = new SmartQuery(this.vm, key, options, false)
8082
smart.autostart()
8183

82-
const subs = options.subscribeToMore
83-
if (subs) {
84-
if (Array.isArray(subs)) {
85-
subs.forEach((sub, index) => {
86-
this.addSmartSubscription(`${key}${index}`, {
87-
...sub,
84+
if (!this.vm.$isServer) {
85+
const subs = options.subscribeToMore
86+
if (subs) {
87+
if (Array.isArray(subs)) {
88+
subs.forEach((sub, index) => {
89+
this.addSmartSubscription(`${key}${index}`, {
90+
...sub,
91+
linkedQuery: smart,
92+
})
93+
})
94+
} else {
95+
this.addSmartSubscription(key, {
96+
...subs,
8897
linkedQuery: smart,
8998
})
90-
})
91-
} else {
92-
this.addSmartSubscription(key, {
93-
...subs,
94-
linkedQuery: smart,
95-
})
99+
}
96100
}
97101
}
98102

99103
return smart
100104
}
101105

102106
addSmartSubscription (key, options) {
103-
options = reapply(options, this.vm)
107+
if (!this.vm.$isServer) {
108+
options = reapply(options, this.vm)
104109

105-
const smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false)
106-
smart.autostart()
110+
const smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false)
111+
smart.autostart()
107112

108-
return smart
113+
return smart
114+
}
109115
}
110116

111117
defineReactiveSetter (key, func) {

src/smart-apollo.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class SmartApollo {
3232
}))
3333
}
3434

35+
if (this.vm.$isServer) {
36+
this.options.fetchPolicy = 'cache-first'
37+
}
38+
3539
if (autostart) {
3640
this.autostart()
3741
}

0 commit comments

Comments
 (0)