Viteでrobots.txtを公開する方法です。
本ページはReact + Typescriptのプロジェクトを作成した場合の例になります。
public
ディレクトリーを作成してファイルを配置
:
├ my_app/
│ ├ dist/
│ ├ node_modules/
│ ├ public/
│ │ └ robots.txt
│ └ src/
:
vite.config.ts
を編集
assetsInclude: ["robots.txt"]
をコンフィグに追記します。
デフォルトではpublic
ディレクトリーを参照するようになっていますが、変更することもできます。
import { defineConfig } from "vite"
import reactRefresh from "@vitejs/plugin-react-refresh"
// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()],
assetsInclude: ["robots.txt"],
})
確認
ローカル環境で確認するにはyarn dev
を実行してhttp://localhost:ポート番号/robots.txt
でrobots.txt
の中身が表示されれば成功です。
補足
yarn build
を実行するとdist
ディレクトリーにファイルをコピーします。そのためdist
ディレクトリーにファイルがあるかどうかで確認することもできます。