重新加载页面时路由失败

重新加载页面时路由失败

本文介绍了next.js导出静态-S3-重新加载页面时路由失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将next.js应用程序作为静态导出部署到配置为静态网站托管的s3存储桶中.

I'm deploying a next.js app as a static export, to an s3 bucket configured for static website hosting.

我使用next的buildexport命令生成out/目录,然后将其复制到我的s3存储桶中

I use next's build and export commands to generate the out/ directory and then copy that into my s3 bucket

然后存储桶中包含一些文件,为简单起见,可以说只有index.htmlabout.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-重新加载页面时路由失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:18