简体   繁体   English

如何配置我的 React 项目以从公共文件夹 html 文件中删除.html 扩展?

[英]How do I configure my React project to remove .html extensions from public folder html files?

I currently am working on a project that involves appending a react app to a static website.我目前正在开展一个项目,该项目涉及将反应应用程序附加到 static 网站。 I can't just convert the html to jsx because the website uses custom css that can't be easily rendered by React without a ton of refactoring.我不能只将 html 转换为 jsx,因为该网站使用自定义 css,如果不进行大量重构,React 就无法轻松呈现。

I've been using react-app-rewired-multiple-entry to add the html files to the project, which worked in development, but didn't work in the build.我一直在使用 react-app-rewired-multiple-entry 将 html 文件添加到项目中,该项目在开发中有效,但在构建中无效。 Another approach I have considered is putting the html files into the public folder and defining the route to the public folder to not require.html extensions to access html files.我考虑的另一种方法是将 html 文件放入公用文件夹,并将到公用文件夹的路径定义为不需要。html 扩展来访问 html 文件。 I can't use React-Router-DOM or anything because these files are outside of the scope of the react-app.我不能使用 React-Router-DOM 或任何东西,因为这些文件位于 react-app 的 scope 之外。

The HTML files will render properly when placed in the public folder, but they have that pesky.html extension in the URL, and I was wondering if there's a way to rewire the create-react-app configuration, or even next.js configurations, to tell it's internal router to serve.html files when the extensions aren't put into the URL. The HTML files will render properly when placed in the public folder, but they have that pesky.html extension in the URL, and I was wondering if there's a way to rewire the create-react-app configuration, or even next.js configurations, to tell当扩展名未放入 URL 时,它是用于服务的内部路由器。html 文件。

SOLVED:解决了:

So my mistake was assuming that the dev server was the same server that the build bundle was served on.所以我的错误是假设开发服务器与提供构建包的服务器相同。 Huge mistake.巨大的错误。 To elaborate, what this means is that when I would I would do serve -s build, the server set up by serve didn't have the routes set up to display the static files, even though I did properly by modifying the dev server.详细地说,这意味着当我要进行 serve -s build 时,由 serve 设置的服务器没有设置路由来显示 static 文件,即使我通过修改开发服务器做得正确。

What I wound up doing was creating a custom (but simple) server.js file that served the unique build bundle, serving each static file separately.我最终要做的是创建一个自定义(但简单)的 server.js 文件,该文件为唯一的构建包提供服务,分别为每个 static 文件提供服务。 It wound up looking something like this (code provided in case anyone else encounters this problem):它最终看起来像这样(提供代码以防其他人遇到此问题):

const path = require("path");
const express = require("express");
const app = express(); // create express app

// serve the react-app by serving index.html
app.get("/react-app", (req, res) => {
  res.sendFile(path.resolve("./build/index.html"));
});

// serving the static files at different routes
const routes = ['home', 'about', 'help', 'etc'];

routes.forEach(route => {
  app.get(`/${route}`, (req, res) => {
    res.sendFile(path.resolve(`./build/${route}.html`));
  });
});

// start express server on port 5000
app.listen(5000, () => {
  console.log("server started on port 5000");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM