Gatsby 未生成正确的静态 HTML 文件

And*_*ter 3 reactjs gatsby

我正在开发一个基于 Gatsby 的网站,该网站目前进展顺利。但是在为生产构建时遇到了一个问题,我们没有在各种页面索引文件中获得任何静态 html,相反,看起来 Gatsby 将尝试从 javascript 注入页面,这与我们的预期相反。

我看过一些与 Gatsby 离线插件相关的帖子,但我已将其禁用,但这并没有解决问题。我们的页面不包含静态 html 输出,并且我希望 react-helmet 注入的元内容也不存在。

是否有关于如何调试构建以查看可能重置静态输出并将其替换为动态生成的 Gatsby 引导程序代码的任何建议。

该网站使用的插件是:

 plugins: [
    `gatsby-plugin-react-helmet`,
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/favicon.png`, // This path is relative to the root of the site.
      },
    },
    `gatsby-transformer-json`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: 'data',
        path: `${__dirname}/src/data/`,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-plugin-styled-components`,
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    //`gatsby-plugin-offline`,
    {
      resolve: 'gatsby-plugin-root-import',
      options: {
        src: path.join(__dirname, 'src'),
        pages: path.join(__dirname, 'src/pages'),
        images: path.join(__dirname, 'src/images'),
      },
    },
    `gatsby-plugin-transition-link`,
    {
      resolve: `gatsby-plugin-google-analytics`,
      options: {
        trackingId: process.env.GOOGLE_ANALYTICS_TRACKING_ID,
        head: true,
      },
    },
  ]
Run Code Online (Sandbox Code Playgroud)

提前感谢您的任何指点

M.C*_*M.C 5

有同样的问题。

看来 PersistGate 破坏了 ssr。由于 ssr 是在构建时完成的,我们不需要它,我们可以检查我们的环境。

wrap-with-provider.js从 1.更改为 2. 成功了

import React from "react"
import { Provider } from "react-redux"
import { PersistGate } from 'redux-persist/integration/react'

import {store, persistor} from "./src/redux/store"
Run Code Online (Sandbox Code Playgroud)

1:

export default ({ element }) => (
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      {element}
    </PersistGate>
  </Provider>
)
Run Code Online (Sandbox Code Playgroud)

2:

export default ({ element }) => {
  if (typeof window === "undefined") {
    return (
      <Provider store={store}>
        {element}
      </Provider>
    )
  }
  return (
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        {element}
      </PersistGate>
    </Provider>
  )
}
Run Code Online (Sandbox Code Playgroud)

  • 当我使用此解决方案时,我收到重复的内容? (3认同)

归档时间:

查看次数:

2197 次

最近记录:

5 年,11 月 前