【问题标题】:Deploy static website to Google Cloud made with Gatsby将静态网站部署到使用 Gatsby 制作的 Google Cloud
【发布时间】:2019-10-21 15:21:56
【问题描述】:

我一直在尝试在 Google Cloud Platform 上部署我使用 Gatsby 制作的静态应用。

我的问题在于 app.yaml 文件。

  • 这是我到目前为止所做的:

    1. 做了一个盖茨比项目。

    2. 盖茨比构建。

    3. 已将公用文件夹上传到 GCS 存储桶。

    4. 使用以下命令将其复制到 Google Cloud Shell:gsutil rsync -r gs://static-site-test ./gatsby-test

    5. cd gatsby-test

    6. 创建了一个 app.yaml 文件:

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


【解决方案1】:

以下 app.yaml 文件应与 Gatsby 快速入门 example 一起正常工作:

runtime: python27
api_version: '1'
threadsafe: true

handlers:

  - url: /
    static_files: public/index.html
    upload: public/index.html

  - url: /((.*\/)*[^\/]+\.[^\/]+)$
    static_files: public/\1
    upload: public/.*

  - url: /(.*)$
    static_files: public/\1/index.html
    upload: public/.*/index.html

您可以参考以下post,了解更多在服务静态网站时的 app.yaml 配置示例,以及以下documentation,了解所有 app.yaml 元素。

【讨论】:

  • # 404 for all other website - url: /.* 这是否意味着对于任何其他端点,您会得到 404?此外,图像不起作用。我还需要什么?
  • 最后一个 URL 处理程序仅显示 404 html 网站,以防有人键入类似 your-project.appspot.com/whateverisnotdefined 或您网站上未定义的任何其他处理程序。如果您将图像作为静态文件保存在您的网站文件夹中,这些图像将不起作用。您还需要上传它们。简单看看这个post,根据自己的文件夹结构改一下# image files
  • ``` - url: /(.*\.(bmp|gif|ico|jpeg|jpg|png)) static_files: public/\1 上传: public/(.*\.( bmp|gif|ico|jpeg|jpg|png)) ``` 我试过这样,问题是图像在 public/static 文件夹中,但是有 gatsby 生成的随机文件夹。我这样做是为了让我的图像直接显示在 public/ 中,但它仍然不适用于我在 app.yaml 中的内容。代码的格式在这里很奇怪,所以我把它放在主帖中。
  • 我只是想部署错误的方式吗?我应该为此使用其他东西吗?
  • 我放弃了 GCloud,去了 AWS S3,我第一次尝试就成功了……我不知道为什么在 gcloud 上没有。
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 2020-04-04
  • 2020-12-18
  • 2018-03-23
  • 1970-01-01
相关资源
最近更新 更多