I've been struggling to fix an error in the Next.js 13 App Router.
Warning: Prop `className` did not match.
Server: "styled__HeadLink-sc-6589f63-0 fIOFTm"
Client: "styled__HeadLink-sc-b76127ed-0 jzAIeh"
I've spent a lot of time searching online for solutions to this issue. Errors can be quite challenging to pinpoint and resolve.
- To address this problem, I followed advice from various online sources.
Here is my .babelrc configuration:
{
"env": {
"development": {
"presets": ["next/babel"],
"plugins": [["babel-plugin-styled-components", { "ssr": true, "displayName": true, "preprocess": false }]]
},
"production": {
"plugins": [["babel-plugin-styled-components", { "ssr": true, "displayName": true, "preprocess": false }]],
"presets": ["next/babel"]
},
"test": {
"presets": ["next/babel"]
}
},
"plugins": [["babel-plugin-styled-components", { "ssr": true, "displayName": true, "preprocess": false }]]
}
And here is my next.config.js setup:
reactStrictMode: false,
compiler: {
styledComponents: true,
},
In my code, I used window checks like this:
export const isClient = () => typeof window !== 'undefined';
I also attempted to use next/dynamic as shown below:
export default dynamic(() => Promise.resolve(VeryTroubleBlock), { ssr: false });
The root of the issue seems to lie in the initial rendering of Swiper on specific pages.
const isTablet = useAdaptiveSize(Breakpoints.tablet);
const isMobile = useAdaptiveSize(Breakpoints.mobile);
// Swiper component usage here...
export default dynamic(() => Promise.resolve(VeryTroubleBlock), { ssr: false });
My custom hook implementation looks like this:
import { isClient } from '@/utils/client-check';
import { useState, useLayoutEffect } from 'react';
import { useWindowSize } from 'usehooks-ts';
export function useAdaptiveSize(size: number): boolean {
// Hook logic here...
}
While my custom hook resolved the issue on one page, it persists on another where Swiper is being utilized.
To eliminate the "Warning: Prop className
did not match" error, replacing the custom hook execution with
should work. However, this may lead to a server-side console error due to innerWidth < Breakpoints.tablet
window is not defined
.
I welcome any feedback and assistance in resolving this issue.
Have a wonderful day!