Gatsby plugin that automatically creates pages from React components in specified directories. This plugin is included by default in all Gatsby sites for creating pages from src/pages, and supports the File System Route API for programmatic page creation from data sources using collection routes and client-only routes.
npm install gatsby-plugin-page-creatorThis plugin is imported and configured in your Gatsby configuration file:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: `${__dirname}/src/pages`,
},
},
],
}The plugin also provides GraphQL fields that can be used in components:
// In React components or page queries
export const query = graphql`
query {
allFile {
nodes {
id
gatsbyPath(filePath: "/blog/{File.name}")
}
}
}
`The plugin automatically converts React components in specified directories to pages:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: `${__dirname}/src/pages`,
ignore: [`*.test.js`, `private-*.js`],
},
},
],
}// src/pages/about.js - becomes /about/
import React from "react";
export default function About() {
return <h1>About Page</h1>;
}
// src/pages/blog/index.js - becomes /blog/
export default function Blog() {
return <h1>Blog Index</h1>;
}Gatsby Plugin Page Creator is built around several key components:
createPagesStatefully, onPluginInit, and setFieldsOnGraphQLNodeType{}, client-only routes with [])gatsbyPath field to GraphQL node types for programmatic path generationCore plugin configuration options for specifying directories, ignore patterns, and slugify options.
interface PluginOptions {
path: string;
pathCheck?: boolean;
ignore?: IPathIgnoreOptions | string | Array<string> | null;
slugify?: ISlugifyOptions;
}Advanced routing patterns including collection routes for data-driven pages and client-only routes for dynamic content.
// Collection route example: {Model.field} syntax
// File: src/pages/blog/{MarkdownRemark.frontmatter__slug}.js
// Creates: /blog/my-post/, /blog/another-post/, etc.
// Client-only route example: [param] syntax
// File: src/pages/app/[...].js
// Creates: /app/* with client-side routingMultiple plugin instances, programmatic page creation, and integration with Gatsby's createPages API.
// Multiple instances
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-page-creator`,
options: { path: `${__dirname}/src/pages` },
},
{
resolve: `gatsby-plugin-page-creator`,
options: { path: `${__dirname}/src/admin/pages` },
},
],
}interface IOptions extends PluginOptions {
path: string;
pathCheck?: boolean;
ignore?: IPathIgnoreOptions | string | Array<string> | null;
slugify?: ISlugifyOptions;
}
interface IPathIgnoreOptions {
patterns: string[];
options?: {
nocase?: boolean;
[key: string]: any;
};
}
interface ISlugifyOptions {
separator?: string;
lowercase?: boolean;
decamelize?: boolean;
customReplacements?: Array<[string, string]>;
preserveLeadingUnderscore?: boolean;
preserveTrailingDash?: boolean;
[key: string]: any;
}The plugin exports the following Gatsby Node API functions:
/**
* Main page creation function called during Gatsby's createPages lifecycle
* @param args - Standard Gatsby CreatePagesArgs with store, actions, reporter, graphql, emitter
* @param pluginOptions - Plugin configuration options
* @param doneCb - Callback function to signal completion
*/
function createPagesStatefully(
args: CreatePagesArgs & { traceId: "initial-createPages" },
pluginOptions: IOptions,
doneCb: PluginCallback
): Promise<void>;
/**
* Simple page creation function for synchronous page creation
* @param args - Standard Gatsby CreatePagesArgs
* @param pluginOptions - Plugin configuration options
*/
function createPages(args: CreatePagesArgs, pluginOptions: IOptions): void;
/**
* Plugin initialization hook to set up error mapping and collection detection
* @param args - Gatsby ParentSpanPluginArgs with reporter
* @param options - Plugin options containing path configuration
*/
function onPluginInit(
args: ParentSpanPluginArgs,
options: IOptions
): Promise<void>;
/**
* Adds gatsbyPath field to GraphQL node types for collection routes
* @param args - Gatsby SetFieldsOnGraphQLNodeTypeArgs
* @param options - Plugin options with slugify configuration
* @returns GraphQL field definitions object
*/
function setFieldsOnGraphQLNodeType(
args: SetFieldsOnGraphQLNodeTypeArgs,
options: PluginOptions & { slugify: ISlugifyOptions }
): Record<string, unknown>;interface CreatePagesArgs {
store: Store;
actions: Actions;
reporter: Reporter;
graphql: GraphQL;
emitter: EventEmitter;
}
interface ParentSpanPluginArgs {
reporter: Reporter;
[key: string]: any;
}
interface SetFieldsOnGraphQLNodeTypeArgs {
getNode: (id: string) => Node | undefined;
type: GraphQLObjectType;
store: Store;
reporter: Reporter;
}
type PluginCallback = (error?: Error | null, result?: any) => void;The plugin automatically adds the gatsbyPath field to GraphQL node types when collection routes are detected during onPluginInit:
type File implements Node {
# ... other fields
gatsbyPath(filePath: String!): String
}
type MarkdownRemark implements Node {
# ... other fields
gatsbyPath(filePath: String!): String
}The gatsbyPath field resolver implementation:
/**
* GraphQL field resolver for gatsbyPath
* @param source - GraphQL node being resolved
* @param args - Field arguments containing filePath
* @param context - GraphQL context with nodeModel
* @returns Generated URL path for the node
*/
interface GatsbyPathResolver {
gatsbyPath: {
type: GraphQLString;
args: {
filePath: {
type: GraphQLString;
};
};
resolve: (
source: Record<string, unknown>,
args: { filePath: string },
context: GraphQLResolveInfo
) => Promise<string>;
};
}Collection routes are detected during plugin initialization by scanning for files with {Model.field} syntax:
/**
* Collection detection process during onPluginInit
* Scans for files matching pattern **/**\{*\}**(/**)
*/
const pagesGlob = `**/**\\{*\\}**(/**)`;
/**
* Find all collection page files in the specified path
* @param pagesPath - Directory path to scan for collection routes
* @returns Array of relative file paths containing collection routes
*/
function findCollectionPageFiles(pagesPath: string): Promise<Array<string>>;