fix(nuxt-env): Configuration issue with websockets

After some intensive debugging I found out that `req` (on the server)
and `nuxtState` (on the client) have access to the configurations
changed at runtime.

How did I debug it:
```
yarn run build
env WEBSOCKET_URI=whatever yarn run start
```
I also put a `console.log` into `plugins/apollo-client.js`.
This commit is contained in:
roschaefer 2020-02-17 22:54:19 +01:00
parent 6ea2f3c8a9
commit 2e19cf6592
3 changed files with 13 additions and 4 deletions

View File

@ -2,3 +2,5 @@ MAPBOX_TOKEN="pk.eyJ1IjoiaHVtYW4tY29ubmVjdGlvbiIsImEiOiJjajl0cnBubGoweTVlM3VwZ2l
SENTRY_DSN_WEBAPP=
COMMIT=
PUBLIC_REGISTRATION=false
WEBSOCKETS_URI=ws://localhost:3000/api/graphql
GRAPHQL_URI=http://localhost:4000/

View File

@ -4,7 +4,13 @@ import dotenv from 'dotenv'
dotenv.config() // we want to synchronize @nuxt-dotenv and nuxt-env
const pkg = require('./package')
export const envWhitelist = ['NODE_ENV', 'MAPBOX_TOKEN', 'PUBLIC_REGISTRATION']
export const envWhitelist = [
'NODE_ENV',
'MAPBOX_TOKEN',
'PUBLIC_REGISTRATION',
'WEBSOCKETS_URI',
'GRAPHQL_URI',
]
const dev = process.env.NODE_ENV !== 'production'
const styleguidePath = '../styleguide'

View File

@ -5,11 +5,12 @@ const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
})
export default ({ app }) => {
const backendUrl = process.env.GRAPHQL_URI || 'http://localhost:4000'
export default ({ req, nuxtState }) => {
const { env } = req || nuxtState
const backendUrl = env.GRAPHQL_URI || 'http://localhost:4000'
return {
wsEndpoint: process.env.WEBSOCKETS_URI || 'ws://localhost:4000/graphql',
wsEndpoint: env.WEBSOCKETS_URI || 'ws://localhost:4000/graphql',
httpEndpoint: process.server ? backendUrl : '/api',
httpLinkOptions: {
credentials: 'same-origin',