1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

golang+ginでreact router対応してみた

Posted at

問題

r.StaticFile("/", "./dist")などの書き方だと、ルートページから遷移する場合には動作するが、URL直打ちのように直接アクセスすると404が返ってくる

結論

file path + request uriで対応できる

r.NoRoute(func(c *gin.Context) {
	_, file := path.Split(c.Request.RequestURI)
	ext := filepath.Ext(file)
	//ディレクトリアクセス(ファイル名がない)かパスクエリ(拡張子がない)
	if file == "" || ext == "" {
		c.File("./dist" + "/index.html")
	} else {
		c.File("./dist" + c.Request.RequestURI)
	}
})

react routerのような静的fileをserveするときなら全部これで対応できるかなと思います。
たとえsolidjsやvueなども可能。
実際主もsolidjs + vite + vite-plugin-pagesの環境でテストしているいけたので、多分この方法で可能かと思います。

参考記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?