https://github.com/takahiro-okada/nextjs-learn-nextjs
/app
ディレクトリ
/app/lib
ディレクトリ
/app/ui
ディレクトリ
public/
ディレクトリ
next.config.js
ファイル
プロジェクトの設定を管理
create-next-app
を使用して新しいプロジェクトを開始すると自動生成される
基本的には変更不要
const nextConfig: NextConfig = {
images: {
domains: ['hoge.com'],
},
};
export default nextConfig;
.module.css
を付けることで有効化なぜフォントの最適化が必要なのか
next/font
を使うことで
next/image
も同様<Image>
コンポーネントを使うと
lazy loading
を付与WebP
や AVIF
を使う<Image>タグを使った時の生成される DOM
<img alt="Screenshots of the dashboard project showing desktop version" loading="lazy" width="1000" height="760" decoding="async" data-nimg="1" class="hiddne md:block" srcset="/_next/image?url=%2Fhero-desktop.png&w=1080&q=75 1x, /_next/image?url=%2Fhero-desktop.png&w=2048&q=75 2x" src="/_next/image?url=%2Fhero-desktop.png&w=2048&q=75" style="color: transparent;">
webpになっていないと思ったけどnextworkで見たら配信は正しくwebpになっていた
このあたりの仕組みよくわかっていないマンです
imgタグと<image>コンポーネントの話
page.tsx
とlayout.tsx
の考え方がある
page.tsx
はページを作るファイル
app/page.tsx
/
app/dashboard/page.tsx
/dashboard/
layout.tsx
はレイアウトを管理するページ
共通のレイアウトを持たせたい時に使う
HeaderとFooterがサイト内で共通なのであれば次のように書ける
/app/layout.tsx
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className={`${inter.className} antialiased`}>
<Header />
{children}
<Footer />
</body>
</html>
);
}
next/link
の使い方
<Link>
コンポーネントを使うと遷移先のページコードをバックグランドでプリフェッチするのでパフォーマンスが向上する
一方で不要なデータ通信が入るので対象が多いとoffにすることもあるらしい
<Link href="/dashboard" prefetch={false}>
Dashboard
</Link>
usePathname()
のhookについて
現在のURLを取得できるhookになる
http://localhost:3000/dashboard/invoices
/dashboard/invoices
/https://nextjs.org/docs/app/api-reference/functions/use-pathname
.env
にコピーする