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

fix(gatsby): trigger location effects in navigation #27594

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ describe(`navigation`, () => {
})
})

describe(`All location changes should trigger an effect`, () => {
beforeEach(() => {
cy.visit(`/navigation-effects`).waitForRouteChange()
})

it(`should trigger an effect after a search param has changed`, () => {
cy.getTestElement(`effect-message`).invoke(`text`).should(`eq`, `Waiting for effect`)
cy.getTestElement(`send-search-message`).click().waitForRouteChange()
cy.getTestElement(`effect-message`).invoke(`text`).should(`eq`, `?message=searchParam`)
})

it(`should trigger an effect after the hash has changed`, () => {
cy.getTestElement(`effect-message`).invoke(`text`).should(`eq`, `Waiting for effect`)
cy.getTestElement(`send-hash-message`).click().waitForRouteChange()
cy.getTestElement(`effect-message`).invoke(`text`).should(`eq`, `#message-hash`)
})
})

describe(`Route lifecycle update order`, () => {
it(`calls onPreRouteUpdate, render and onRouteUpdate the correct amount of times on route change`, () => {
cy.lifecycleCallCount(`onPreRouteUpdate`).should(`eq`, 1)
Expand Down
40 changes: 40 additions & 0 deletions e2e-tests/development-runtime/src/pages/navigation-effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useEffect, useState } from "react"
import { navigate } from "gatsby"

import Layout from "../components/layout"

export default function NavigationEffects({ location }) {
const [message, setMessage] = useState("Waiting for effect")

const searchParam = location.search
const searchHash = location.hash

useEffect(() => {
setMessage(searchParam)
}, [searchParam])

useEffect(() => {
setMessage(searchHash)
}, [searchHash])

const handleClick = next => navigate(`${next}`, { replace: true })

return (
<Layout>
<h1 data-testid="effect-message">{message}</h1>

<button
data-testid="send-search-message"
onClick={() => handleClick("?message=searchParam")}
>
Send Search
</button>
<button
data-testid="send-hash-message"
onClick={() => handleClick("#message-hash")}
>
Send Hash
</button>
</Layout>
)
}
4 changes: 2 additions & 2 deletions packages/gatsby/cache-dir/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class RouteUpdates extends React.Component {
}

shouldComponentUpdate(prevProps) {
if (this.props.location.pathname !== prevProps.location.pathname) {
if (this.props.location.href !== prevProps.location.href) {
onPreRouteUpdate(this.props.location, prevProps.location)
return true
}
Expand All @@ -217,7 +217,7 @@ class RouteUpdates extends React.Component {
}

componentDidUpdate(prevProps) {
if (this.props.location.pathname !== prevProps.location.pathname) {
if (this.props.location.href !== prevProps.location.href) {
onRouteUpdate(this.props.location, prevProps.location)
}
}
Expand Down