在 next.js 中将顶级域作为静态 html 页面提供服务? [英] Serve top level domain as static html page in next.js?

查看:27
本文介绍了在 next.js 中将顶级域作为静态 html 页面提供服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 next.js 中,您可以使用根目录中的公共"目录来提供静态资产.我在这个目录中有我的 index.html 文件,因为我希望我的登录页面是纯静态的.

In next.js you can use the "public" directory in your root directory to serve static assets. I have my index.html file in this directory because I want my landing page to be purely static.

但是,当使用 nextnext start 时,我无法在 http://localhost:3000/.我只能通过http://localhost:3000/index.html"访问我的登录页面.

However, when using next or next start, I cannot access my landing page at http://localhost:3000/. I can only access my landing page at "http://localhost:3000/index.html".

有没有办法设置next.js,所以顶级域(http://localhost:3000) 将提供我的目标网页?

Is there a way to setup next.js, so the top level domain (http://localhost:3000) will serve my landing page?

推荐答案

Next.js 路由器只提供从 .js.ts 导出的页面.pages 目录中的 tsx.

Next.js router serves only pages exported from .js, .ts and .tsx in pages directory.

您需要一个自定义服务器来提供除此之外的任何内容.

You would need a custom server that would serve anything outside of that.

自定义服务器

另外,如果你使用像 nginx 这样的反向代理,你可以在它到达 Node.js 之前由服务器处理这个请求.

Also, if you use a reverse proxy like nginx you could handle this request by the server before it hits Node.js.

它的用例是什么?如果登录页面只是一个 .html 文件,您可以将其包装在 Next.js 页面中.使用服务器端渲染,页面将在服务器上预渲染并提供与它只是 .html 文件相同的服务.

What is the use case of it? If the landing page is just a .html file, you can wrap it in a Next.js page. With server side rendering, the page will be pre-rendered on the server and served the same as it was just a .html file.

或者,如果您无法将 HTML 转换为 JSX,您可以使用 dangerouslySetInnerHTML 将原始 HTML 代码设置为 React 的 render().但是,请注意跨站点脚本 (XSS) 攻击.

Alternatively, if you can't convert HTML to JSX you could use dangerouslySetInnerHTML to set raw HTML code to React's render(). However, beware of cross-site scripting (XSS) attacks.

这篇关于在 next.js 中将顶级域作为静态 html 页面提供服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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