Skip to content

Commit 2989326

Browse files
Merge pull request #7513 from saurabhraghuvanshii/blog
fix: date sort in /blog
2 parents 536daa1 + a83fa9c commit 2989326

File tree

7 files changed

+27
-8
lines changed

7 files changed

+27
-8
lines changed

gatsby-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ module.exports = {
360360
}
361361
}
362362
allMdx(
363-
sort: {frontmatter: {date: DESC}}
363+
sort: {fields: {dateForSort: DESC}}
364364
limit: 20
365365
filter: {
366366
frontmatter: { published: { eq: true } }

gatsby-node.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,24 @@ exports.onCreateNode = ({ node, actions, getNode }) => {
845845
value: collection,
846846
});
847847

848+
// Normalize blog date to ISO string for stable sort order across build environments (fixes production blog order)
849+
if (collection === "blog") {
850+
let dateForSort = "1970-01-01T00:00:00.000Z";
851+
if (node.frontmatter?.date != null) {
852+
try {
853+
const parsed = new Date(node.frontmatter.date).toISOString();
854+
if (!Number.isNaN(Date.parse(parsed))) dateForSort = parsed;
855+
} catch {
856+
// keep fallback
857+
}
858+
}
859+
createNodeField({
860+
name: "dateForSort",
861+
node,
862+
value: dateForSort,
863+
});
864+
}
865+
848866
if (collection !== "content-learn") {
849867
let slug = "";
850868
if (node.frontmatter.permalink) {

src/pages/blog/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const BlogList = loadable(() => import ("../../sections/Blog/Blog-list"));
99

1010
export const query = graphql`query allBlogs {
1111
allMdx(
12-
sort: {frontmatter: {date: DESC}}
12+
sort: {fields: {dateForSort: DESC}}
1313
filter: {fields: {collection: {eq: "blog"}}, frontmatter: {published: {eq: true}}}
1414
) {
1515
nodes {
@@ -41,7 +41,8 @@ export const query = graphql`query allBlogs {
4141
}`;
4242

4343
const Blog = (props) => {
44-
if (props.data.allMdx.nodes.length === 0) {
44+
const nodes = props.data?.allMdx?.nodes ?? [];
45+
if (nodes.length === 0) {
4546
return (
4647
<LitePlaceholder
4748
pageContext={{
@@ -57,7 +58,7 @@ const Blog = (props) => {
5758
const [isListView, setIsListView] = useState(false);
5859
const [searchQuery, setSearchQuery] = useState("");
5960
const { queryResults, searchData } = useDataList(
60-
props.data.allMdx.nodes,
61+
nodes,
6162
setSearchQuery,
6263
searchQuery,
6364
["frontmatter", "title"],

src/sections/Blog/Blog-single/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const BlogSingle = ({ data, children }) => {
2727
const { relatedPosts: blogData, authors } = useStaticQuery(
2828
graphql`query relatedPosts {
2929
relatedPosts: allMdx(
30-
sort: {frontmatter: {date: DESC}}
30+
sort: {fields: {dateForSort: DESC}}
3131
filter: {fields: {collection: {eq: "blog"}}, frontmatter: {published: {eq: true}}}
3232
) {
3333
nodes {

src/sections/Platform-Engineering/platform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Platform = () => {
2121
const data = useStaticQuery(
2222
graphql`query relatedBlogPosts {
2323
relatedPosts: allMdx(
24-
sort: { frontmatter: { date: DESC } }
24+
sort: { fields: { dateForSort: DESC } }
2525
filter: {
2626
fields: { collection: { eq: "blog" } }
2727
frontmatter: {

src/templates/blog-category-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { graphql } from "gatsby";
77

88
export const query = graphql`query BlogsByCategory($category: String!) {
99
allMdx(
10-
sort: {frontmatter: {date: DESC}}
10+
sort: {fields: {dateForSort: DESC}}
1111
filter: {fields: {collection: {eq: "blog"}}, frontmatter: {category: {eq: $category}, published: {eq: true}}}
1212
) {
1313
nodes {

src/templates/blog-tag-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { graphql } from "gatsby";
77

88
export const query = graphql`query BlogsByTags($tag: String!) {
99
allMdx(
10-
sort: {frontmatter: {date: DESC}}
10+
sort: {fields: {dateForSort: DESC}}
1111
filter: {fields: {collection: {eq: "blog"}}, frontmatter: {tags: {in: [$tag]}, published: {eq: true}}}
1212
) {
1313
nodes {

0 commit comments

Comments
 (0)