我想把一个文本文件添加到/src
,并以一种方式使其始终不变地出现在构建目录(/public
)的根目录中。
特别是,在我的情况下,我需要添加一个文件 _redirects
以便Netlify将子域重定向到自定义域。
运行Gatsby 1.0 alpha。
来晚了一点。
最简单的方法是在你的项目根目录下添加一个名为static
的文件夹,然后将_redirects
文件放在其中。然后当你构建Gatsby时,Gatsby会自动获取它并将其放置在/public
文件夹中。
请参考官方文档
不需要将它放在/src
中。我直接将其添加到/public
中。
尽管已在.gitignore
中,我使用了git add -f /public/_redirects
,对其进行了提交,并且它能正常工作。它不会在构建期间被删除或覆盖。
另一种方法是通过在gatsby-node.js的post-build脚本中将文件复制到其在/public中的目标位置。
Gatsby现在建议使用./static/
目录将文件原封不动地复制到构建目标中。
_redirects
文件或任何文件即可,就像这样:
"build": "gatsby build && cp src/_redirects public/"
每次构建部署时,它都会复制您的文件。希望这能帮助到某些人!
gatsby-plugin-copy-files-enhanced
插件并编辑 gatsby-config.js 文件解决了一个类似的问题,编辑如下:{
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,
},
},
注意: 不要忘记将你的_redirects
文件移动到src/extra
目录下。
postBuild
替换为onPostBuild
。 例如:https://spectrum.chat/gatsby-js/general/generating-json-file-with-gatsby~c539d9a7-970c-4a52-ac79-18a51f254537?m=MTUyMzk5NjM1OTk2Nw== - Emil B. Kjeruser-select: none;
的index.html
文件,刚刚运行了命令git add -f public/index.html
,推送到Netlify,但用户仍然可以选择。任何提示都将是很好的。 - MadHatter