加载模块script - text/html而不是application/javascript失败



在同一个IIS实例上部署Angular 12,后端。net 5。没有负载均衡器

我有一个文本/html而不是应用程序/javascript响应问题,很少发生,它似乎是随机的,我不能查明原因。

Failed to load module script: The server response with atext/html&quot的非javascript MIME类型。严格的MIME类型检查Main-es2015.6ed6d8b....js:1

刷新页面修复问题,但其他网站卡住。我不确定这是由Service Worker还是其他什么引起的。Edge和Chrome都是如此。Angular是用生产模式构建的。

出现此错误时,页面加载如下:failure

main-es2015.js都有text/html响应,里面的内容是我的index.html文件中添加了script/style引用而不是纯JavaScript: failure script text/html

On success: success

从相同的错误响应线程未能加载模块脚本,我看到响应,但我已经有:

c#启动:

public void ConfigureServices(IServiceCollection services) {
services.**AddSpaStaticFiles**(configuration => configuration.RootPath = "Client/dist/AngularSpa");
}
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.**UseSpaStaticFiles**();
}

角index . html:

<!DOCTYPE html>
<html lang="en">
<head>
<base **href="/"** />
</head>
<body>
</body>
</html>

IIS服务器记录此错误:

2022-05-27 13:18:30 W3SVC2 SERVER-XX GET / - 80 - adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress
2022-05-27 13:18:30 W3SVC2 SERVER-XX GET /main-es2015.6ed6d8b5172c982059f0.js - 443 - adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress
2022-05-27 13:18:32 W3SVC2 SERVER-XX GET /main-es2015.6ed6d8b5172c982059f0.js - 443 adressMozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress
2022-05-27 13:18:32 W3SVC2 SERVER-XX GET /ngsw-worker.js - 443 adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress
2022-05-27 13:18:35 W3SVC2 SERVER-XX GET /ngsw.json ngsw-cache-bust=0.7298837691083289 443 adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress
2022-05-27 13:18:35 W3SVC2 SERVER-XX GET /index.html - 443 adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress
2022-05-27 13:18:35 W3SVC2 SERVER-XX GET /main-es2015.c448947b8e21da262380.js - 443 adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress
2022-05-27 13:18:37 W3SVC2 SERVER-XX GET /main-es5.c448947b8e21da262380.js - 443 adress Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/adress+Safari/537.36+Edg/adress

这个问题也发生在其他脚本中:多个脚本出现此错误

我的理论:问题是客户端缓存的index.html引用了旧的(在部署期间删除的)版本文件,但浏览器丢失了缓存的.js,或者由于其他原因试图从服务器请求现在已删除的文件。请求通过IIS文件系统、。net后端回退到Angular进行最终的路由匹配。它会返回index.html,因为如果用户直接调用浏览器URL,它会尝试加载Angular应用,并尝试匹配Angular路由。但是调用是由应用程序进行的,所以它只得到初始的index.html响应并中断。我实现的是在管道CD过程中,我将这些旧的散列文件移动到临时文件夹,一旦部署完成,我就从临时文件夹返回这些文件。所以在这些罕见的情况下,应用程序可以加载,然后service worker update会推送最新版本。

- task: PowerShell@2
displayName: "Move versioned Angular scripts to temp folder"
inputs:
targetType: inline
script: |
# Requirement is to have access to the root angular folder directly.
# Moves hashed js and css files that are 6 months old or newer to %temp% folder
$dateFromWhichToInclude = (Get-Date).AddMonths(-6).Date; `
$angularRoot = "${{ parameters.iis_website_physical_path }}ClientdistAngularSpa"; `
$tempFolderPath = Join-Path $Env:Temp previous-version-scripts; `
New-Item -Type Directory -Path $tempFolderPath -Force | Out-Null; `
Get-ChildItem -Path $angularRoot | Where-Object{ ($_.Name -match '.w{10,30}.(js|css)') `
-and ($_.CreationTime -ge $dateFromWhichToInclude) } `
| Copy-Item -Destination $tempFolderPath –PassThru

- task: PowerShell@2
displayName: "Move versioned Angular scripts back from temp folder"
inputs:
targetType: inline
script: |
# Requirement is to have access to the root angular folder directly.
# Once files are moved back, the created temp folder is deleted
$angularRoot = "${{ parameters.iis_website_physical_path }}ClientdistAngularSpa"; `
$tempFolderPath = Join-Path $Env:Temp previous-version-scripts; `
Copy-Item -Path "$($tempFolderPath)*" -Destination $angularRoot –PassThru; `
Remove-Item $tempFolderPath -Force -Recurse

相关内容

最新更新