18

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.

4

5 回答 5

27

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

于 2018-07-11T14:13:01.003 回答
8

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.

于 2017-05-04T21:10:53.143 回答
0

Even though this question answered, I found another way to do this and I wanted to share. Just copy your _redirects file or any file during build time, like this:

"build": "gatsby build && cp src/_redirects public/"

and it will copy your file everytime you build for deployment. I hope this helps somebody!

于 2020-05-22T04:29:00.467 回答
0

I solved a similar issue by installing gatsby-plugin-copy-files-enhanced plugin and editing gatsby-config.js like the following:

{
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `extra`,
    path: `${__dirname}/src/extra`,
  },
},
{
  resolve: 'gatsby-plugin-copy-files-enhanced',
  options: {
    source: `${__dirname}/src/extra`,
    destination: '/',
    purge: false,
  },
},

Note: Don't forget to move your _redirects file to src/extra.

于 2020-09-11T03:35:54.000 回答
0

Gatsby now recommends using the ./static/ directory for files to be copied verbatim into the build destination.

于 2021-03-30T19:44:00.713 回答