Fixing Missing SEO Meta Tags in Next.js SSR: Debugging AuthProvider and NextSeo


Although our Next.js project used server-side rendering (SSR), SEO meta tags were not appearing correctly for search engines. SEO was broadly broken on key pages, including product details and the Expert home page. Investigating revealed several intertwined layers: the combination of AuthProvider and NextSeo, useMediaQuery compatibility with SSR, and the SEO component’s useState/useEffect implementation.

This post walks through the approaches I tried and the eventual solution, step by step. I hope it helps developers implementing both SSR and SEO in Next.js.

TL;DR

The problem

  • SEO across the marketplace, including product details and the Expert home page, was broken.
  • Server-side rendering did not work under AuthProvider; components ran only on the client. We use an SEO library called NextSeo. However, the NextSeo component did not render under AuthProvider.

Attempted solutions


Issue 1. AuthStore and GNB Rendering

  1. Return children under authStore, rendering only the GNB after initialization.
	return !isInitializing ? (
		<ReactChannelIO
			pluginKey={CHANNEL_ID_PLUGIN_KEY}
			language="ko"
			autoBoot={CHANNEL_USE_AUTO_BOOT}
			{...(isLoggedIn &&
				user?.id && {
					memberId: `${user.id}`,
					profile: { name: user.displayName },
				})}>
			{children}
		</ReactChannelIO>
	) : (
		<>{children}</> //as-is null
	)
})
  • Problem: login information was checked on the client, causing a state-related reflow in the GNB.
  1. Render only the GNB after initialization.
	<WebViewWrapper>
			{!isInitializing && isUseFooter && renderGNB}
			<Content>{children}</Content>
			{isUseFooter && <FooterView />}
		</WebViewWrapper>
  • Problem: reflow occurred in the GNB.
  1. Fix the size of the GNB area to improve reflow.
  • Problem: reflow improved, but the rendering delay relative to the content below remained..
    • Then wait for initialization for the whole component? The entire page looks like a cold start. And does using Next.js even make sense then..?
  1. Let’s fetch user information on the server.
MyApp.getInitialProps = async (appContext) => {  
	// calls page's `getInitialProps` and fills `appProps.pageProps`
	const appProps = await App.getInitialProps(appContext)
	const req = appContext.ctx.req
	let auth = req?.auth
	let { cookie } = appContext.ctx.req.headers
	let accessToken = cookie.split(';').find((c) => c.trim().startsWith('accessToken'))
	if (accessToken) {
		accessToken = accessToken.split('=')[1]
		try {
			let response = await getMe(accessToken)
			auth = { ...auth, ...response }
		} catch (e) {}
	}

	return { ...appProps, auth: auth }
}

Fixed..!

Or So I Thought

  1. Change the GNB to render after mounting.
const mounted = useMounted()
	return (
		<WebViewWrapper>
			{mounted && isUseFooter && renderGNB}
  • Problem: rerendering caused a slight flicker.

    • Confirmed that _app.tsx rendered twice.
    • Removed an unused useState → fixed the rerender. The flicker remained.
    • The layout component called useAuth() → removed it because the information was already supplied through server-side props → flicker resolved.
  1. Fix the height to minimize reflow.

    	const renderGNB = useMemo(() => {
    		if (!mounted) return <SkeletonGNB/>
    		if (goodNotesPage) return <GoodnotesGNB />
    		if (expertPage) return <ExpertGNB />
    		return <MarketGNB />
    	}, [mounted, pathname])

CSR version

  1. Conclusion)
    • Create a <SkeletonGNB/> to display during the initial server render and minimize reflow.

Issue 2. The SEO Component

  • Confirmed SSR was working through the Expert component.

Fixed..!

Or So I Thought

  • Problem: the meta tags were still missing.

  • Confirmed the meta tags appeared when using next-seo directly instead of the shared SEO component.

  • Found that the SEO component transformed metadata received as props through useState and useEffect. On the server, nextSeoOptions was an empty object.

const SEO = (props: SEOProps) => {
	console.log('SEO props',props)
	const [nextSeoOptions, setNextSeoOptions] = useState<NextSeoProps>({})

	useEffect(() => {
	// ... omitted
		setNextSeoOptions(transformedProps)
	}, [props])
	console.log('nextSeoOptions', nextSeoOptions)
  • Changed from transforming props inside useEffect to assigning variables directly. Since server-side rendering generates HTML for every request, separate memoization or optimization is unnecessary here. Since changes to props already trigger a rerender, I concluded there was no need to force the use of useState.
const SEO = (props: SEOProps) => {
	const openGraph = _.pickBy(
		{
			url: props.ogUrl,
			title: props.ogTitle,
			description: props.ogDescription,
			images: _.identity(props.ogImage) ? [{ url: props.ogImage }] : undefined,
		},
		_.identity,
	)
	const transformedProps = _.pickBy(
		{
			title: props.title,
			description: props.description,

			additionalMetaTags: _.identity(props.keywords)
				? [
						{
							name: 'keywords',
							content: props.keywords,
						},
				  ]
				: undefined,
			openGraph: _.isEmpty(openGraph) ? undefined : openGraph,
		},
		_.identity,
	)
	return <NextSeo {...transformedProps} />

Actually Fixed!


Remaining Improvements

  • Improve the design system’s useMediaQuery to work with SSR.
  • Could moving the API for the search input placeholder to server-side props improve the initial rendering experience further?

References