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

gatsby-plugin-create-client-paths remounts the root on every page transition #10227

Closed
jaredpalmer opened this issue Nov 30, 2018 · 3 comments · Fixed by #10261 · 4 remaining pull requests
Closed

gatsby-plugin-create-client-paths remounts the root on every page transition #10227

jaredpalmer opened this issue Nov 30, 2018 · 3 comments · Fixed by #10261 · 4 remaining pull requests
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby

Comments

@jaredpalmer
Copy link

jaredpalmer commented Nov 30, 2018

Description

gatsby-plugin-create-client-paths is remounting the root component on every single page transition for the client-only path. This means that if you have auth check in your top-level root, your authentication check will run on each new page instead of just once like it would with a normal React SPA (e.g. one built with create-react-app)

Expected result

The root component should only mount once.

Actual result

The root (with routes) is remounted on every single transition.

@jaredpalmer
Copy link
Author

Cc @kkemple

@pieh
Copy link
Contributor

pieh commented Dec 2, 2018

Context:
This is happening because of key used in component just above user pages/templates -

key: this.props.location.pathname,

There is a way to override this in user code - adding something like this in gatsby-browser.js in root directory of project:

import React from "react"

export const replaceComponentRenderer = ({ props }) => (
  <props.pageResources.component {...props} />
)

Will do everything we do in core except for setting key. Alternatively you can add key={props.pageResources.page.path} there, if you would only want to not remount for same client-only path.

This key was initially added as (not ideal) solution for #8181 (problems that some plugins/libs that need to manipulate DOM directly and without remounts would cause weird problems), so we need to be careful how to deal with this. Potentially making it so just pages with client-only paths don't remount might be feasible, but would need some testing.

@pieh pieh added type: bug An issue or pull request relating to a bug in Gatsby status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. labels Dec 3, 2018
jaredpalmer added a commit to jaredpalmer/gatsby that referenced this issue Dec 3, 2018
Add docs about avoiding request waterfalls with client routes by overriding component renderer `key` prop (gatsbyjs#10227 (comment))

Closes gatsbyjs#10227
@jaredpalmer
Copy link
Author

This worked. Submitted #10255 which documents the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
2 participants