Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send the ads.txt file correctly using Fastify? #894

Open
Uriel29 opened this issue Jun 21, 2023 · 3 comments
Open

How to send the ads.txt file correctly using Fastify? #894

Uriel29 opened this issue Jun 21, 2023 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@Uriel29
Copy link

Uriel29 commented Jun 21, 2023

馃挰

Hello!

I need to send a file called ads.txt so that Google AdSense can work on my website!

I tried it this way first:

{
method:'GET',
url:'/ads.txt',
handler: async function (req, reply) {
reply.type('text/plain; charset=utf-8')
reply.send("google.com, pub-0000000, DIRECT, f00000000");
}

},

When I open the URL in the browser, it appears normal! But Google doesn't recognize that the file exists, even though the data is correct! So I tried another way:

handler: async function (req, reply) {

  const stream = fs.createReadStream('../public/ads.txt', 'utf8');
     reply.type('text/plain').send(stream);
}

When I check the URL, only a blank page appears! How can I resolve this with Fastify?

These data need to appear for Google to correctly serve the ads!

Thank you in advance.

Your Environment

  • node version: 20
  • fastify version: >=^4.17.0
  • os: Linux Ubuntu
  • any other relevant information: Nginx reverse proxy
@Uriel29 Uriel29 added the help wanted Extra attention is needed label Jun 21, 2023
@Uzlopak
Copy link

Uzlopak commented Jun 21, 2023

Why do you do async-await?

See:
https://www.fastify.io/docs/latest/Reference/Routes/#async-await

You cannot return undefined.

Try:

{
  method:'GET',
  url:'/ads.txt',
  handler: function (req, reply) {
    reply
      .type('text/plain; charset=utf-8')
      .send("google.com, pub-0000000, DIRECT, f00000000");
  }
}

@Uriel29
Copy link
Author

Uriel29 commented Jun 22, 2023

Hello! my first code works.
But it is not recognized by google!

//this code works, if you see the URL in the browser
{
method:'GET',
url:'/ads.txt',
handler: async function (req, reply) {
reply.type('text/plain; charset=utf-8')
reply.send("google.com, pub-0000000, DIRECT, f00000000");
}

},

Must be missing something that google needs.

Hugs

@Uzlopak
Copy link

Uzlopak commented Jun 22, 2023

E.g.

  • add a newline at the end of the file.
  • wait till google reindexes the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants