【发布时间】:2020-04-20 20:27:41
【问题描述】:
我正在我的 Gatsby 网站上建立一个博客。当我从我的网站导航到博客时,内容会加载。但是当我复制并粘贴博客的网址直接去那里时,内容将无法加载。只会加载模板。这是网址:https://www.squarepatch.io/Instructions/blog/
不确定是什么文件造成了这个问题,但这是我的模板文件:
import React from "react";
import { graphql, Link } from "gatsby";
import styled from "styled-components"
import './item.css'
import Layout from "../components/layout";
class Blog extends React.Component {
render() {
const item = this.props.data.markdownRemark
const siteTitle = this.props.data.site.siteMetadata.title
return (
<Layout location={this.props.location} title={siteTitle}>
<Article>
<Heading>
<Title>{item.frontmatter.title}</Title>
<Date>{item.frontmatter.date}</Date>
</Heading>
<Body>
<Text dangerouslySetInnerHTML={{ __html: item.html }} />
</Body>
<Closing>
<Questions>????Have a question / comment / concern? Feel free to <Link to='/contact' style={{ fontWeight: `500`, fontStyle: `italic` }}>reach out!</Link></Questions>
</Closing>
</Article>
</Layout>
)
}
}
export default Blog;
export const pageQuery = graphql`
query BlogBySlug($slug: String!) {
site {
siteMetadata {
title
}
}
markdownRemark(fields: { slug: { eq: $slug } }) {
html
fields {
slug
}
frontmatter {
title
date
}
}
}
`
这里是节点文件
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const item = path.resolve(`./src/templates/item.js`)
const blog = path.resolve(`./src/templates/blog.js`)
return graphql(
`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
title
posttype
}
}
}
}
}
`
).then(result => {
if (result.errors) {
throw result.errors
}
// Create item posts pages.
const posts = result.data.allMarkdownRemark.edges
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
if (post.node.frontmatter.posttype === 'product') { //product post
createPage({
path: post.node.fields.slug,
component: item,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
} else { //blog post
createPage({
path: post.node.fields.slug,
component: blog,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
}
})
return null
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value
})
}
}
【问题讨论】:
-
您正在检索一个
null值,该值在您的请求中生成 404 (/blog/nulll)。查看检查器中的网络选项卡。我需要更多信息,但是,您的查询似乎有误。如果您提供更多信息(例如您的查询),我会尝试正确回答您的问题。 -
谢谢。我刚刚更新了节点文件