Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an arbitrary file to /public with Gatsby?

Tags:

gatsby

netlify

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.

like image 227
agentofuser Avatar asked May 04 '17 20:05

agentofuser


People also ask

What is static folder in Gatsby?

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.

How do you use local image in The Great Gatsby?

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.

Where do you put the assets in The Great Gatsby?

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.

What are SRC pages in Gatsby?

/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”.


2 Answers

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

like image 134
csukcc Avatar answered Sep 21 '22 00:09

csukcc


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.

like image 29
agentofuser Avatar answered Sep 21 '22 00:09

agentofuser