Skip to content

Commit

Permalink
fix(gatsby): Wrap Head with <Location> (#36160) (#36165)
Browse files Browse the repository at this point in the history
Co-authored-by: Jude Agboola <marvinjudehk@gmail.com>
  • Loading branch information
ViCo0TeCH and marvinjude committed Jul 19, 2022
1 parent 6f0d46b commit 19c9c0b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
@@ -0,0 +1,13 @@
import headFunctionExportSharedData from "../../../shared-data/head-function-export.js"

it(`Page with Head Export that uses useLocation works`, () => {
cy.visit(headFunctionExportSharedData.page.pageWithUseLocation).waitForRouteChange()

cy.getTestElement(`location-pathname-in-template`)
.invoke(`text`)
.then(text => {
cy.getTestElement(`location-pathname-in-head`)
.invoke(`attr`, `content`)
.should('equal', text)
})
})
Expand Up @@ -12,6 +12,7 @@ const page = {
invalidElements: `${path}/invalid-elements/`,
fsRouteApi: `${path}/fs-route-api/`,
deduplication: `${path}/deduplication/`,
pageWithUseLocation: `${path}/page-with-uselocation/`,
}

const data = {
Expand Down
@@ -0,0 +1,19 @@
import * as React from "react"
import { useLocation } from '@gatsbyjs/reach-router';

export default function HeadFunctionExportWithUseLocation() {
const location = useLocation();

return (
<>
<h1>I test that Head export with useLocation hook works</h1>
<p data-testid="location-pathname-in-template">{location.pathname}</p>
</>
)
}

export function Head() {
const location = useLocation();

return <meta data-testid="location-pathname-in-head" name="location" content={location.pathname}/>
}
@@ -1,6 +1,7 @@
import React from "react"
import { useEffect } from "react"
import { StaticQueryContext } from "gatsby"
import { LocationProvider } from "@gatsbyjs/reach-router"
import { reactDOMUtils } from "../react-dom-utils"
import { FireCallbackInEffect } from "./components/fire-callback-in-effect"
import { VALID_NODE_NAMES } from "./constants"
Expand Down Expand Up @@ -81,7 +82,9 @@ export function headHandlerForBrowser({
// In Prod we only call onHeadRendered in FireCallbackInEffect to render to head
<FireCallbackInEffect callback={onHeadRendered}>
<StaticQueryContext.Provider value={staticQueryResults}>
<Head {...filterHeadProps(pageComponentProps)} />
<LocationProvider>
<Head {...filterHeadProps(pageComponentProps)} />
</LocationProvider>
</StaticQueryContext.Provider>
</FireCallbackInEffect>,
hiddenRoot
Expand Down

0 comments on commit 19c9c0b

Please sign in to comment.