Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit 4a308c3

Browse files
committed
Add guards for skipping socket use when using Prerender
1 parent 77003f1 commit 4a308c3

11 files changed

Lines changed: 70 additions & 36 deletions

File tree

client/app/adapters/socket-source.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Ember from 'ember';
22
import Orbit from 'orbit';
33
import OC from 'orbit-common';
4-
import SocketService from '../services/socket';
4+
import { SocketService } from '../services/socket';
55
import JSONAPISource from 'orbit-common/jsonapi-source';
66

77
Orbit.Promise = Orbit.Promise || Ember.RSVP.Promise;

client/app/initializers/socket.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import EO from 'ember-orbit';
2-
import SocketService from '../services/socket';
2+
import { SocketService, canUseSocket } from '../services/socket';
33

44
export default {
55
name: 'socket',
66

77
initialize: function (container, app) {
8+
if (!canUseSocket()) { return; }
89
container.register('socket:main', SocketService, { singleton: false });
910
app.inject('controller', 'socket', 'socket:main');
1011
app.inject('route', 'socket', 'socket:main');

client/app/mixins/push-support.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import Ember from 'ember';
22

33
export default Ember.Mixin.create({
44

5-
beforeModel: function () {
6-
this.socketSanityCheck();
7-
return this._super();
8-
},
9-
105
socketSanityCheck: function () {
11-
// Sanity check, is socket working? check output browser console.
12-
var socket = this.socket;
13-
socket.on('hello', function (data) {
14-
console.log(data);
15-
socket.emit('talk-to-me', 'I like talking.', function (msg) {
16-
console.log('back talk', msg);
6+
try {
7+
// Sanity check, is socket working? check output browser console.
8+
var socket = this.socket;
9+
socket.on('hello', function (data) {
10+
console.log(data);
11+
socket.emit('talk-to-me', 'I like talking.', function (msg) {
12+
console.log('back talk', msg);
13+
});
1714
});
18-
});
15+
} catch (e) {
16+
console.warn('Push support socket sanity check failed.');
17+
}
1918
},
2019

2120
// Template methods...

client/app/routes/application.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var ApplicationRoute = Ember.Route.extend(PushSupport, {
1515
this._pingOk = true;
1616
}.bind(this));
1717
}
18+
this.socketSanityCheck();
1819
return this._super();
1920
},
2021

@@ -55,7 +56,11 @@ var ApplicationRoute = Ember.Route.extend(PushSupport, {
5556
// Push support...
5657

5758
onDidPatch: function () {
58-
this.socket.on('didPatch', this.patchRecord.bind(this));
59+
try {
60+
this.socket.on('didPatch', this.patchRecord.bind(this));
61+
} catch (e) {
62+
console.warn('Push support not enabled for application route.');
63+
}
5964
}.on('init'),
6065

6166
addLink: function (operation) {

client/app/routes/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export default Ember.Route.extend(ResetScroll, RecordChunksMixin, PushSupport, {
3636
// Push support...
3737

3838
onDidPatch: function () {
39-
this.socket.on('didPatch', this.patchRecord.bind(this));
39+
try {
40+
this.socket.on('didPatch', this.patchRecord.bind(this));
41+
} catch (e) {
42+
console.warn('Push support not enabled for index route.');
43+
}
4044
}.on('init'),
4145

4246
addRecord: function (operation) {

client/app/routes/posts/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export default Ember.Route.extend(ResetScroll, RecordChunksMixin, PushSupport, {
1414
// Push support...
1515

1616
onDidPatch: function () {
17-
this.socket.on('didPatch', this.patchRecord.bind(this));
17+
try {
18+
this.socket.on('didPatch', this.patchRecord.bind(this));
19+
} catch (e) {
20+
console.warn('Push support not enabled for index route.');
21+
}
1822
}.on('init'),
1923

2024
addRecord: function (operation) {

client/app/services/socket.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config from '../config/environment';
22

3-
function SocketService() {
3+
export function SocketService() {
44
return this;
55
}
66

@@ -13,7 +13,7 @@ SocketService.create = function () {
1313
}
1414
var socket;
1515
try {
16-
if (navigator.onLine) {
16+
if (navigator.onLine && canUseSocket()) {
1717
socket = window.io(config.APP.SOCKET_URL);
1818
socket.on('connect_failed', function () {
1919
socket = undefined;
@@ -25,7 +25,7 @@ SocketService.create = function () {
2525
console.error('Socket Error!', e);
2626
});
2727
} else {
28-
window.alert('Your network is offline');
28+
console.warn('Your network is offline');
2929
}
3030
} catch (e) {
3131
if (typeof window.io === 'undefined') {
@@ -37,4 +37,10 @@ SocketService.create = function () {
3737
return instance;
3838
};
3939

40-
export default SocketService;
40+
export function canUseSocket() {
41+
return window.WebSocket && notPrerenderService();
42+
}
43+
44+
function notPrerenderService() {
45+
return window.navigator.userAgent.match(/Prerender/) === null;
46+
}

client/app/views/post.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import Ember from 'ember';
33
export default Ember.View.extend({
44
didInsertElement: function () {
55
var notTesting = !Ember.testing;
6-
var notPrerendering = window.location.search.match(/_escaped_fragment_/) === null;
6+
var notCrawler = window.location.search.match(/_escaped_fragment_/) === null;
7+
var notPrerender = window.navigator.userAgent.match(/Prerender/) === null;
78
var notLocal = window.location.hostname.match(/localhost/) === null;
8-
if (notTesting && notPrerendering && notLocal) {
9+
if (notTesting && notCrawler && notPrerender && notLocal) {
910
this.configureDisqus();
1011
this.setupDisqus();
1112
}

client/public/sitemap.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,23 +253,27 @@
253253
<lastmod>2014-03-13T07:00:00.000Z</lastmod>
254254
</url>
255255
<url>
256-
<loc>http://pixelhandler.com/posts/real-time-data-for-an-emberjs-application-using-websockets</loc>
257-
<lastmod>2014-08-20T07:00:00.000Z</lastmod>
256+
<loc>http://pixelhandler.com/posts/real-time-data-in-an-emberjs-application-with-websockets</loc>
257+
<lastmod>2014-11-16T07:00:00.000Z</lastmod>
258258
</url>
259259
<url>
260260
<loc>http://pixelhandler.com/posts/how-to-use-404-page-in-your-emberjs-application</loc>
261261
<lastmod>2014-08-29T07:00:00.000Z</lastmod>
262262
</url>
263+
<url>
264+
<loc>http://pixelhandler.com/posts/develop-embercomponents-for-sharing-as-ember-cli-addons-a-practical-example</loc>
265+
<lastmod>2014-11-25T07:00:00.000Z</lastmod>
266+
</url>
263267
<url>
264268
<loc>http://pixelhandler.com</loc>
265-
<lastmod>2014-08-29T07:00:00.000Z</lastmod>
269+
<lastmod>2014-11-25T07:00:00.000Z</lastmod>
266270
</url>
267271
<url>
268272
<loc>http://pixelhandler.com/about</loc>
269273
<lastmod>2014-03-02T08:00:00.000Z</lastmod>
270274
</url>
271275
<url>
272276
<loc>http://pixelhandler.com/posts</loc>
273-
<lastmod>2014-08-29T07:00:00.000Z</lastmod>
277+
<lastmod>2014-11-25T07:00:00.000Z</lastmod>
274278
</url>
275279
</urlset>

server/seeds/authors.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ var testData = [{
33
"id": "5c9b62ec-1569-448b-912a-97e6d62f493e",
44
"links": {
55
"posts": [
6-
"2e941340-2a35-4cbb-9202-4fb45862891d",
7-
"c5d13fd7-5614-4531-a1da-4fa0855fdcc8",
8-
"341207e0-cfd9-4d3a-a5ab-d2268ab2e472",
96
"7e163fe2-21fb-4cf9-9764-d3df2001fff5",
107
"327fc99b-f471-497c-9bfd-50c43110d309",
118
"65d42214-3a7b-4f26-95de-528bb593ea5b",
@@ -29,15 +26,15 @@ var testData = [{
2926
"2c2260b2-6130-4ee9-b836-1b1efc47bc80",
3027
"79a0e9af-9c91-4f2e-8a9a-46acf1906f93",
3128
"246426bb-f296-4231-86f4-14b133c9d46f",
32-
"c3a30eac-991a-4015-af97-30f4b728db4f",
33-
"f18ad48e-e87f-47df-bc35-82470daa8812",
3429
"06e1cbf1-f291-4b33-b395-42c62b01de85",
3530
"ecbcb94d-cc7a-4b19-8852-b3a76a0367d7",
31+
"0ada0c67-1736-46f3-b341-a0929174dff5",
32+
"7fbfba42-d265-4993-8dc2-2ed83124faa8",
33+
"c3a30eac-991a-4015-af97-30f4b728db4f",
34+
"f18ad48e-e87f-47df-bc35-82470daa8812",
3635
"48f07f58-beb5-4203-8629-45619fabfd89",
3736
"82e0ea4d-9a72-4013-bd8b-d70b64fca9aa",
3837
"8b9618cb-06a8-4b9f-897b-15cff2f3e318",
39-
"0ada0c67-1736-46f3-b341-a0929174dff5",
40-
"7fbfba42-d265-4993-8dc2-2ed83124faa8",
4138
"75931c66-f57f-4be8-8e20-9089dc2534d0",
4239
"7c2feec5-8b6b-41b8-a16f-d71f4e2154b9",
4340
"a2f03da1-21cc-42bc-9700-e20c138c824f",
@@ -68,7 +65,9 @@ var testData = [{
6865
"e4c13305-9b0b-4f08-ab36-acc82e9a7d61",
6966
"3c1c2baa-2639-44c7-a39c-b2d868679ad3",
7067
"7bba292d-d381-4c5b-a18a-2d63b9d55db1",
71-
"6f9dbb3c-a1a2-4031-a57a-ae1561f18d84"
68+
"6f9dbb3c-a1a2-4031-a57a-ae1561f18d84",
69+
"341207e0-cfd9-4d3a-a5ab-d2268ab2e472",
70+
"b4eef92a-b5ed-4ec4-9075-3f88c2add997"
7271
]
7372
},
7473
"name": "pixelhandler"

0 commit comments

Comments
 (0)