【发布时间】:2019-10-21 15:21:56
【问题描述】:
我一直在尝试在 Google Cloud Platform 上部署我使用 Gatsby 制作的静态应用。
我的问题在于 app.yaml 文件。
-
这是我到目前为止所做的:
做了一个盖茨比项目。
盖茨比构建。
已将公用文件夹上传到 GCS 存储桶。
使用以下命令将其复制到 Google Cloud Shell:gsutil rsync -r gs://static-site-test ./gatsby-test
cd gatsby-test
创建了一个 app.yaml 文件:
gcloud 应用部署
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: public/
upload: public/index.html
- url: /
static_dir: public/static
- 应用程序的第一页加载正常,但任何指向其他页面的链接都不起作用。
我认为我的问题在于 app.yaml,我不明白如何正确填写。
这是我的公用文件夹:
我有多个页面。
page-2 和 about 文件夹都包含一个 index.html 文件。
有什么想法吗?
我尝试了以下方法:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: public/index.html
upload: public/index.html
- url: /page-2
static_dir: public/page-2/index.html
还有
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: public/index.html
upload: public/index.html
- url: /page-2
upload: public/page-2/index.html
虽然我希望这种方法不正确,因为如果我有多个页面,这会变得很麻烦。
这是我的索引页:
import React from "react"
import { Link } from "gatsby"
import Layout from "../components/layout"
import Image from "../components/image"
import SEO from "../components/seo"
const IndexPage = () => (
<Layout>
<SEO title="Home" />
<h1>Hi people</h1>
<p>Welcome to your new Gatsby site.</p>
<p>Now go build something great.</p>
<div style={{ maxWidth: `300px`, marginBottom: `1.45rem` }}>
<Image />
</div>
<Link to="/page-2/">Go to page 2</Link>
<Link to="about">Go to about</Link>
<p>paragrap h</p>
<div style={{ color: `purple` }}>
<h1>Hello Gatsby!</h1>
<p>What a word</p>
<img src="https://source.unsplash.com/random/400x200" alt="" />
</div>
</Layout>
)
export default IndexPage
我的结果是我只能访问 index.html 页面,但是当我点击链接时,我得到一个“页面未找到错误”。
编辑:
我更改了我的 app.yaml,但现在我的图片有问题
runtime: python27
api_version: 1
threadsafe: true
handlers:
#site root
- url: /
static_files: public/index.html
upload: public/index.html
# page-2
- url: /page-2/
static_files: public/page-2/index.html
upload: public/page-2/index.html
# Page not found
- url: /.*
static_files: public/404/index.html
upload: public/404/index.html
# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: public/\1
upload: public/(.*\.(bmp|gif|ico|jpeg|jpg|png))
层次结构如下:
\公共\静态
├───6d91c86c0fde632ba4cd01062fd9ccfa
│├───59139
│├───af144
│├───b5207
│├───d3809
│├───e22c9
│└───fdbb0
└───d
在这些随机文件夹中是图像。
我还让我的图像直接显示在 public/ 文件夹中,但仍然不起作用。
这是我使用图像的方式:
import React from "react"
import { Link } from "gatsby"
import Layout from "../components/layout"
import Image from "../components/image"
import SEO from "../components/seo"
import logo from "../images/gatsby-icon.png"
import logo2 from "../../static/secondicon.png"
const IndexPage = () => (
<Layout>
<SEO title="Home" />
<h1>Hi people</h1>
<p>Welcome to your new Gatsby site.</p>
<p>Now go build something great.</p>
<div style={{ maxWidth: `300px`, marginBottom: `1.45rem` }}>
<Image />
</div>
<Link to="/page-2/">Go to page 2</Link>
<Link to="about">Go to about</Link>
<p>paragrap h</p>
<div style={{ color: `purple` }}>
<h1>Hello Gatsby!</h1>
<p>What a word</p>
<img src={logo} alt="" />
<p>Second icon:</p>
<img src={logo2} alt="" />
</div>
</Layout>
)
export default IndexPage
这是部署:https://static-site-test-256614.appspot.com/
我只是想部署错误的方式吗?我应该为此使用其他东西吗?
【问题讨论】:
-
为什么不直接从存储桶中为 Gatsby 站点提供服务呢?我已经为我的一个网站做了这个,没有什么问题。 link to gcloud docs on hosting static site
-
您好,谢谢您的回答。我知道那些文档,没有域我无法做到这一点。这就是我想使用引擎的原因,它给了我一个免费的域来测试。有没有办法把两者联系起来?
-
只创建一个bucket是行不通的。至少我不能让它工作:(
标签: javascript reactjs static gcloud gatsby