【问题标题】:npm packages are not found - SyntaxError: Unexpected token '<'未找到 npm 包 - SyntaxError: Unexpected token '<'
【发布时间】:2020-12-01 07:32:51
【问题描述】:

我正在Kubernetes env (AKS) 中运行一个带有istio gateway 的反应容器

在本地环境中,使用docker build &amp;&amp; docker run 一切正常

在 Kubernetes 中,对于每个 npm 包,我得到的响应是 status code: 200, response content-type: text/html,响应内容是 index.html,这就是我假设未找到包的原因?

  • 检查 docker dist 内容时,我可以断言该包存在(例如 /dist/npm.babel.4fca54....chunk.js 存在
  • docker 镜像是使用基本上下文构建的:docker build -t imagename --build-arg PUBLIC_URL=/base-app .
  • index.html 有效并且包含有效的脚本声明。例如&lt;script type="text/javascript" src="/base-app/npm.babel.743f....chunk.js"&gt;&lt;/script&gt;

Istio 网关

spec:
  selector:
  istio: ingressgateway
  servers:
    - hosts:
      - dev.myDomain.com
      port:
        name: https
        number: 443
        protocol: HTTPS
      tls:
        credentialName: someCreds
        mode: SIMPLE

Istio VS

   - match:
    - uri:
        prefix: /base-app
     route:
      - destination:
          host: svc-app
          port:
            number: 8000

请求 + 标头的示例

Request URL: https://dev.myDomain.com/base-app/npm.babel.4fca5....chunk.js
Request Method: GET
Status Code: 200 
Remote Address: xxx.xxx.xxx:443
Referrer Policy: no-referrer-when-downgrade

响应标头

accept-ranges: bytes
content-length: 11920 ---> length of index.html
content-type: text/html --> bad needs to be text/javascript
date: Tue, 11 Aug 2020 21:30:07 GMT
etag: "5f32aff9-2e90"
last-modified: Tue, 11 Aug 2020 14:49:29 GMT
server: istio-envoy
status: 200
x-envoy-upstream-service-time: 4

【问题讨论】:

    标签: reactjs docker webpack kubernetes istio


    【解决方案1】:

    默认情况下,Istio 将从 https://dev.myDomain.com/base-app/npm.babel.4fca5....chunk.js 代理到 http://pod_ip:8000/base-app/npm.babel.4fca5....chunk.js,据我所知,您的图像直接从 /dist 提供文件,因此 Istio 实际上应该代理到 http://pod_ip:8000/npm.babel.4fca5....chunk.js;注意缺少的/base-app/。为此,请尝试将 Istio 虚拟服务更新为:

    - match:
      - uri:
          prefix: /base-app
      rewrite:
        uri: /
      route:
      - destination:
          host: svc-app
          port:
            number: 8000
    

    【讨论】:

    • 你太棒了!非常感谢您的回复。当然,你是正确的,但你可以解释为什么:1.得到200? 2. webpack public root 设置为 /base-app 但由于某种原因重写 uri 解决了这个问题? 3.index.html 调用是 200,这是主要的混淆点 --> 我认为是因为自动重定向到...index.html (:
    • 你是对的,大多数情况下 SPA 将提供index.html,而不是为不存在的 URL 返回 404。另外,配置 webpack publicPath 仅适用于生成 URL,不适用于服务行为。
    猜你喜欢
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2017-02-03
    • 2012-05-17
    • 2019-02-10
    • 2019-11-10
    相关资源
    最近更新 更多