栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Azure网站上的React App中替换%PUBLIC_URL%

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Azure网站上的React App中替换%PUBLIC_URL%

如何替换%PUBLIC_URL%

默认情况下,假设您的应用托管在服务器根目录下,Create React App会生成一个构建。

example.com

如果您的网站不是从该根目录提供的,(也许您想从进行服务
example.com/relativepath
,则可以通过在package.json中指定主页来覆盖它

"homepage": "http://example.com/relativepath",

这将使Create React App可以正确推断要在生成的HTML文件中使用的根路径。

但是,就您的情况而言,我认为您会收到404错误,因为Azure正在尝试查找index.html(或其他一些名为index。*的名称)。要解决此问题,Azure需要知道如何将预期的路由传递到位于站点根目录的唯一的index.html。使用以下内容创建一个名为

web.config
inside
/site/wwwroot
文件夹的新文件:

<?xml version="1.0" encoding="UTF-8"?><configuration>   <system.webServer>      <rewrite>         <rules> <rule name="React Routes" stopProcessing="true">    <match url=".*" />    <conditions logicalGrouping="MatchAll">       <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />       <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />    </conditions>    <action type="Rewrite" url="/" /> </rule>         </rules>      </rewrite>   </system.webServer></configuration>

这将告诉Azure重写所有URL,就像它们指向我们的根文件一样,并且我们的SPA应用程序可以正确处理链接。

参考文献:

  • https://facebook.github.io/create-react-app/docs/deployment#building-for-relative-paths

  • https://medium.com/@to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321

希望对您有帮助



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号