Skip to content

Commit

Permalink
fix: move fetching global data from craft to server apis and use nitr…
Browse files Browse the repository at this point in the history
…o storage in these files to set and get
  • Loading branch information
pghorpade committed Feb 17, 2024
1 parent 95413dd commit 682f198
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 101 deletions.
7 changes: 5 additions & 2 deletions layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ const classes = computed(() => [
<slot />
<footer>
<footer-primary :form="true" />
<footer-sock />
<footer-primary
v-if="globalStore.footerPrimary"
:form="true"
/>
<footer-sock v-if="globalStore.footerSock" />
</footer>
<div id="libchat_5a44dfe7cc29aaee5bba635ab13fa753" />
</div>
Expand Down
127 changes: 63 additions & 64 deletions modules/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,86 +64,85 @@ query FooterSockItems {

export default defineNuxtModule({

async setup(options, nuxt) {
/* async setup(options, nuxt) {
console.log('Nuxt module start ')
const endpoint = nuxt.options.runtimeConfig.public.craftGraphqlURL
nuxt.hooks.hook("nitro:init", async (nitro) => {
console.log('Fetching global craft data...')
// console.log("what us this nuxt object", nuxt)
/* const endpoint = nuxt.options.runtimeConfig.public.craftGraphqlURL
nuxt.hooks.hook("nitro:init", async (nitro) => {
console.log('Fetching global craft data...')
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: globalsQuery }),
})
const data = await response.json()
console.log("Nitro Global Data object:" + JSON.stringify(data))
const setData = await nitro.storage.setItem('craftData:globals', data)
console.log("Nitro Global Data object:" + JSON.stringify(setData))
console.log("Nitro storage keys:" + JSON.stringify(await nitro.storage.getKeys()))
const globalGetData = await nitro.storage.getItem('craftData:globals')
console.log("Nitro Global Data object:" + JSON.stringify(globalGetData))
}
catch (e) {
throw new Error(`Craft API error, trying to set globals. ${e}`)
}
/* try {
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: headerQuery }),
body: JSON.stringify({ query: globalsQuery }),
})
const data = await response.json()
console.log("Nitro Header Data object:" + JSON.stringify(data))
await nitro.storage.setItem('craftData:header', data)
console.log("Nitro Global Data object:" + JSON.stringify(data))
const setData = await nitro.storage.setItem('craftData:globals', data)
console.log("Nitro Global Data object:" + JSON.stringify(setData))
console.log("Nitro storage keys:" + JSON.stringify(await nitro.storage.getKeys()))
const globalGetData = await nitro.storage.getItem('craftData:globals')
console.log("Nitro Global Data object:" + JSON.stringify(globalGetData))
}
catch (e) {
throw new Error(`Craft API error, trying to set globals Header. ${e}`)
throw new Error(`Craft API error, trying to set globals. ${e}`)
}
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: footerPrimaryQuery }),
})
/* try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: headerQuery }),
})
const data = await response.json()
console.log("Nitro Footer Primary Data object:" + JSON.stringify(data))
await nitro.storage.setItem('craftData:footerPrimary', data)
}
catch (e) {
throw new Error(`Craft API error, trying to set globals FooterPrimary. ${e}`)
}
const data = await response.json()
console.log("Nitro Header Data object:" + JSON.stringify(data))
await nitro.storage.setItem('craftData:header', data)
}
catch (e) {
throw new Error(`Craft API error, trying to set globals Header. ${e}`)
}
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: footerSockQuery }),
})
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: footerPrimaryQuery }),
})
const data = await response.json()
console.log("Nitro Footer Sock Data object:" + JSON.stringify(data))
await nitro.storage.setItem('craftData:footerSock', data)
}
catch (e) {
throw new Error(`Craft API error, trying to set globals FooterSockData. ${e}`)
}
const data = await response.json()
console.log("Nitro Footer Primary Data object:" + JSON.stringify(data))
await nitro.storage.setItem('craftData:footerPrimary', data)
}
catch (e) {
throw new Error(`Craft API error, trying to set globals FooterPrimary. ${e}`)
}
console.log('Craft data fetched succesfully!') */
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: footerSockQuery }),
})
}
const data = await response.json()
console.log("Nitro Footer Sock Data object:" + JSON.stringify(data))
await nitro.storage.setItem('craftData:footerSock', data)
}
catch (e) {
throw new Error(`Craft API error, trying to set globals FooterSockData. ${e}`)
}
console.log('Craft data fetched succesfully!')
console.log('Nuxt module end ')
} */
})
4 changes: 2 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export default defineNuxtConfig({
},
},

/*nitro: {
/* nitro: {
prerender: {
crawlLinks: true,
// ignore: ['/help', '/impact/all', '/about/reports'],
},
},*/
}, */

