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!
source:https://uonfu.com/
Github User Rank List