[英]Nuxt - Axios requests on server side not sending referer
My backend app provides SEO information for my sites pages. 我的后端应用程序为我的网站页面提供SEO信息。 One of this informations are OpenGraph meta tags, such as
og:type
and og:url
. 这些信息之一是OpenGraph元标记,例如
og:type
和og:url
。 og:url
value is given by the API through the HTTP "Referer" header. API会通过HTTP“ Referer”标头提供
og:url
值。
I'm using Axios module to make my requests. 我正在使用Axios模块发出请求。 Through
asyncData
function in my pages I can get the req
variable and it's headers.referer
property, which is what I want, like this: 通过页面中的
asyncData
函数,我可以获取req
变量及其headers.referer
属性,这是我想要的,如下所示:
// page.vue
async asyncData({ app, req }) {
app.$axios.setHeader('Referer', req.headers.referer);
}
If I am at the index page, let's say, then I click in a link to a dynamic page I got an error, for req
is not available on asyncData
function while navigating, I suppose . 假设,如果我在索引页面上,那么我单击指向动态页面的链接,但出现错误,因为我认为 ,导航时
asyncData
函数上的req
不可用。
How can I dynamically get my requests referers to send it with Axios request for both client-side and server-side requests? 如何动态获取我的请求引用,以便与Axios请求一起将其发送给客户端和服务器端?
You can do something like this: 您可以执行以下操作:
async asyncData({ app, req }) {
const referrer = process.client ? window.document.referrer : req.headers.referer
app.$axios.setHeader('Referer', referrer)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.