I would like to add a text file to /src
in a way that it always ends up unchanged in the root of the build directory (/public
).
In my case specifically I need to add a _redirects
file for Netlify to redirect a subdomain to a custom domain.
Running Gatsby 1.0 alpha.
You can create a folder named static at the root of your project. Every file you put into that folder will be copied into the public folder. E.g. if you add a file named sun.jpg to the static folder, it'll be copied to public/sun.jpg.
If you are using a local image, copy it into the project. A folder such as src/images is a good choice. Add the StaticImage component to your template.
There are two major ways to import assets, such as images, fonts, and files, into a Gatsby site. The default path is to import the file directly into a Gatsby template, page, or component. The alternative path, which makes sense for some edge cases, is to use the static folder.
/src This directory will contain all of the code related to what you will see on the frontend of your site (what you see in the browser), like your site header, or a page template. “src” is a convention for “source code”.
A bit late to the party.
The easiest way is to add static
folder in your project root directory, then place _redirects
inside it. Then when you build Gatsby will automatically pick that up and place it within /public
folder.
See official docs
Didn't need to put it in /src
. I just added it directly to /public
.
Even though it's in .gitignore
, I used git add -f /public/_redirects
, committed it, and it worked. It doesn't get deleted or overwritten during build.
Another way to do this (which I haven't tested) is to is to copy the file to its destination in /public as part of a post-build script in gatsby-node.js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With