简体   繁体   English

IIS 子文件夹中的 Angular PWA 无法脱机工作

[英]Angular PWA in IIS subfolder not work offline

When I publish angular pwa application to subfolder in IIS, pwa application not work in offline.当我将 angular pwa 应用程序发布到 IIS 中的子文件夹时,pwa 应用程序无法离线工作。

ng build --prod --baseHref=/subfolder/ does not help. ng build --prod --baseHref=/subfolder/ 没有帮助。

I use https on web site.我在网站上使用 https。

How exactly reconfigure angular pwa application to work offline if application is not published to root of IIS but to subfolder of IIS?如果应用程序不是发布到 IIS 的根目录而是发布到 IIS 的子文件夹,那么如何准确地重新配置 angular pwa 应用程序以使其脱机工作?

Does anybody have functional demo of angular pwa application working offline after publishing to IIS subfolder?发布到 IIS 子文件夹后,是否有人有 angular pwa 应用程序脱机工作的功能演示?

I know I am very late on this, I have also struggled on this and finally followed these steps to solve the issue:我知道我在这方面已经很晚了,我也在这方面苦苦挣扎,最后按照以下步骤解决了问题:

  1. In manifest.JSON file, set "scope": "."manifest.JSON文件中,设置"scope": "." and "start_url": "./" ."start_url": "./" This indicate that you are using sub-directory.这表明您正在使用子目录。

  2. Then build the project using CLI command ng build --prod --baseHref=/Your_sub_directory_name/然后使用 CLI 命令ng build --prod --baseHref=/Your_sub_directory_name/构建项目

  3. And finally paste the dist folder content to your pointed Your_sub_directory_name IIS location.最后将dist文件夹内容粘贴到您指定的Your_sub_directory_name IIS 位置。

  4. Then just refresh page and wait to register the service worker, you can check this in application tab of chrome's developer tool.然后只需刷新页面并等待注册服务工作者,您可以在 chrome 的开发人员工具的应用程序选项卡中查看。 Once service worker is reigstered, just switch to offline mode and you'll the page will work fine in offline.一旦 service worker 被注册,只需切换到离线模式,页面就会在离线状态下正常工作。

The --baseHref=/mypath/ for building from Vinod led me the way, thanks!!用于从 Vinod 构建的 --baseHref=/mypath/ 为我指明了方向,谢谢!! Some more observations:还有一些观察:

  • Angular's service-worker seems to be case sensitive on URLs: If in your ngsw.json you have /MyPath/index.html and someone runs the page typing mysite.com/mypath the service worker fails. Angular 的 service-worker 似乎对 URL 区分大小写:如果在你的 ngsw.json 中有 /MyPath/index.html 并且有人运行输入 mysite.com/mypath 的页面,service worker 就会失败。 Nasty!可恶的! As per the spec the path of a URL is case-sensitive, but well...按照规范中的一个URL的路径区分大小写的,但是呢......

  • If you use IIS: Just stopping a website will NOT simulate an offline situation for the Angular service-worker.如果您使用 IIS:只是停止网站不会模拟 Angular 服务工作者的离线情况。 You would have to stop the entire IIS.您将不得不停止整个 IIS。

  • From my observation the manifest does not matter for the service worker / offline functionality.根据我的观察,清单对于服务工作者/离线功能无关紧要。 You can comment it out in index.html.您可以在 index.html 中将其注释掉。 Required for install only.仅安装时需要。 Vinod's values worked for that. Vinod 的价值观为此发挥了作用。

Useful steps for newbies to follow what's going on:新手遵循正在发生的事情的有用步骤:

  1. After a build check ngsw.json in dist.在构建后检查 dist 中的 ngsw.json。 All files should be prefixed with /mypath/.所有文件都应以 /mypath/ 为前缀。

  2. After loading in Chrome check DevTools/Application/Cache storage.在 Chrome 中加载后检查 DevTools/Application/Cache 存储。 The entry "ngsw:/...:assets:app:cache" should contain the files from ngsw.json WITH /mypath/ prefix.条目“ngsw:/...:assets:app:cache”应该包含来自 ngsw.json 的文件,并带有 /mypath/ 前缀。

  3. Have DevTool/Network open when hitting F5 too see WHERE files come from.在点击 F5 时也打开 DevTool/Network,看看文件来自哪里。 For files to work offline a refresh should show (ServiceWorker) in the size column.对于离线工作的文件,刷新应该在大小列中显示 (ServiceWorker)。

I found that putting baseHref in angular.json also works in this situation.我发现将 baseHref 放在 angular.json 中也适用于这种情况。 Makes sense next to outputPath.在 outputPath 旁边有意义。

  "outputPath": "D:/somewhere/WwwRoot/full/public/path",
  "baseHref":"/full/public/path/",

Issue that i faced was, After deployment on Azure, Angular PWA service worker does not fetch the api response from cache in Offline mode, and also the manifest created was showing error in deployed version, whereas in localhost it was all working perfect.我面临的问题是,在 Azure 上部署后,Angular PWA 服务工作者不会在离线模式下从缓存中获取 api 响应,而且创建的清单在部署版本中显示错误,而在 localhost 中,它一切正常。 the main issue for me was: By default, IIS does not serve any files that does not have a MIME map associated with it in its (IIS) core settings.对我来说的主要问题是:默认情况下,IIS 不提供任何在其 (IIS) 核心设置中没有关联 MIME 映射的文件。 To address this challenge, you will need to map the .webmanifest file extension to its appropriate MIME type.为了应对这一挑战,您需要将 .webmanifest 文件扩展名映射到其适当的 MIME 类型。 For this you need to add following to web.config file:为此,您需要在 web.config 文件中添加以下内容:

        <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json" />
            <mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
        </staticContent>

this helps azure to understand our manifest.webmanifest file which otherwise it will not be able to.这有助于 azure 理解我们的 manifest.webmanifest 文件,否则它将无法理解。 After this it was able to detect manifest.webmanifest file which solved my both issues, Fetching the response from cache in Offline mode, And also with manifest file now my Angular PWA app was installable with app icon and all other features.在此之后,它能够检测到 manifest.webmanifest 文件,该文件解决了我的两个问题,在离线模式下从缓存中获取响应,并且现在我的 Angular PWA 应用程序可以使用应用程序图标和所有其他功能安装清单文件。 You can also refer to my question to get rest of details on ngsw-config.json file and full code for web.config file etc, After deployment Angular PWA service worker does not fetch the api response from cache in Offline mode您还可以参考我的问题以获取有关 ngsw-config.json 文件的其余详细信息以及 web.config 文件的完整代码等,部署后 Angular PWA 服务工作者不会在离线模式下从缓存中获取 api 响应

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

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