Closed6

【key-front】Next.js App Routerの素振り(環境構築・デプロイ・テスト)

1zushun1zushun

モチベーション

  • 毎週木曜日Slackのkey_frontチャンネルでハドル機能を使いお題に対してメンバー同士ディスカッションをする時間を15〜30分程度設けている
  • 今回はNext.js App Routerについて取り上げる
  • ファシリテーターは筆者なので、事前に読み込んで気になった点などをスクラップに投げていく
  • 開催日は○○/○○(木)で最終的に議事録として結論をまとめる

過去に開催した関連してそうな内容

https://zenn.dev/shuuuuuun/scraps/7b2aaf746aec91

https://zenn.dev/shuuuuuun/scraps/9cd8d4c0a7be87

1zushun1zushun

ディレクトリ構成を整える

チームで検討、必要なければ個人の好みで整える。

https://zenn.dev/link/comments/d5f91937c516d5

tsconfig.jsonの設定

tsconfig.jsonはstrictestを導入して硬めに行く。ついでにエイリアスも設定しておく。

https://zenn.dev/shuuuuuun/scraps/48ac73aeb3076c

リンター周りの設定

最近はeslint-config-next(Next.jsのルールセット)でも十分だと思ってきているのでESLintの設定はデフォルトのままにする。Stylelint・Prettierは一旦流す。コミット品質担保(Husky + Lint-staged)は設定しておく。

https://zenn.dev/shuuuuuun/articles/127728961f89a0

CSS周りの設定をする

CSS Modulesで進める。必要であればSassを入れる。Tailwind CSSをいれないのでリセットCSSの用意も別途する(制作チームのおすすめは以下に添付)

https://zenn.dev/shuuuuuun/scraps/744aa994686183

https://github.com/elad2412/the-new-css-reset/blob/main/css/reset.css

VSCodeの設定をしておく

以下はextenstions.jsonの記事になるが、settings.jsonも設定する。

https://zenn.dev/shuuuuuun/scraps/bcc59eb2f7d640

settings.jsonがなんか変わっていたのでメモ

https://zenn.dev/braveryk7/articles/source-fixall-eslint-value

Storybookの準備をする

エイリアスの追加を設定しておく

import type { StorybookConfig } from "@storybook/nextjs";
const path = require("path");

const config: StorybookConfig = {
  stories: ["../app/**/*.mdx", "../app/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/addon-onboarding",
    "@storybook/addon-interactions",
  ],
  // @see https://github.com/storybookjs/storybook/issues/3916#issuecomment-871283551
  webpackFinal(config: any) {
    config.resolve.alias = {
      ...config.resolve.alias,
      "@": path.resolve(__dirname, "../app"),
    };
    return config;
  },
  framework: {
    name: "@storybook/nextjs",
    options: {},
  },
  docs: {
    autodocs: "tag",
  },
};
export default config;


bundle-analyzerを導入する

ドキュメントに案内があった、こっちの案内に従ってみる

https://nextjs.org/docs/app/building-your-application/optimizing/bundle-analyzer


全部実装して最後にコミットできればok

1zushun1zushun

Vercel

ホスティングはVercelを選択した。

本番環境が遅い

  • 開発環境ではprefetchがなし(以下参照・実際に確認済み)である程度早く処理されていたのに逆にprefetchされる本番環境では異様に遅い

Prefetching happens when a <Link /> component enters the user's viewport (initially or through scroll). Next.js prefetches and loads the linked route (denoted by the href) and its data in the background to improve the performance of client-side navigations. Prefetching is only enabled in production.

https://nextjs.org/docs/app/api-reference/components/link#prefetch

  • ネットワークタブを見るとcontent downloadにかなり持って行かれている
  • bundle-analyzerで確認してみたがバンドルサイズに問題はなさそう
  • 開発環境でnpm run build && npm run startを実行しprefetchできる環境で試してみた。これは期待値の速さになっていた

→ 恐らくVercel周りの設定で何かやらかしているはず

  • 上記の理由からVercel周りで調査を進めて、以下の記事を見つけた。本番環境が異様に遅い原因はVercelのリージョンがワシントンDCになっていたからだった。東京リージョンに修正して完了。
  • リージョンが違うだけでここまでパフォーマンスに影響が出るのかと知った

https://zenn.dev/sh1n4ps/articles/34df41ddc9957e

https://stackoverflow.com/questions/66514628/why-does-prefetching-not-work-for-link-component-in-next-js

ドキュメントを見たらServerless FunctionはデフォルトでワシントンDCとして設定されるとのこと

By default, Serverless Functions execute in Washington, D.C., USA (iad1) for all new projects to ensure they are located close to most external data sources

https://vercel.com/docs/functions/configuring-functions/region

Vercel Functionsの詳細

  • 何も設定していないとVercel Functions・API Routes・Server Rendering・ISRの向きがワシントンDCになるのか

https://vercel.com/docs/functions

Speed Insights・Web Analyticsの導入

  • Vercelをいじっていく中でVercel謹製のSpeed InsightsとWeb Analyticsがあることを知った
  • どのプランでも使うことができる、詳細は下記記事から確認する
  • 極端な話、VercelのダッシュボードからWeb Vitalsの確認もウォッチできるからlighthouse使う必要がなくなるのかな。一旦運用してみてる。

https://vercel.com/docs/speed-insights

https://vercel.com/docs/analytics

1zushun1zushun

Next.jsのtesting-libraryのドキュメント

以下に沿って進める

https://nextjs.org/docs/app/building-your-application/testing/jest

ドキュメントにインストールするパッケージの案内がある。
@testing-library/user-event追加した方が良いかも

RTL
$ npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom @testing-library/user-event

jest・ts-jest・@types/jestがインストールされる

Jest
$ npm init jest@latest

RTLのルールセットを入れる。
testing-library/prefer-user-eventを有効にしてfireEventを使わないようにする

ESLint
$ npm install -D eslint-plugin-testing-library

以前まとめたRTLの記事との差分はjest.config.tsの部分だけだった

このスクラップは2ヶ月前にクローズされました