Does Gatsby guarantee deterministic page mutations when multiple plugins implement onCreatePage?
0 reputation · 16 Sept 2026, 10:10 UTC
0 reputation · 16 Sept 2026, 10:10 UTC
Gatsby's onCreatePage Node API allows plugins to mutate page objects after createPages completes. When several plugins implement this lifecycle, each receives the page object already mutated by preceding plugins, so the final path, matchPath, and context values depend on execution sequence.
The only ordering control is the plugin array in gatsby-config.js. Gatsby documents conventional sequencing for site and theme plugins but does not expose a formal dependency declaration for onCreatePage, nor does it guarantee that the config order maps 1:1 to lifecycle execution across major versions (v2–v5+).
A later plugin can silently override fields set by an earlier plugin, and context values added in onCreatePage are only surfaced to GraphQL when they become part of pageContext. Without a transactional or declarative mechanism, teams must rely on implicit ordering and manual verification after upgrades.
Which Gatsby versions, if any, formally guarantee that plugin array order determines onCreatePage execution order? Does the API provide any mechanism to declare explicit dependencies between plugins for this lifecycle? What is the recommended verification strategy to ensure deterministic page mutations in a multi-plugin codebase?
No Gatsby version from v2 through v5+ is documented as formally guaranteeing that gatsby-config.js plugin array order determines onCreatePage execution order. Treat that order as conventional implementation behavior, not a contractual guarantee. The API also exposes no onCreatePage-specific dependsOn, before, or after declaration, and no transactional commit for page mutations.
onCreatePage executes after createPages completes.path, matchPath, and context remain visible to later handlers and can be silently overridden.onCreatePage is available to GraphQL only when it becomes part of pageContext.gatsby-config.js.Gatsby commonly invokes Node API implementations for each plugin in sequence, often themes before site plugins and the site's own gatsby-node.js late in the run. This sequence may change between major versions and is not protected by semver unless documented for the exact version you run.
package.json and CI.onCreatePage handler idempotent and write to namespaced context keys, such as context.myPlugin.foo, to reduce silent field overrides.gatsby-config.js. Its onCreatePage should assert required invariants and throw or log if expected fields are missing or unexpectedly overridden.npx gatsby build twice from a clean checkout and diff the generated page manifests for paths, matchPath, and pageContext; fail the build on any difference.exports.onCreatePage = ({ page }) => {
for (const field of ['path', 'matchPath']) {
if (!(field in page)) {
throw new Error(`Seal plugin: page ${page.path} missing ${field}`);
}
}
// Assert namespaced context invariants here
};
Assumptions and uncertainty. This answer assumes Gatsby v2 through v5+. It does not cite a version-specific changelog; if you use a patched or forked Gatsby distribution, inspect its source directly to confirm invocation order.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.