【问题标题】:Gatsby Script API returns 404 with src fileGatsby Script API 返回 404 和 src 文件
【发布时间】:2023-02-08 16:57:36
【问题描述】:

我正在尝试盖茨比,我有一个小的,可能很容易解决,但无法弄清楚。

我想在带有 Script API 的页面中使用脚本。该文件位于静态文件夹中。但是当我尝试加载它时,它返回 404 错误。

这是结构:

src
|---> components
|---> images
|---> pages
|     |---> mypage.js
|---> static
      |---> myscript.js

我的页面.js

import * as React from "react"
import { Link } from "gatsby"
import { Script } from "gatsby"


import Layout from "../components/layout"
import Seo from "../components/seo"

const MyPage = () => (
  <Layout>
    <h1>Hi from my page</h1>
    <Script id="hello" src="../static/test.js" />
    <p>Welcome</p>
    <Link to="/">Go back to the homepage</Link>
  </Layout>
)

export const Head = () => <Seo title="Page two" />

export default MyPage

脚本文件只是一个控制台日志。当我内联这个脚本时,它起作用了。但是当我将 src 与文件脚本的路径一起使用时,出现了 404 错误。

我错过了什么 ?

多谢

【问题讨论】:

    标签: gatsby


    【解决方案1】:

    好的,之后再弄清楚:我必须在项目的根目录中创建一个静态文件夹:https://www.digitalocean.com/community/tutorials/how-to-use-static-files-in-gatsby

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多