runtimeConfig: {
// Private keys are only available on the server
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"nuxt-graphql-request": "^7.0.5",
"sass": "^1.66.1",
"ucla-library-design-tokens": "^5.5.0",
"ucla-library-website-components": "2.39.0-alpha.40"
"ucla-library-website-components": "2.39.0-alpha.41"
}
}
8 changes: 4 additions & 4 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ useHead({

<section-wrapper>
<!-- fix card meta useroute add a check for route undefined-->
<!--section-teaser-highlight
<section-teaser-highlight
v-if="parsedSectionHighlightCollection.length > 1"
:items="parsedSectionHighlightCollection"
/-->
/>
<nuxt-link
to="/collections/explore"
class="button-more"
Expand Down Expand Up @@ -301,10 +301,10 @@ useHead({
</section-wrapper>

<section-wrapper>
<!--section-teaser-card
<section-teaser-card
v-if="parsedNewsList.length > 1"
:items="parsedNewsList"
/-->
/>
<nuxt-link
to="/about/news"
class="button-more"
Expand Down
5 changes: 2 additions & 3 deletions plugins/init.server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
export default defineNuxtPlugin(async (nuxtApp) => { //
/* console.log(
'this is called only once during build time and should not run on every page load',
) */
console.log('init.server.js start')
const craftGlobals = useGlobalStore(nuxtApp.$pinia) //
await craftGlobals.fetchGlobals()
await craftGlobals.fetchHeader()
await craftGlobals.fetchFooterPrimary()
await craftGlobals.fetchFooterSock()
console.log('init.server.js end')
})
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion server/api/fetchNitroStorageCraftFooterPrimaryData.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
const MenuItem = `
id
name: title
to: url
classes
target: newWindow
`
const footerPrimaryQuery = `
query FooterPrimaryItems {
nodes(navHandle: "footerPrimary", level: 1) {
${MenuItem}
children {
${MenuItem}
}
}
}
`
export default cachedEventHandler(async () => {
return await useStorage().getItem('craftData:footerPrimary')
const endpoint = useRuntimeConfig().public.craftGraphqlURL
let footerPrimaryData = await useStorage().getItem('craftData:footerPrimary')
console.log('Server api Craft Footer primary Data object:' + JSON.stringify(footerPrimaryData))
if (!footerPrimaryData) {
const { data } = await $fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: footerPrimaryQuery })
})
await useStorage().setItem('craftData:footerPrimary', data)
footerPrimaryData = await useStorage().getItem('craftData:footerPrimary')
console.log('Server api Craft Footer primary Data object first set and then get:' + JSON.stringify(footerPrimaryData))
}
return footerPrimaryData
})
32 changes: 31 additions & 1 deletion server/api/fetchNitroStorageCraftFooterSockData.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
const MenuItem = `
id
name: title
to: url
classes
target: newWindow
`
const footerSockQuery = `
query FooterSockItems {
nodes(navHandle: "footerSockLinks") {
${MenuItem}
}
}
`

export default cachedEventHandler(async () => {
return await useStorage().getItem('craftData:footerSock')
const endpoint = useRuntimeConfig().public.craftGraphqlURL
let footerSockData = await useStorage().getItem('craftData:footerSock')
console.log('Server api Craft Footer sock Data object:' + JSON.stringify(footerSockData))
if (!footerSockData) {
const { data } = await $fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: footerSockQuery })
})
await useStorage().setItem('craftData:footerSock', data)
footerSockData = await useStorage().getItem('craftData:footerSock')
console.log('Server api Craft Footer sock Data object first set and then get:' + JSON.stringify(footerSockData))
}
return footerSockData
})
40 changes: 38 additions & 2 deletions server/api/fetchNitroStorageCraftGlobalData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
const globalsQuery = `
query Globals {
globalSets {
dataId: id
handle
... on askALibrarian_GlobalSet {
askALibrarianTitle: titleGeneral
askALibrarianText: summary
buttonUrl {
buttonText
buttonUrl
}
}
... on libraryAlert_GlobalSet {
title: entryTitle
text: richTextAlertBox
}
}
}
`
export default cachedEventHandler(async () => {
const keys = await useStorage().getKeys()
const endpoint = useRuntimeConfig().public.craftGraphqlURL

// const keys = await useStorage().getKeys()
// console.log('Server api storage keys:' + JSON.stringify(keys))
const globalData = await useStorage().getItem('craftData:globals')
let globalData = await useStorage().getItem('craftData:globals')
console.log('Server api Global Data object:' + JSON.stringify(globalData))
if (!globalData) {
const { data } = await $fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: globalsQuery })
})
await useStorage().setItem('craftData:globals', data)
globalData = await useStorage().getItem('craftData:globals')
console.log('Server api Global Data object first set and then get:' + JSON.stringify(globalData))
}
return globalData
})
Loading

0 comments on commit 682f198

Please sign in to comment.