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

Add browser API to enable page transitions plugins #1415

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions packages/gatsby/src/cache-dir/component-renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createElement } from "react"
import loader from "./loader"
import emitter from "./emitter"
import apiRunner from "./api-runner-browser"

// Pass pathname in as prop.
// component will try fetching resources. If they exist,
Expand All @@ -12,6 +13,7 @@ class ComponentRenderer extends React.Component {
location: props.location,
pageResources: loader.getResourcesForPathname(props.location.pathname),
}
this.previousPageResources = {}
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -55,22 +57,40 @@ class ComponentRenderer extends React.Component {
// Check if the component or json have changed
shouldComponentUpdate(nextProps, nextState) {
if (
this.state.pageResources.component !== nextState.pageResources.component
this.state.pageResources.component !== nextState.pageResources.component ||
this.state.pageResources.json !== nextState.pageResources.json
) {
return true
}
if (this.state.pageResources.json !== nextState.pageResources.json) {
this.previousProps = this.props
this.previousPageResources = this.state.pageResources
return true
}
return false
}

render() {
if (this.state.pageResources) {
return createElement(this.state.pageResources.component, {
...this.props,
...this.state.pageResources.json,
const pluginResponses = apiRunner('componentRenderer', {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's name this API replacePageComponentRenderer per https://www.gatsbyjs.org/docs/api-specification/#operators

previousPage: {
...this.props,
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be this.previousProps

component: this.previousPageResources.component,
json: this.previousPageResources.json,
},
currentPage: {
...this.props,
component: this.state.pageResources.component,
json: this.state.pageResources.json,
},
})

const resolvedComponent =
pluginResponses.length ?
pluginResponses[pluginResponses.length - 1] :
createElement(this.state.pageResources.component, {
...this.props,
...this.state.pageResources.json,
})

return resolvedComponent
} else {
return null
}
Expand Down