如何处理 NextJS 导出带有 .html 扩展名但在 <Link> 中的文件没有 .html [英] How to deal with NextJS exporting files with .html extension but in<Link> there is no .html
问题描述
所以,我有一个 NextJS 应用程序,我构建链接的方式是通过组件.例如:My page
.我已经使用这种模式构建了整个应用程序,但是,当我进行导出以生成静态站点时,它创建了带有 .html 扩展名的页面的 out 文件夹.现在这是一个问题,因为假设链接可能指向 /my-page
但实际页面是 /my-page.html
.处理这个问题的正确方法是什么?NextJS 是否允许我们在这种情况下做任何事情,还是我需要返回并使用 /my-page.html
链接对所有标签进行编码?
So, I have a NextJS application and the way I have been building links is through component. For eg: <Link href="/my-page"><a>My page</a></Link>
. I have built the whole app with this pattern for However, when I did an export to generate a static site, it created the out folder with pages with .html extension. This is now a problem, since, say a link might be pointing to /my-page
but the actual page is /my-page.html
. What's the right way of handling this issue? Does NextJS allows us to do anything in this situation or do I need to go back and code all my tags using /my-page.html
links?
谢谢.
推荐答案
next.js 中静态生成的默认是映射这段代码:
The default in next.js for static generation is to map this code:
/pages/p1.tsx
到这个文件:
/p1.html
这样做的问题是,当您使用下一个服务器时,指向/p1 的链接会起作用,而在您提供静态文件时会失败.
The problem with that is that a link to /p1 will work when you use the next server, and will fail when you're serving static files.
如果启用此功能,则在 next.config.js 中:
If you enable this, in next.config.js:
module.exports = {trailingSlash: true,}
然后你会得到一个不同的文件:
Then you'll get a different file:
/p1/index.html
在大多数通用 Web 服务器(例如,开箱即用的 Apache)上,您会得到以下行为:
And on most generic web servers (Apache out of the box, for example), you get this behavior:
/p1 - redirects to /p1/
/p1/ - serves /p1/index.html
/p1/index.html - serves /p1/index.html
所以我认为 module.exports = {trailingSlash: true,}
能让你得到你所期望的行为.
So I think module.exports = {trailingSlash: true,}
gets you the behavior you're expecting.
(@jdaz 指向另一个答案的链接对此绝对有用,但该问题非常具体地与配置 .htaccess 有关,因此我在这里重复了类似的答案)
(@jdaz's link to the other answer is definitely useful for this, but that question was very specifically about configuring .htaccess, so I'm repeating a similar answer here)
这篇关于如何处理 NextJS 导出带有 .html 扩展名但在 <Link> 中的文件没有 .html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!