参考にさせていただいたページ
解決したい事象
以下の手順を実行したときにnetlifyが用意している404ページが表示されてしまいました。
yarn create react-app my-app --template typescript
でreactアプリケーションをcreateします。- /exampleページを作ります。
- react-router-domのLinkコンポーネントで、rootページから/exampleページへのリンクを設置します。
yarn build
します。- buildされたディレクトリ/buildをnetlifyにデプロイします。
- 「 https://xxxxx.netlify.app(rootページ)」にアクセスします。
- リンクをクリックして「 https://xxxxx.netlify.app/example」に遷移します。
- リロードするとnetlifyが用意している404ページが表示されてしまいます。
以下のようなページが表示されました。
Page Not Found
Looks like you've followed a broken link or entered a URL that doesn't exist on this site.
Back to our site
If this is your site, and you weren't expecting a 404 for this path, please visit Netlify's "page not found" support guide for troubleshooting tips.
どうしたいか
rootページ以外でリロードしたとき、サーバーにリクエストして同期的に遷移したときも、reactアプリケーションのルーティングに従ってページを表示したい。
解決方法
- 参考ページにならって、build/_redirectsファイルを作成し、以下の通り記述します。
/* /index.html 200
- デプロイし直します。