Using route-based code splitting causes css duplicate in chunks.
Steps to reproduce:
- Create simple app with create-react-app
- Use react-router-dom for client side routing
- Add one common component with local css.
- Add 2 pages, using the common component with style overwriting
- Run build command
- Check static folder
// /components/CommonPageHeader/CommonPageHeader.module.scss
.header {
color: black;
}
// /components/CommonPageHeader/CommonPageHeader.tsx
import styles from "./CommonPageHeader.module.scss";
import { ReactNode } from "react";
export const CommonPageHeader = (props: {
className: string;
children: ReactNode;`your text`
}) => {
const { className = "", children } = props;
return <h1 className={`${styles.header} ${className}`}>{children}</h1>;
};
// /pages/Home/Home.module.scss
.greenHeader {
color: green;
}
// /pages/Home/Home.tsx
import React from "react";
import { Link } from "react-router-dom";
import { CommonPageHeader } from "../../components/CommonPageHeader/CommonPageHeader";
import styles from "./Home.module.scss";
const Home = () => (
<>
<CommonPageHeader className={styles.greenHeader}>
I'm a green header
</CommonPageHeader>
<Link to={"some-page"}>Some page</Link>
</>
);
export default Home;
// /pages/SomePage/SomePage.module.scss
.redHeader {
color: red;
}
// /pages/SomePage/SomePage.tsx
import React from "react";
import { Link } from "react-router-dom";
import { CommonPageHeader } from "../../components/CommonPageHeader/CommonPageHeader";
import styles from "./SomePage.module.scss";
const SomePage = () => {
return (
<>
<CommonPageHeader className={styles.redHeader}>
I'm a red header
</CommonPageHeader>
<Link to={"/"}>Home</Link>
</>
);
};
export default SomePage;
// index.tsx
import React, { Suspense } from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const Home = React.lazy(() => import("./pages/Home/Home"));
const SomePage = React.lazy(() => import("./pages/SomePage/SomePage"));
const router = createBrowserRouter([
{
path: "/",
element: (
<Suspense>
<Home />
</Suspense>
),
},
{
path: "/some-page",
element: (
<Suspense>
<SomePage />
</Suspense>
),
},
]);
const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
You get css chunks with duplicates and unexpected styles overwrites in production:
// /build/static/css/499.408c3ff0.chunk.css
.CommonPageHeader_header__eTauC {
color: #000
}
.Home_greenHeader__YwF4H {
color: green
}
// /build/static/css/136.855973ab.chunk.css
.CommonPageHeader_header__eTauC {
color: #000
}
.SomePage_redHeader__a-I8c {
color: red
}
- Click to some page link
- Go back to home page
Is there way to prevent it?
CommonPageHeaderhigher up in the React tree so it's included with a "core" app chunk, if that makes sense. I don't believereact-routerhas much of anything to do with this so I've retagged with hopefully more accurate tags for someone with more experience to see and provide an answer.useRouteshook and pass the routes config to that and render within a regularBrowserRoutercomponent? Or manually render the routes via theRoutesandRoutecomponents?