Skip to content

Commit

Permalink
Merge pull request #933 from amazeeio/913-ssr-websocket-errors
Browse files Browse the repository at this point in the history
UI: Disable websockets when rendering server side
  • Loading branch information
Schnitzel authored Mar 4, 2019
2 parents 486c2fd + 04a46d1 commit 7f6defc
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions services/ui/src/lib/ApiConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ const ApiConnection = ({ children }) =>
},
})

const wsLink = new WebSocketLink({
uri: publicRuntimeConfig.GRAPHQL_API.replace(/https/, 'wss').replace(/http/, 'ws'),
options: {
reconnect: true,
connectionParams: {
authToken: auth.apiToken,
const HttpWebsocketLink = () => {
const wsLink = new WebSocketLink({
uri: publicRuntimeConfig.GRAPHQL_API.replace(/https/, 'wss').replace(/http/, 'ws'),
options: {
reconnect: true,
connectionParams: {
authToken: auth.apiToken,
},
},
},
});
});

const requestLink = ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink
);
return ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink
);
}

const client = new ApolloClient({
link: ApolloLink.from([
Expand All @@ -57,7 +59,8 @@ const ApiConnection = ({ children }) =>
);
if (networkError) console.log(`[Network error]: ${networkError}`);
}),
requestLink
// Disable websockets when rendering server side.
process.browser ? HttpWebsocketLink() : httpLink
]),
cache: new InMemoryCache()
});
Expand Down

0 comments on commit 7f6defc

Please sign in to comment.