Skip to content

Commit

Permalink
fix: SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Aug 30, 2018
1 parent 1bde0ef commit 8cc2e1d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"presets": [
["@babel/preset-env", {
"modules": false
"modules": "commonjs"
}]
]
}
4 changes: 2 additions & 2 deletions generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chalk = require('chalk')
module.exports = (api, options, rootOptions) => {
api.extendPackage({
dependencies: {
'vue-apollo': '^3.0.0-beta.10',
'vue-apollo': '^3.0.0-beta.11',
},
devDependencies: {
'graphql-tag': '^2.9.0',
Expand Down Expand Up @@ -99,7 +99,7 @@ module.exports = (api, options, rootOptions) => {

const file = tsExists ? 'src/main.ts' : 'src/main.js'
api.injectImports(file, `import { createProvider } from './vue-apollo'`)
api.injectRootOptions(file, `provide: createProvider().provide(),`)
api.injectRootOptions(file, `apolloProvider: createProvider(),`)
} catch (e) {
api.exitLog(`Your main file couldn't be modified. You will have to edit the code yourself: https://github.com/Akryum/vue-cli-plugin-apollo#manual-code-changes`, 'warn')
}
Expand Down
12 changes: 7 additions & 5 deletions generator/templates/vue-apollo/default/src/vue-apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const httpEndpoint = process.env.VUE_APP_GRAPHQL_HTTP || 'http://localhost:4000/
// Files URL root
export const filesRoot = process.env.VUE_APP_FILES_ROOT || httpEndpoint.substr(0, httpEndpoint.indexOf('/graphql'))

Object.defineProperty(Vue.prototype, '$filesRoot', {
get: () => filesRoot,
})
Vue.prototype.$filesRoot = filesRoot
<%_ } %>
// Config
const defaultOptions = {
Expand Down Expand Up @@ -79,7 +77,9 @@ export function createProvider (options = {}) {

// Manually call this when user log in
export async function onLogin (apolloClient, token) {
localStorage.setItem(AUTH_TOKEN, token)
if (typeof localStorage !== 'undefined' && token) {
localStorage.setItem(AUTH_TOKEN, token)
}
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
try {
await apolloClient.resetStore()
Expand All @@ -91,7 +91,9 @@ export async function onLogin (apolloClient, token) {

// Manually call this when user log out
export async function onLogout (apolloClient) {
localStorage.removeItem(AUTH_TOKEN)
if (typeof localStorage !== 'undefined') {
localStorage.removeItem(AUTH_TOKEN)
}
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
try {
await apolloClient.resetStore()
Expand Down
10 changes: 6 additions & 4 deletions graphql-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ export function restartWebsockets (wsClient) {
}

function defaultGetAuth (tokenName) {
// get the authentication token from local storage if it exists
const token = localStorage.getItem(tokenName)
// return the headers to the context so httpLink can read them
return token ? `Bearer ${token}` : ''
if (typeof window !== 'undefined') {
// get the authentication token from local storage if it exists
const token = window.localStorage.getItem(tokenName)
// return the headers to the context so httpLink can read them
return token ? `Bearer ${token}` : ''
}
}

0 comments on commit 8cc2e1d

Please sign in to comment.