最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

next.js - How do I include CSS file from public folder in Next JS 15 project? - Stack Overflow

programmeradmin0浏览0评论

I am using Next JS 15.2.3, and I am trying to include a CSS file from the public folder into a layout.tsx file.

According to this similar question, I tried to include the CSS file like this in the layout.tsx file, but it didn't work.

import React from 'react';
import Head from 'next/head'
...
<Head>
    <link rel="stylesheet" href="/assets/css/styles.css" />
</Head>
...

I found from the documentation (link here) that the Head element is not valid anymore because of the introduction of the app router, since when the metadata included in the head can be provided in the following format and there is no way to provide a link tag in the head of the document:

import type { Metadata } from 'next'
 
export const metadata: Metadata = {
  title: "My Page Title",
}
 
export default function Page() {
  return "..."
}

I also tried to include the path directly to the file directly in the layout.tsx file, like so

import "/assets/css/styles.css";

But it shows this error:

Module not found: Can't resolve "/assets/css/styles.css"

Is there a way to include CSS from a public folder without moving it to the app directory?

I am using Next JS 15.2.3, and I am trying to include a CSS file from the public folder into a layout.tsx file.

According to this similar question, I tried to include the CSS file like this in the layout.tsx file, but it didn't work.

import React from 'react';
import Head from 'next/head'
...
<Head>
    <link rel="stylesheet" href="/assets/css/styles.css" />
</Head>
...

I found from the documentation (link here) that the Head element is not valid anymore because of the introduction of the app router, since when the metadata included in the head can be provided in the following format and there is no way to provide a link tag in the head of the document:

import type { Metadata } from 'next'
 
export const metadata: Metadata = {
  title: "My Page Title",
}
 
export default function Page() {
  return "..."
}

I also tried to include the path directly to the file directly in the layout.tsx file, like so

import "/assets/css/styles.css";

But it shows this error:

Module not found: Can't resolve "/assets/css/styles.css"

Is there a way to include CSS from a public folder without moving it to the app directory?

Share Improve this question edited yesterday Harish asked yesterday HarishHarish 5282 gold badges11 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Just import the CSS with the following path in layout.tsx:

import "public/assets/css/styles.css";

I had missed the public keyword.

发布评论

评论列表(0)

  1. 暂无评论