我想在next.js上使用html文件作为我的主页

pbossiut  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(178)

当有人访问我的根目录“https://example.com/“我想呈现一个正常的.html文件。(index.html)我怎么能用下一个.js做到这一点?
我做了一个研究和编辑我的配置文件像这样,

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  rewrites: async () => {
    return [
      {
        source: "/public/myfile.html",
        destination: "/pages/api/myfile.js",
      },
    ];
  },
};

/pages/api/myFile.js

import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;

但是这个配置文件渲染我的.html页面,就在我点击这个URL的时候:https://example.com/index.html

prdp8dxp

prdp8dxp1#

rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],

在我的next.js.js文件中添加了这个(在nextConfig对象中)
我的/public目录下有一个index.html文件。

vqlkdk9b

vqlkdk9b2#

超级简单的方法是将你的HTML代码与CSS和JS一起粘贴到public文件夹中,这样你就可以路由到mywebsite.com/index.html,而不再需要代码:)
您可以通过添加以下内容来处理next.config文件的主URL重定向:您必须从SRC或应用程序文件夹中删除主索引.TSX或页面.TSX

rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],

相关问题