简体   繁体   English

Nuxt-服务器端的Axios请求未发送引荐来源

[英]Nuxt - Axios requests on server side not sending referer

The story 故事

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:typeog:url og:url value is given by the API through the HTTP "Referer" header. API会通过HTTP“ Referer”标头提供og:url值。

What I'm doing now 我现在在做什么

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);
}

The problem 问题

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不可用。

The question 问题

How can I dynamically get my requests referers to send it with Axios request for both client-side and server-side requests? 如何动态获取我的请求引用,以便与Axios请求一起将其发送给客户端和服务器端?

Info about the versions: 有关版本的信息:
  • nuxt: 1.4.2 nuxt:1.4.2
  • @nuxtjs/axios: 5.3.1 @ nuxtjs / axios:5.3.1

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM