简体   繁体   English

构建后未显示公共目录中的 React sitemap.xml 文件

[英]React sitemap.xml file in public directory is not showing after build

I have a script that generates sitemap.xml file in the public directory.我有一个在公共目录中生成 sitemap.xml 文件的脚本。 It is accessible in localhost:3000/sitemap.xml when it is in development.它在开发时可以在 localhost:3000/sitemap.xml 中访问。 But when it goes for build, I cant access the file from the public folder.但是当它进行构建时,我无法从公用文件夹访问该文件。 I made some research and I found out build doesnt have access of the public folder!我做了一些研究,发现 build 没有公共文件夹的访问权限! Please help me with a way to access the public/sitemap.xml if there is any, if not is it possible to show .xml format as a page in reac-router-dom?如果有的话,请帮助我访问 public/sitemap.xml,如果没有,是否可以将 .xml 格式显示为 reac-router-dom 中的页面?

Reading through your question and comments there isn't a lot to work with, however I can inform you of a couple of things.通读您的问题和评论,并没有太多需要解决的问题,但是我可以告诉您一些事情。

  • The reason your sitemap might work locally and not on a build is because you're very likely using webpack-dev-server (if you're using CRA then you're using it), which in return uses express to locally serve your files.您的站点地图可能在本地工作而不是在构建上工作的原因是因为您很可能使用 webpack-dev-server(如果您使用的是 CRA,那么您正在使用它),作为回报,它使用 express 在本地为您的文件提供服务. When you build it will no longer be using that server and you'll need your own custom server.当您构建它时,它将不再使用该服务器,您将需要自己的自定义服务器。

  • When you build your files for production it will output all your files to static js/css files which then need to be served using some or other server (express/nginx/apache).当您为生产构建文件时,它会将所有文件输出到静态 js/css 文件,然后需要使用某些或其他服务器(express/nginx/apache)提供这些文件。 In your question you've not specified what server you're using, beyond the development server, so I can only assume that you probably don't have any and/or are depending on some host that you're using without realizing that you're using one.在您的问题中,除了开发服务器之外,您还没有指定您正在使用的服务器,所以我只能假设您可能没有任何和/或依赖于您正在使用的某个主机而没有意识到您'正在使用一个。

  • When you want to serve something like .xml, you have to configure the server to actually serve .xml files.当您想要提供类似 .xml 的内容时,您必须将服务器配置为实际提供 .xml 文件。 Also you should not be serving .xml as any form of react route, it's not a route and it has nothing to do with react.此外,您不应该将 .xml 作为任何形式的反应路线提供服务,它不是路线,与反应无关。 Your request should directly hit the server, ie nginx, and that server should be configured to serve the .xml file.您的请求应直接命中服务器,即 nginx,并且该服务器应配置为提供 .xml 文件。

  • In your comments you mentioned that it's "sitemap.exe", I'm not sure if that's a typo but you can't serve and run .exe files over web and no server will be configured to do so by default.在您的评论中您提到它是“sitemap.exe”,我不确定这是否是一个错字,但您无法通过网络提供和运行 .exe 文件,并且默认情况下不会配置任何服务器。 The sitemap.xml or sitemap.txt should contain only the valid xml or text content and also make sure it's actually in the build directory once you've built it. sitemap.xml 或 sitemap.txt 应仅包含有效的 xml 或文本内容,并确保在构建后它实际上位于构建目录中。

  • If the .xml exists in your build and all of that's fine then the only thing left you need is to add a location block to your nginx server, or to add a get route to your express (or static resources) in order to actually serve that file.如果 .xml 存在于您的构建中并且所有这些都很好,那么您唯一需要做的就是向您的 nginx 服务器添加一个位置块,或者向您的 express(或静态资源)添加一个获取路由以便实际服务那个文件。 If your server is giving you a 404 at that point it likely means it isn't recognizing .xml files at all.如果您的服务器此时给您一个 404,则可能意味着它根本无法识别 .xml 文件。 That needs to be configured in your server.这需要在您的服务器中进行配置。

In nginx it means you need something like location = /sitemap.xml .在 nginx 中,这意味着您需要诸如location = /sitemap.xml类的东西。
In express it means you need something like app.get('/sitemap.xml') .明确地说,这意味着您需要app.get('/sitemap.xml')类的东西。

A default nginx installation with try_files $uri $uri/ =404 should work for .xml as well.带有try_files $uri $uri/ =404的默认 nginx 安装也应该适用于 .xml。

尝试添加 sitemap.xml 作为反应路由器。

我通过进入“重写和重定向”部分解决了类似的问题,并添加了一条规则说“/sitemap.xml”的“源地址”将重定向到“/sitemap.xml”,并将该规则优先于其中的规则</^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|woff2|ttf|map|json)$)([^.]+$)/>被重定向到/index.html

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

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