ui icon indicating copy to clipboard operation
ui copied to clipboard

TypeError in Next.js Layout "fontSans" is not a valid Layout export field.

Open zxcvbinz opened this issue 1 year ago • 3 comments
trafficstars

Issue Report: TypeError in Next.js Layout

Overview

This document outlines a type error encountered in a Next.js project, specifically with the layout file app/layout.tsx.

Description

The layout file app/layout.tsx does not match the required types for a Next.js layout component, resulting in a type error.

Steps to Reproduce

  1. Create or edit the layout component in app/layout.tsx.
  2. Integrate shadcn/ui guide font with a Next.js page.
  3. Build the project and observe the TypeScript error.

Expected Behavior

The layout component in app/layout.tsx should comply with the type requirements of Next.js layouts.

Error Message

CleanShot 2024-01-11 at 11  16 48@2x

Environment

  • Next.js version: 14.0.4
  • TypeScript version: 5
  • OS: Mac OS 14.2
  • Node.js version: 20.9.0

zxcvbinz avatar Jan 11 '24 10:01 zxcvbinz

Same here. The shadcn/ui docs: https://ui.shadcn.com/docs/installation/next#fonts

livingstonlarus avatar Jan 11 '24 12:01 livingstonlarus

In /app/layout.tsx, try removing export from const fontSans:

Before:

export const fontSans = FontSans({
  subsets: ["latin"],
  variable: "--font-sans",
})

After:

const fontSans = FontSans({
  subsets: ['latin'],
  variable: "--font-sans",
})

Build no longer breaks (on Vercel).

I was looking at:

https://nextjs.org/docs/pages/building-your-application/optimizing/fonts

and noticed there's no export.

Maybe it's a bad copy-paste in:

https://ui.shadcn.com/docs/installation/next#fonts

coming from:

https://github.com/shadcn-ui/ui/blob/main/apps/www/lib/fonts.ts

where /lib/fonts exports to /app/layout.tsx:

https://github.com/shadcn-ui/ui/blob/main/apps/www/app/layout.tsx)

livingstonlarus avatar Jan 12 '24 00:01 livingstonlarus

If you want to use the font properties somehow, you can create font.tsx file, then move the font snippet code and export it:

font.tsx

import { Inter as FontSans } from "next/font/google";
// Will break at build. Waiting for https://github.com/shadcn-ui/ui/issues/2377
export const fontSans = FontSans({
  subsets: ["latin"],
  variable: "--font-sans",
});

layout.tsx


import type { Metadata } from "next";
import "./globals.css";
import { cn } from "@/lib/utils";
import { ThemeProvider } from "@/components/theme-provider";
import { fontSans } from "./font";
export const metadata: Metadata = {
  title: "My app",
  description: "Testing Next 14 + shadcn/ui",
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body
        className={cn(
          "min-h-screen bg-background font-sans antialiased",
          fontSans.variable
        )}
      >
        <ThemeProvider
          attribute="class"
          defaultTheme="system"
          enableSystem
          disableTransitionOnChange
        >
          {children}
        </ThemeProvider>
      </body>
    </html>
  );
}

build

image

alamenai avatar Jan 14 '24 01:01 alamenai

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Feb 26 '24 23:02 shadcn