-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
<Head><title> not set by the time history change event fires #6025
Comments
I also checked the value of |
I upgraded to Next.js 8 (stable) and using React 16.8. The issue is still present. |
I am afraid not. Nevertheless I am currently not tracking the progress of this project on a daily basis. |
Thanks, @HaNdTriX. Can you think of a way for me to hack/force the update before the event fires, until the bug itself is fixed in Next.js? |
yepp. Implement it yourself using react ( components/OnRouteChange.js import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import { withRouter } from 'next/router'
const OnRouteChange = ({ children, router, onComplete }) => {
useEffect(() => {
onComplete(router)
}, [router.pathname])
return children
}
OnRouteChange.propTypes = {
children: PropTypes.node,
router: PropTypes.object.isRequired,
onComplete: PropTypes.func.isRequired
}
export default withRouter(OnRouteChange) pages/_app.js (Usage example) import React from 'react'
import App, { Container } from 'next/app'
import OnRouteChange from '../components/OnRouteChange'
export default class MyApp extends App {
static async getInitialProps({ Component, router, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render () {
const { Component, pageProps } = this.props
return (
<OnRouteChange onComplete={(router) => {
console.log('Route has changed', router.pathname)
}}>
<Container>
<Component {...pageProps} />
</Container>
</OnRouteChange>
)
}
} Future (Standalone Hook)In the future we could probably implement this as a standalone hook. import { useRouter } from 'next/router'
function useRouteChange (callback) {
const router = useRouter()
useEffect(() => callback, [router.pathname])
}
export default useRouteChange |
Thanks @HaNdTriX for the code, but I'm afraid the problem still persists. e.g. even here:
I still get mismatches.
Seems |
We have the same issue however we fixed it using a combination of the above suggestions, setTimeout and using Google Tag Manager to map some data into Google Analytics. Wrapping the event in a setTimeout gives NextJS time to run the componentDidMount and setup the properly.
Based on the variable and event naming in my example, in tag manager, if you setup a new Tag on a custom event of 'VirtualPageView' with a 'Tag Type' of 'Google Analytics - Universal Analytics'. |
I think this might be solved once we change next/head to use the |
Accessing |
Similarly to @StarpTech I've worked around it by wrapping it in Will be watching this issue to remove the workaround once it's sorted. Cheers! |
@WillSelway I'm also using GTM to send pageviews to GA. Thanks for the setTimeout trick - that fixed it for me. For anyone else trying to send pageviews to GA through GTM, I just thought I'd add that with the setTimeout trick, you don't actually need to send the page path or the title, because at that point it has updated in the browser, so the GA tag will pick it up like normal. |
Thank @WillSelway and @jackocnr for greate comment. gtag.js export const GA_TRACKING_ID = '<YOUR_GA_TRACKING_ID>'
export const pageview = url => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url,
})
} pages/_app.js import { useRouter } from "next/router";
import * as gtag from 'src/lib/gtag'
// For the purpose of using function component hooks
function MyComponent({ children }) {
const router = useRouter()
React.useEffect(() => {
setTimeout(() => {
if (!window) {
return
}
gtag.pageview(router.asPath)
}, 0)
}, [router.asPath])
return <>{children}</> // The fragment is just illustrational
}
class CustomApp extends App {
render() {
const { Component, pageProps } = this.props
return (
<MyComponent>
{Component ? <Component {...pageProps} /> : <div />}
</MyComponent>
)
}
}
export default CustomApp |
This adds a test case for the `document.title` not being updated by the time `routeChangeComplete` is fired. This should have been made consistent with vercel#13287 so should be able to be closed now Closes: vercel#6025
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
I'm using Google Tag Manager with a custom trigger for history change (inspired by SO).
The problem is that the content of the <title> tag as reported in GTM at the time the history change event fires is inconsistent: it may be set to the correct value, to the previous page's title, or it may even be "not set" entirely.
I set the title with the typical JSX:
import Head from "next/head";
/* ... */
<Head><title>stuff</title></Head>
Issue #4044 that was reported for Next.js v5.0.0. may be related.
Expected behavior
<title>
should have the right value by the time the history change event fires.System information
Is there a way to solve this?
Alternatively, is there a way to force server rendering every time, so as to bypass all client-side routing / dynamic rendering entirely?
The text was updated successfully, but these errors were encountered: