Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0] Refactor and document APIs #1053

Merged
merged 23 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b46a59b
Add basic version of new documentation.js transformer
KyleAMathews May 28, 2017
ab7ba98
Change api 'addFieldToNode' to 'createNodeField'
KyleAMathews May 29, 2017
45f7a31
Fix up node creation and add some simple tests
KyleAMathews May 30, 2017
1067771
Get basic docs page for action creators up
KyleAMathews May 30, 2017
0e39282
change deletePageByPath to just deletePage also sort action creators …
KyleAMathews May 30, 2017
d27833d
Change 'upsertPage' to 'createPage' and document it
KyleAMathews May 30, 2017
8a7575d
Only hide parent object description if its a desctructured object + i…
KyleAMathews May 30, 2017
61b79ff
Update action type
KyleAMathews May 30, 2017
776f77d
Support third level of parameters
KyleAMathews May 30, 2017
8863d25
Document createNode
KyleAMathews May 30, 2017
3697716
change 'addNodeToParent' to 'createParentChildLink' and document
KyleAMathews May 30, 2017
9e1c25a
Change 'addPageDependency' to 'createPageDependency' and document
KyleAMathews May 30, 2017
a8108ab
rename internal APIs
KyleAMathews May 30, 2017
5b0528b
Add links from function names at top to reference
KyleAMathews May 30, 2017
cba9b46
Ignore yarn.lock
KyleAMathews May 30, 2017
3ad7e09
Don't change the changelog...
KyleAMathews May 30, 2017
3c77aa3
Document and slightly modify node APIs
KyleAMathews May 31, 2017
6acd904
Factor out a FunctionsList component for rendering function docs
KyleAMathews May 31, 2017
6d536b3
Add browser/ssr docs and make a few tweaks to APIs
KyleAMathews May 31, 2017
62461fe
Add API specification document
KyleAMathews May 31, 2017
b5d634c
Actually add docs for node/browser/ssr
KyleAMathews May 31, 2017
e6600ab
Tweaks
KyleAMathews May 31, 2017
385bc78
Add README for gatsby-transformer-documentationjs
KyleAMathews May 31, 2017
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Change 'upsertPage' to 'createPage' and document it
  • Loading branch information
