Skip to content

Instantly share code, notes, and snippets.

@LeeDDHH
Last active September 24, 2022 00:12
Show Gist options
  • Save LeeDDHH/2dc54f1a3284e1d27ea92125595b2c9a to your computer and use it in GitHub Desktop.
Save LeeDDHH/2dc54f1a3284e1d27ea92125595b2c9a to your computer and use it in GitHub Desktop.
next.jsでメンテナンスモードを実装する場合の書き方

手順

  • メンテナンス用のページを定義する
    • pages/maintenance.tsx を定義する
  • next.config.js のredirectsに以下の設定をする
    • メンテナンスモードの場合、 /maintenance 以外のパスを /maintenance にリダイレクトさせる
    • メンテナンスモードではない場合、 /maintenance/ にリダイレクトさせる
  • .env ファイルにメンテナンスモードのフラグを設定する

maintainance.tsx

"use strict";

import type { NextPage } from "next";

const Maintenance: NextPage = () => {
  return <div>メンテナンス中です</div>;
};

export default Maintenance;

next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  async redirects() {
    return process.env.MAINTENANCE_MODE === "true"
      ? [
          {
            source: "/((?!maintenance$).*$)",
            destination: "/maintenance",
            permanent: false,
          },
        ]
      : [
          {
            source: "/maintenance",
            destination: "/",
            permanent: false,
          },
        ];
  },
};

module.exports = nextConfig;

.env

MAINTENANCE_MODE=false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment