Skip to content
Permalink
Browse files

Merge pull request #933 from amazeeio/913-ssr-websocket-errors

UI: Disable websockets when rendering server side
  • Loading branch information...
Schnitzel committed Mar 4, 2019
2 parents 486c2fd + 04a46d1 commit 7f6defc5a7a334dfc2d3e0204883a051023eabf6
Showing with 20 additions and 17 deletions.
  1. +20 −17 services/ui/src/lib/ApiConnection.js
@@ -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([
@@ -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()
});

0 comments on commit 7f6defc

Please sign in to comment.
You can’t perform that action at this time.