Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually add a .txt file at page root in Gatsby JS?

I want to retrieve access to a Google Analytics account. Google has advised me to create an analytics.txt file at the root of the website such that htttp://my-site.com/analytics.txt.

How do you manually place a .txt file at the root of a site using Gatsby? Gatsby compiles JS files into HTML. I've tried manually placing an analytics.txt file in the /public folder but that doesn't seem to have worked either.

The Gatsby documentation is not helpful on this topic.

We are deploying the site using Netlify and storing the source code on Git Lab.

Any help would be appreciated.

like image 476
Richard Jarram Avatar asked Mar 02 '23 17:03

Richard Jarram


1 Answers

If you create in your root project a folder named static, it will compile with the same name and internal structure in the public folder, so you will be able to refer to those files and assets in your React components. You can check for further information in their docs.

However, in your case, you may want to use a Gatsby plugin to place your analytics code. Here's the documentation.

In the scenario you've described, your solution didn't work because the public folder is regenerated in each build/compilation of code so your .txt is deleted in each compilation.

like image 177
Ferran Buireu Avatar answered Apr 08 '23 18:04

Ferran Buireu