将静态网站部署到Gatsby打造的Google Cloud

本教程将介绍将静态网站部署到Gatsby打造的Google Cloud的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

将静态网站部署到Gatsby打造的Google Cloud 教程 第1张

问题描述

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

我的问题是app.yaml文件。

    以下是我到目前为止所做的工作:

      创建了Gatsby项目。

      Gatsby版本。

      已将公用文件夹上载到GCS存储桶。

      使用以下命令将其复制到Google Cloud Shell:gsutil rsync-r gs://静态站点-测试。/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页面,但在单击链接时,我收到了"Page Not Found Error"(找不到页面错误)。

编辑:

我更改了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))

层次结构如下:

PUBLICSTATIC

├───6d91c86c0fde632ba4cd01062fd9ccfa

│├───59139

│├───af144

│├───b5207

│├───d3809

│├───e22c9

│└───fdb0

└───%d

在这些随机文件夹中是图像。

我还使我的图像直接显示在公用/文件夹中,但仍然不起作用。

以下是我使用图像的方式:

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/

我只是试图以错误的方式进行部署吗?我是否应该为此使用其他工具?

推荐答案

以下app.yaml文件应该可以与Gatsby QuickStart一起正常工作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配置示例,以及以下针对所有app.yaml元素的documentation配置示例。

好了关于将静态网站部署到Gatsby打造的Google Cloud的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。