Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby): DSG support for FS routes #33697

Merged
merged 13 commits into from
Oct 29, 2021
Merged

Conversation

vladar
Copy link
Contributor

@vladar vladar commented Oct 27, 2021

Description

This PR adds support for DSG config for FS routes.

Caveat: it only runs in gatsby build right now. So there is no way to debug it in develop. Wiring it up to develop will be a follow-up. The reason why this is not a priority is that DSG in develop has no effect anyways.

Usage example

Say you have a template src/pages/posts/{MarkdownRemark.fields__slug}.js (using FS routes), then you can optionally mark some of the pages created with this template as defer using the following code:

import React from "react"
import { graphql } from "gatsby"

export default function Component(props) {
  return <pre>{JSON.stringify(props, null, 2)}</pre>
}

export async function config() {
  const { data } = graphql`
    {
      oldPosts: allMarkdownRemark(filter: { frontmatter: { date: { lt: "2015-05-27" } } }) {
        nodes { fields { slug } }
      }
    }
  `
  // Trim slashes because FS routes params have no slashes
  const oldPosts = new Set(data.oldPosts.nodes.map((n) => n.fields.slug.replace(/^\/|\/$/g, ``)))

  // Return a function that when called will return a config for FS route pages
  // (right now only `defer` is supported)
  return ({ params }) => {
    return {
      defer: oldPosts.has(params.fields__slug),
    }
  }
}

export const pageQuery = graphql`
  query BlogPost($id: String!) {
    markdownRemark(id: { eq: $id }) {
      id
      excerpt(pruneLength: 160)
      html
      frontmatter {
        title
        date(formatString: "MMMM DD, YYYY")
        description
      }
    }
  }
`

[ch38465]

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 27, 2021
@vladar vladar removed the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 27, 2021
@vladar vladar force-pushed the feat/fs-routes-config branch from bb255cf to 6938ae5 Compare October 29, 2021 11:53
@vladar vladar marked this pull request as ready for review October 29, 2021 13:14
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢 it ;)

@vladar vladar added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Oct 29, 2021
@gatsbybot gatsbybot merged commit dd46c64 into master Oct 29, 2021
@gatsbybot gatsbybot deleted the feat/fs-routes-config branch October 29, 2021 15:54
axe312ger pushed a commit that referenced this pull request Nov 9, 2021
* initial scaffolding to make it work

* Support `graphql` queries in the `config` function

* Support `graphql` tag in config queries

* fix ts

* some tests for config queries

* use `graphql` tagged template literal to run queries

* Update packages/gatsby/src/utils/page-mode.ts

Co-authored-by: Michal Piechowiak <[email protected]>

* Update packages/gatsby/src/utils/page-mode.ts

Co-authored-by: Michal Piechowiak <[email protected]>

* test two queries in one file

* rephrase TODO comment

* revert unneeded change

Co-authored-by: Michal Piechowiak <[email protected]>
@LekoArts LekoArts added the topic: render-mode Related to Gatsby's different rendering modes label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes topic: render-mode Related to Gatsby's different rendering modes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants