如何处理 NextJS 导出带有 .html 扩展名但在<Link>中的文件没有.html [英] How to deal with NextJS exporting files with .html extension but in<Link> there is no .html

查看:28
本文介绍了如何处理 NextJS 导出带有 .html 扩展名但在<Link>中的文件没有.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个 NextJS 应用程序,我构建链接的方式是通过组件.例如:<Link href="/my-page>><a>我的页面</a></Link>.我已经使用这种模式构建了整个应用程序,但是,当我进行导出以生成静态站点时,它创建了带有 .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

在大多数通用网络服务器(例如,开箱即用的 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 扩展名但在&lt;Link&gt;中的文件没有.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