Skip to content

fix(gatsby-transformer-javascript-frontmatter): support non-exported frontmatter to enable HMR#39428

Open
sudip-kumar-prasad wants to merge 4 commits intogatsbyjs:masterfrom
sudip-kumar-prasad:feature/fix-35756-frontmatter-hmr
Open

fix(gatsby-transformer-javascript-frontmatter): support non-exported frontmatter to enable HMR#39428
sudip-kumar-prasad wants to merge 4 commits intogatsbyjs:masterfrom
sudip-kumar-prasad:feature/fix-35756-frontmatter-hmr

Conversation

@sudip-kumar-prasad
Copy link
Copy Markdown

Summary

Fixes #35756

Currently, gatsby-transformer-javascript-frontmatter requires frontmatter to be an exported variable. However, exporting non-component objects from a page file breaks React Fast Refresh (HMR), causing full page reloads on every change.

This PR updates the plugin to support extracting frontmatter from non-exported variables (e.g., const frontmatter = { ... }). This allows developers to define frontmatter without violating Fast Refresh's requirement that files only export React components.

Changes

  • Updated src/gatsby-node.js to visit VariableDeclaration nodes and extract frontmatter even if it is not exported.
  • Added a regression test in src/__tests__/gatsby-node.js to verify that non-exported frontmatter is correctly parsed.

Copilot AI review requested due to automatic review settings February 11, 2026 19:46
@gatsbot gatsbot Bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Feb 11, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #35756 by enabling gatsby-transformer-javascript-frontmatter to extract frontmatter from non-exported variables. Previously, the plugin required frontmatter to be exported, which broke React Fast Refresh (HMR) since exporting non-component objects causes full page reloads. The change allows developers to define frontmatter as a simple const variable while maintaining backward compatibility with exported frontmatter.

Changes:

  • Modified the AST traversal to visit VariableDeclaration nodes instead of only ExportNamedDeclaration nodes
  • Added parent type validation to process only top-level and exported variable declarations
  • Added test coverage for non-exported frontmatter variables

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/gatsby-transformer-javascript-frontmatter/src/gatsby-node.js Replaced ExportNamedDeclaration visitor with VariableDeclaration visitor that checks parent types to support both exported and non-exported frontmatter
packages/gatsby-transformer-javascript-frontmatter/src/tests/gatsby-node.js Added test case to verify non-exported frontmatter variables are correctly parsed
packages/gatsby-transformer-javascript-frontmatter/src/tests/snapshots/gatsby-node.js.snap Added snapshot for non-exported frontmatter test case

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 98 to 102
if (dataVariableDeclarator && dataVariableDeclarator.init) {
dataVariableDeclarator.init.properties.forEach(node => {
frontmatter[node.key.name] = parseData(node.value)
})
}
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

The code directly accesses dataVariableDeclarator.init.properties without verifying that init is an ObjectExpression. This will cause a runtime error if frontmatter is assigned a non-object value (e.g., const frontmatter = someVariable or const frontmatter = getFrontmatter()). Consider adding a type check similar to line 57-61 where ObjectExpression is explicitly checked before accessing .properties.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[gatsby-transformer-javascript-frontmatter] Frontmatter export breaks HMR

2 participants