Ajility
Ajility
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
thanks 🙂
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
yea I prefer RESTful APIs, but I'm using BigCommerce headless for my store & getting info about a product is maybe 4 different API calls in REST or 1 API call in GraphQL
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
I appreciate all of your help Sandvich.. I think I'm looking in the right direction now.. apollo is probably not what I want to use
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
I'm working with a lot of new to me technologies like GraphQL & SSR, so maybe its time to take a step back and read more about what Apollo does
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
the way I understand it, in ex2, it would not be SSR?
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
ex2. Other times, the request would originate from the client (the user clicks a related product on the product page they're currently on)
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
ex1. Sometimes the request to the product API can initiate on the server (the user visit a direct URL to a product page)
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
I've gone mad - sorry, so useAsyncQuery is what I'm currently trying to work with for SSR now
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
Let me try it out again and recall what the warning was..
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
I've been messing with issues related to SSR / client side for a while now.. there was some issue wirth useAsyncQuery
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
basically a customer can view different products during a session on my website.. as they navigate around, I keep information in the store cached incase they navigate to a product they've already viewed, they've already got that info loaded
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
So a pattern similar to the one i'm following to differentiate between SSR / client would be required for my use case..
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
Everything seems to work fine, but I get that console warning..
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
No description
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
@Sandvich - that saves some code, thank you. If I just useAsyncData on client side / server side, I see the following:
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
You will notice that I am tracking if the request is originated from Server Side or Client side using an argument that is passed to setProductData().. my pinia CatalogStore.fetchProductBySlug action looks like this:
async fetchProductBySlug(params: { is_server_side: boolean, slug: string, set_active?: true }): Promise<NuxtProduct> {
const variables = {
slug: `/products/${params.slug}/`,
}

let response: any
if (params.is_server_side) {
response = await useAsyncQuery({
query: getProductBySlugQuery,
variables: variables,
})

} else {
response = await useApolloClient().client.query({
query: getProductBySlugQuery,
variables: variables,
})
}

const data = params.is_server_side ? response.data.value : response.data
const product = bigCommerceToNuxtProduct(data.site.route.node)
this.addProduct(product)
if (params.set_active) {
this.setActiveProduct(product)
}
return product
},
async fetchProductBySlug(params: { is_server_side: boolean, slug: string, set_active?: true }): Promise<NuxtProduct> {
const variables = {
slug: `/products/${params.slug}/`,
}

let response: any
if (params.is_server_side) {
response = await useAsyncQuery({
query: getProductBySlugQuery,
variables: variables,
})

} else {
response = await useApolloClient().client.query({
query: getProductBySlugQuery,
variables: variables,
})
}

const data = params.is_server_side ? response.data.value : response.data
const product = bigCommerceToNuxtProduct(data.site.route.node)
this.addProduct(product)
if (params.set_active) {
this.setActiveProduct(product)
}
return product
},
I check if params.is_server_side is true. If It is, I use useAsyncQuery, else useApolloClient(). This is wordy and doesn't seem right.. how should I be approaching this type of issue? I think I'm missing something fundamental..
34 replies
NNuxt
Created by Ajility on 3/25/2024 in #❓・help
Handling API Requests on pages from Client / Server Side (SSR)
My pages/products/[product_slug].vue look like this:
<template>
<product-view :product="product"/>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router'
import { isEmpty } from 'lodash'
import { useCatalogStore } from '@/pinia/CatalogStore'

const route = useRoute()
const product: any = useState()
const productSlug: ComputedRef<string> = computed(() => {
return route.params.product_slug as string
})
const CatalogStore = useCatalogStore()

const setProductData = async(isServerSide: boolean = false) => {
if (productSlug.value) {
product.value = await CatalogStore.fetchProductBySlug({
is_server_side: isServerSide,
slug: productSlug.value
})
}
}

onMounted(async() => {
if (isEmpty(product.value)) {
await setProductData()
}
})

onServerPrefetch(async() => {
await setProductData(true)
})

watch(productSlug.value, async () => {
await setProductData()
})

definePageMeta({
key: route => 'view-product', // prevent newing up a component on each link click - reuse existing
})
</script>
<template>
<product-view :product="product"/>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router'
import { isEmpty } from 'lodash'
import { useCatalogStore } from '@/pinia/CatalogStore'

const route = useRoute()
const product: any = useState()
const productSlug: ComputedRef<string> = computed(() => {
return route.params.product_slug as string
})
const CatalogStore = useCatalogStore()

const setProductData = async(isServerSide: boolean = false) => {
if (productSlug.value) {
product.value = await CatalogStore.fetchProductBySlug({
is_server_side: isServerSide,
slug: productSlug.value
})
}
}

onMounted(async() => {
if (isEmpty(product.value)) {
await setProductData()
}
})

onServerPrefetch(async() => {
await setProductData(true)
})

watch(productSlug.value, async () => {
await setProductData()
})

definePageMeta({
key: route => 'view-product', // prevent newing up a component on each link click - reuse existing
})
</script>
34 replies
NNuxt
Created by Ajility on 3/10/2024 in #❓・help
Differentiating SSR from Client side requests with Pinia...
Is this something I can safely ignore? If not, how can I resolve it without writing duplicate code and while still using pinia store to cache results on the front-end, as the user navigates thru pages on the SPA? I am new to SSR. The docs indicate that its not recommended, but do not indicate why...
9 replies
NNuxt
Created by Ajility on 3/10/2024 in #❓・help
Differentiating SSR from Client side requests with Pinia...
No description
9 replies
NNuxt
Created by Ajility on 3/10/2024 in #❓・help
Differentiating SSR from Client side requests with Pinia...
Note the onServerPrefetch lifecycle, and the watchEffect. They both call thru to CatalogStore.fetchProductBySlug action previously mentioned.
9 replies