profile
viewpoint

Ask questionscontextModified is not properly re-evaluated if pages are deleted & created

Description

In https://github.com/gatsbyjs/gatsby/commit/6d7c5766fb1b4f70ccaf54627389a3d2fc9ee68a logic was added to re-evaluate page query when page context was changed. However, this doesn't work in all cases: sometimes pageContext change and there's no way to retrigger a change

This is the code that re-evaluates if page context have changed:

https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/redux/actions/public.js#L475

But when we try to replace pages hooking into createPage, following this documentation

https://www.gatsbyjs.org/docs/creating-and-modifying-pages/#pass-context-to-pages

with some logic like this one:

exports.onCreatePage = ({ page, actions }) => {
  const { createPage, deletePage } = actions

  if (page.data.replace) {
    deletePage(page)
    // You can access the variable "house" in your page queries now
    createPage({
      ...page,
      context: {
        ...page.context,
        house: `Gryffindor`,
      },
    })
  }
}

and when using gatsby develop, after hitting the __refresh data endpoint, if the conditions when the page gets replaced change, the contextModified logic doesn't work, because after "deleting" the original page, the condition const contextModified = !!oldPage && !_.isEqual(oldPage.context, internalPage.context) evaluates to false (oldPage does not exist)..

Steps to reproduce

  • Gatsby develop with page.data.replace is false
  • Render page
  • Change remote data source so page.data.replace becomes true
  • Hit the __refresh endpoint
  • Render the page

Expected result

Replaced page is rendered with the new context

Actual result

Replaced page with the original context

gatsbyjs/gatsby

Answer questions pieh

You are right. I just wonder how we could fix that... deletePage has immediate effect so our store removes that completely and when we recreate it, there's nothing to compare.

I do wonder if we could figure out "entry" createPage action call to grab oldPage from it, run all of the onCreatePage cascade and then compare after everything finished.

useful!

Related questions

Error: Cannot create as TypeComposer the following value: Date. hot 3
Importing Link from gatsby breaks Storybook hot 2
Loading chunks while a new release is deployed hot 2
Webpack error #98123 when running "gatsby develop" hot 2
gatsby build error #11328 A page component must export a React component for it to be valid. Please make sure this file exports a React component: /Users/tiagosanchez/Documents/Projects/personalBlog/node_modules/gatsby-plugin-offline/app-shell.js hot 2
[gatsby-telemetry] error: src/postinstall.js not found in Linux environments hot 2
IE11: Object not valid as React Child hot 2
CircleCI build Error: spawn ENOMEM hot 2
[Help] I somehow managed to butcher my site's performance hot 2
Build gets stuck at Generating image thumbnails / Update schema on large sites hot 2
[gatsby-source-graphql] Shopify GraphQL Schema Error hot 2
gatsby-source-shopify unable to complete build hot 2
Gatsby's Use Of Polyfills and the 'Missing Resources for x' error hot 2
Autoprefixer "browsers" option is deprecated in v9, produces warnings hot 2
Image loading (via graphql) from front matter fails on Hot Reload when editing MDX while theme development via yarn workspaces hot 1
Github User Rank List