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): Wrap Head with <Location> (#36160) #36165

Merged
merged 1 commit into from Jul 19, 2022
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
@@ -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