fix: Use NextJS recommended config for styled-components
This commit is contained in:
@@ -3,17 +3,19 @@ const nextConfig = {
|
|||||||
images: {
|
images: {
|
||||||
remotePatterns: [
|
remotePatterns: [
|
||||||
{
|
{
|
||||||
protocol: 'https',
|
protocol: "https",
|
||||||
hostname: 'raw.githubusercontent.com',
|
hostname: "raw.githubusercontent.com",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
experimental: {
|
experimental: {
|
||||||
serverActions: {
|
serverActions: {
|
||||||
// edit: updated to new key. Was previously `allowedForwardedHosts`
|
// edit: updated to new key. Was previously `allowedForwardedHosts`
|
||||||
allowedOrigins: ['localhost:3000', 'get-zen.vercel.app'],
|
allowedOrigins: ["localhost:3000", "get-zen.vercel.app"],
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
compiler: {
|
||||||
|
styledComponents: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { Metadata } from "next";
|
import type { Metadata } from "next";
|
||||||
import { Inter } from "next/font/google";
|
import { Inter } from "next/font/google";
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
import { ThemeProvider } from "@/components/theme-provider"
|
import { ThemeProvider } from "@/components/theme-provider";
|
||||||
|
import StyledComponentsRegistry from "@/lib/styled-components-registry";
|
||||||
|
|
||||||
const inter = Inter({ subsets: ["latin"] });
|
const inter = Inter({ subsets: ["latin"] });
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ export default function RootLayout({
|
|||||||
enableSystem
|
enableSystem
|
||||||
disableTransitionOnChange
|
disableTransitionOnChange
|
||||||
>
|
>
|
||||||
{children}
|
<StyledComponentsRegistry>{children}</StyledComponentsRegistry>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
29
src/lib/styled-components-registry.tsx
Normal file
29
src/lib/styled-components-registry.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import React, { useState } from "react";
|
||||||
|
import { useServerInsertedHTML } from "next/navigation";
|
||||||
|
import { ServerStyleSheet, StyleSheetManager } from "styled-components";
|
||||||
|
|
||||||
|
export default function StyledComponentsRegistry({
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}) {
|
||||||
|
// Only create stylesheet once with lazy initial state
|
||||||
|
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
|
||||||
|
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());
|
||||||
|
|
||||||
|
useServerInsertedHTML(() => {
|
||||||
|
const styles = styledComponentsStyleSheet.getStyleElement();
|
||||||
|
styledComponentsStyleSheet.instance.clearTag();
|
||||||
|
return <>{styles}</>;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (typeof window !== "undefined") return <>{children}</>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
|
||||||
|
{children}
|
||||||
|
</StyleSheetManager>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user