问题描述
我在 Netlify 上托管了一个 Nuxt 静态应用程序。在 mounted()
钩子中,我调用了一个设置 cookie 的 Netlify 函数。这发生在页面加载并正常工作之后。
问题是,您能否在页面加载之前进行初始调用? Netlify 是否提供了某种方法来做到这一点?
Nuxt/Vue 代码:
mounted () {
this.myFirstFunctionCall()
},methods: {
async myFirstFunctionCall () {
const funcUrl = '/.netlify/functions/my-initial-function'
await this.$axios
.$get(funcUrl)
.then((res) => {
console.log(res)
})
.catch((error) => {
console.error(error)
})
}
}
exports.handler = async function (event) {
const COOKIE_NAME = 'myfirstcookie'
let cookieValue = null
try {
await getDataFromSomewhere
// ...
cookieValue = someDataThatJustGotFetched
} catch (err) {
console.log(err)
}
return {
statusCode: 200,headers: {
'Content-Type': 'application/json','Set-Cookie': COOKIE_NAME + '=' + cookieValue + '; Path=/; SameSite=Lax;'
},body: JSON.stringify({ hello: 'world' })
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)