Skip to content

Commit

Permalink
fix: respect fetchOnServer option
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 1, 2020
1 parent fe74a77 commit 368f33d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@ import {
onServerPrefetch,
onBeforeMount,
} from '@vue/composition-api'
import { normalizeError } from '@nuxt/vue-app'

import { ComponentInstance } from '@vue/composition-api/dist/component'

function normalizeError(err: any) {
let message
if (!(err.message || typeof err === 'string')) {
try {
message = JSON.stringify(err, null, 2)
} catch (e) {
message = `[${err.constructor.name}]`
}
} else {
message = err.message || err
}
return {
...err,
message,
statusCode:
err.statusCode ||
err.status ||
(err.response && err.response.status) ||
500,
}
}

interface Fetch {
(context: ComponentInstance): void
}
Expand All @@ -23,6 +44,7 @@ interface AugmentedComponentInstance extends ComponentInstance {
_data?: any
_hydrated?: boolean
_fetchDelay?: number
_fetchOnServer?: boolean
}

function registerCallback(vm: ComponentInstance, callback: Fetch) {
Expand Down Expand Up @@ -61,6 +83,9 @@ async function callFetches(this: AugmentedComponentInstance) {
}

async function serverPrefetch(vm: AugmentedComponentInstance) {
if (!vm._fetchOnServer) {
return
}
// Call and await on $fetch
vm.$fetchState =
vm.$fetchState ||
Expand Down Expand Up @@ -108,6 +133,12 @@ export const useFetch = (callback: Fetch) => {

registerCallback(vm, callback)

if (typeof vm.$options.fetchOnServer === 'function') {
vm._fetchOnServer = vm.$options.fetchOnServer.call(vm) !== false
} else {
vm._fetchOnServer = vm.$options.fetchOnServer !== false
}

onServerPrefetch(serverPrefetch)

onBeforeMount(() => {
Expand Down

0 comments on commit 368f33d

Please sign in to comment.