KyleAMathews committed May 30, 2017
commit d27833d5c9a086120ee655e825910eb4a30d78e0
6 changes: 3 additions & 3 deletions docs/blog/gatsbygram-case-study/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const slash = require(`slash`)
// access to any information necessary to programatically
// create pages.
exports.createPages = ({ graphql, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
const { createPage } = boundActionCreators

return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
Expand Down Expand Up @@ -214,9 +214,9 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
// each page's path.
_.each(result.data.allPostsJson.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "upsertPage"
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
upsertPage({
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
Expand Down
16 changes: 8 additions & 8 deletions docs/docs/creating-and-modifying-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ To do this, in your site's `gatsby-node.js` add code
similar to the following:

```javascript
// Implement the Gatsby API “onUpsertPage”. This is
// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onUpsertPage = ({ page, boundActionCreators }) => {
const { upsertPage, deletePage } = boundActionCreators
exports.onCreatePage = ({ page, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators

return new Promise((resolve, reject) => {
// Remove trailing slash
Expand All @@ -41,7 +41,7 @@ exports.onUpsertPage = ({ page, boundActionCreators }) => {
deletePage({ path: oldPath })

// Add the new page
upsertPage(page)
createPage(page)
}

resolve()
Expand All @@ -57,10 +57,10 @@ your app that lives under `/app/*`, you want to add code to your `gatsby-node.js
like the following:

```javascript
// Implement the Gatsby API “onUpsertPage”. This is
// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onUpsertPage = async ({ page, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
exports.onCreatePage = async ({ page, boundActionCreators }) => {
const { createPage } = boundActionCreators

return new Promise((resolve, reject) => {
// page.matchPath is a special key that's used for matching pages
Expand All @@ -69,7 +69,7 @@ exports.onUpsertPage = async ({ page, boundActionCreators }) => {
page.matchPath = "/app/:path"

// Update the page.
upsertPage(page)
createPage(page)
}

resolve()
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/migrating-from-v0-to-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Now we can create pages for each markdown file using our slug. In the same

```javascript
exports.createPages = ({ graphql, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
const { createPage } = boundActionCreators

return new Promise((resolve, reject) => {
const pages = []
Expand Down Expand Up @@ -187,7 +187,7 @@ exports.createPages = ({ graphql, boundActionCreators }) => {

// Create blog posts pages.
result.data.allMarkdownRemark.edges.forEach(edge => {
upsertPage({
createPage({
path: edge.node.fields.slug, // required
component: blogPost,
context: {
Expand Down
8 changes: 4 additions & 4 deletions examples/client-only-paths/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const Promise = require(`bluebird`)
const path = require(`path`)
const slash = require(`slash`)

// Implement the Gatsby API “onUpsertPage”. This is
// Implement the Gatsby API “onCreatePage”. This is
// called after every page is created.
exports.onUpsertPage = ({ page, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
exports.onCreatePage = ({ page, boundActionCreators }) => {
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
// Make the front page match everything client side.
// Normally your paths should be a bit more judicious.
if (page.path === `/`) {
page.matchPath = `/:path`
upsertPage(page)
createPage(page)
}
resolve()
})
Expand Down
6 changes: 3 additions & 3 deletions examples/gatsbygram/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const slash = require(`slash`)
// access to any information necessary to programatically
// create pages.
exports.createPages = ({ graphql, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
const { createPage } = boundActionCreators

return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
Expand Down Expand Up @@ -48,9 +48,9 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
// each page's path.
_.each(result.data.allPostsJson.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "upsertPage"
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
upsertPage({
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
Expand Down
6 changes: 3 additions & 3 deletions examples/hn/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const slash = require(`slash`)
// access to any information necessary to programatically
// create pages.
exports.createPages = ({ graphql, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
// queries against local Hacker News graphql schema. Think of
Expand Down Expand Up @@ -41,9 +41,9 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
// story page. We'll just use the HN story ID for the slug.
_.each(result.data.allHnStory.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "upsertPage"
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
upsertPage({
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
Expand Down
6 changes: 3 additions & 3 deletions examples/no-trailing-slashes/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Promise = require(`bluebird`)

exports.onUpsertPage = ({ page, boundActionCreators }) => {
const { upsertPage, deletePage } = boundActionCreators
exports.onCreatePage = ({ page, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators

return new Promise((resolve, reject) => {
// Remove trailing slash
Expand All @@ -12,7 +12,7 @@ exports.onUpsertPage = ({ page, boundActionCreators }) => {
deletePage({ path: oldPath })

// Add the new page
upsertPage(page)
createPage(page)
}

resolve()
Expand Down
6 changes: 3 additions & 3 deletions examples/using-drupal/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const slash = require(`slash`)
// access to any information necessary to programatically
// create pages.
exports.createPages = ({ graphql, boundActionCreators }) => {
const { upsertPage } = boundActionCreators
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
// The “graphql” function allows us to run arbitrary
// queries against the local Drupal graphql schema. Think of
Expand Down Expand Up @@ -38,9 +38,9 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
// article node. We'll just use the Drupal NID for the slug.
_.each(result.data.allDrupalNodeArticle.edges, edge => {
// Gatsby uses Redux to manage its internal state.
// Plugins and sites can use functions like "upsertPage"
// Plugins and sites can use functions like "createPage"
// to interact with Gatsby.
upsertPage({
createPage({
// Each page is required to have a `path` as well
// as a template component. The `context` is
// optional but is often necessary so the template
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const _ = require(`lodash`)

exports.createPages = ({ boundActionCreators }) => {
if (process.env.NODE_ENV === `production`) {
const { upsertPage } = boundActionCreators
upsertPage({
const { createPage } = boundActionCreators
createPage({
path: `/offline-plugin-app-shell-fallback/`,
component: slash(path.resolve(`${__dirname}/app-shell.js`)),
})
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ data
if (!exists404html) {
store.getState().pages.forEach(page => {
if (page.path === `/404/`) {
boundActionCreators.upsertPage({
boundActionCreators.createPage({
...page,
path: `/404.html`,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const createPath = require(`./create-path`)
// underscored. Then create url w/ our path algorithm *unless* user
// takes control of that page component in gatsby-node.
exports.createPagesStateful = async ({ store, boundActionCreators }) => {
const { upsertPage, deletePage } = boundActionCreators
const { createPage, deletePage } = boundActionCreators
const program = store.getState().program
const pagesDirectory = systemPath.posix.join(program.directory, `/src/pages`)
const exts = program.extensions.map(e => `${e.slice(1)}`).join(`,`)

// Get initial list of files.
let files = await glob(`${pagesDirectory}/**/?(${exts})`)
files.forEach(file => createPage(file, pagesDirectory, upsertPage))
files.forEach(file => _createPage(file, pagesDirectory, createPage))

// Listen for new component pages to be added or removed.
chokidar
.watch(`${pagesDirectory}/**/*.{${exts}}`)
.on(`add`, path => {
if (!_.includes(files, path)) {
createPage(path, pagesDirectory, upsertPage)
_createPage(path, pagesDirectory, createPage)
files.push(path)
}
})
Expand All @@ -40,7 +40,7 @@ exports.createPagesStateful = async ({ store, boundActionCreators }) => {
})
})
}
const createPage = (filePath, pagesDirectory, upsertPage) => {
const _createPage = (filePath, pagesDirectory, createPage) => {
// Filter out special components that shouldn't be made into
// pages.
if (!validatePath(systemPath.posix.relative(pagesDirectory, filePath))) {
Expand All @@ -54,7 +54,7 @@ const createPage = (filePath, pagesDirectory, upsertPage) => {
}

// Add page
upsertPage(page)
createPage(page)
}

const validatePath = path => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const fs = require(`fs-extra`)
exports.createPages = async ({ store, boundActionCreators }) => {
if (process.env.NODE_ENV !== `production`) {
const { program } = store.getState()
const { upsertPage } = boundActionCreators
const { createPage } = boundActionCreators
const currentPath = path.join(__dirname, `./raw_dev-404-page.js`)
const newPath = path.join(program.directory, `.cache`, `dev-404-page.js`)

fs.copySync(currentPath, newPath)

upsertPage({
createPage({
component: newPath,
path: `/dev-404-page/`,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ exports.sourceNodes = ({ boundActionCreators, store }) => {

const createPageId = path => `SitePage ${path}`

exports.onUpsertPage = ({ page, boundActionCreators }) => {
exports.onCreatePage = ({ page, boundActionCreators }) => {
const { createNode } = boundActionCreators

// Add page.
Expand Down
14 changes: 7 additions & 7 deletions packages/gatsby/src/redux/__tests__/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { actions } = require(`../actions`)

describe(`Add pages`, () => {
it(`allows you to add pages`, () => {
const action = actions.upsertPage({
const action = actions.createPage({
path: `/hi/`,
component: `/whatever/index.js`,
})
Expand All @@ -13,7 +13,7 @@ describe(`Add pages`, () => {
})

it(`allows you to add pages with context`, () => {
const action = actions.upsertPage({
const action = actions.createPage({
path: `/hi/`,
component: `/whatever/index.js`,
context: {
Expand All @@ -26,11 +26,11 @@ describe(`Add pages`, () => {
})

it(`allows you to add multiple pages`, () => {
const action = actions.upsertPage({
const action = actions.createPage({
path: `/hi/`,
component: `/whatever/index.js`,
})
const action2 = actions.upsertPage({
const action2 = actions.createPage({
path: `/hi/pizza/`,
component: `/whatever/index.js`,
})
Expand All @@ -41,13 +41,13 @@ describe(`Add pages`, () => {
})

it(`allows you to update existing pages (based on path)`, () => {
const action = actions.upsertPage({
const action = actions.createPage({
path: `/hi/`,
component: `/whatever/index.js`,
})

// Change the component
const action2 = actions.upsertPage({
const action2 = actions.createPage({
path: `/hi/`,
component: `/whatever2/index.js`,
})
Expand All @@ -59,7 +59,7 @@ describe(`Add pages`, () => {
})

it(`allows you to delete paths`, () => {
const action = actions.upsertPage({
const action = actions.createPage({
path: `/hi/`,
component: `/whatever/index.js`,
})
Expand Down
28 changes: 26 additions & 2 deletions packages/gatsby/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,27 @@ actions.deletePage = (page, plugin = ``) => {
}

const pascalCase = _.flow(_.camelCase, _.upperFirst)
// "createPage"
actions.upsertPage = (page, plugin = ``) => {
/**
* Create a page. See https://gatsbyjs.org/docs/creating-and-modifying-pages/
* for detailed documenation about creating pages.
* @param {object} page a page object
* @param {string} page.path Any valid URL. Must start with a forward slash
* @param {string} page.component The absolute path to the component for this page
* @param {object} page.context Context data for this page. Passed as props
* to the component `this.props.pathContext` as well as to the graphql query
* as graphql arguments.
* @example
* createPage({
* path: `/my-sweet-new-page/`,
* component: path.resolve('./src/templates/my-sweet-new-page.js`),
* // context gets passed in as props to the page as well
* // as into the page/template's GraphQL query.
* context: {
* id: `123456`,
* },
* })
*/
actions.createPage = (page, plugin = ``) => {
page.componentChunkName = layoutComponentChunkName(page.component)

let jsonName = `${_.kebabCase(page.path)}.json`
Expand All @@ -54,6 +73,11 @@ actions.upsertPage = (page, plugin = ``) => {
return
}

// If the path doesn't have an initial forward slash, add it.
if (page.path[0] !== `/`) {
page.path = `/` + page.path
}

return {
type: `UPSERT_PAGE`,
plugin,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/plugin-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ emitter.on(`CREATE_NODE`, action => {

emitter.on(`UPSERT_PAGE`, action => {
const page = action.payload
apiRunnerNode(`onUpsertPage`, { page }, action.plugin.name)
apiRunnerNode(`onCreatePage`, { page }, action.plugin.name)
})
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const runAPI = (plugin, api, args) => {

const gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
if (gatsbyNode[api]) {
// if (!_.includes([`onNodeCreate`, `onUpsertPage`], api)) {
// if (!_.includes([`onNodeCreate`, `onCreatePage`], api)) {
// console.log(`calling api handler in ${plugin.resolve} for api ${api}`)
// }
const result = gatsbyNode[api](
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = async (api, args = {}, pluginSource) =>
// Break infinite loops.
// Sometimes a plugin will implement an API and call an
// action which will trigger the same API being called.
// "onUpsertPage" is the only example right now.
// "onCreatePage" is the only example right now.
// In these cases, we should avoid calling the originating plugin
// again.
let noSourcePluginPlugins = filteredPlugins
Expand Down
Loading