问题描述
我正在将next.js应用程序作为静态导出部署到配置为静态网站托管的s3存储桶中.
I'm deploying a next.js app as a static export, to an s3 bucket configured for static website hosting.
我使用next的build
和export
命令生成out/
目录,然后将其复制到我的s3存储桶中
I use next's build
and export
commands to generate the out/
directory and then copy that into my s3 bucket
然后存储桶中包含一些文件,为简单起见,可以说只有index.html
和about.html
The bucket then contains some files, for simplicity lets say there's just index.html
and about.html
问题是,当用户通过www.website.com
击中index.html
然后导航到www.website.com/about
时,一切正常,但是重新加载www.website.com/about
当然会失败.
The problem is when a user hits index.html
via www.website.com
then navigates to www.website.com/about
everything works, but reloading www.website.com/about
fails of course.
www.website.com/about.html
找到正确的资产来渲染网站
www.website.com/about.html
finds the correct asset to render the site however
是否可以导出静态的next.js应用程序,该应用程序在s3上托管,并向/about
代理服务器/about.html
发出请求?
Is there a way to export a static next.js app, host on s3, and have requests to /about
proxy /about.html
?
一如既往,感谢您的关注,并感谢您的参与.
As always, thanks for looking, and thanks even more for participating.
推荐答案
到目前为止,受此要旨启发,我得出了最好的解决方案: https://gist.github.com/rbalicki2/30e8ee5fb5bc2018923a06c5ea5e3ea5
The best solution I've arrived at so far, inspired by this gist:https://gist.github.com/rbalicki2/30e8ee5fb5bc2018923a06c5ea5e3ea5
基本上,将构建部署到s3存储桶时,您可以简单地将.html
文件重命名为没有.html
后缀,例如:www.bucket.com/about.html
-> www.bucket.com/about
,现在同时是SSR和. CSR路由工作按预期进行.
Basically when deploying the build to the s3 bucket, you can simply rename the .html
files to have no .html
suffix, ex: www.bucket.com/about.html
-> www.bucket.com/about
and now both SSR & CSR routing work as expected.
尽管没有后缀,生成的文件仍具有Content-Type: text/html
,我不知道这是否有问题.
The resulting files have Content-Type: text/html
despite not having the suffix, I don't know if this is problematic or not.
这篇关于next.js导出静态-S3-重新加载页面时路由失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!