【问题标题】:How do I deploy a react pwa to IBM Cloud Kubernetes cluster?如何将 React pwa 部署到 IBM Cloud Kubernetes 集群?
【发布时间】:2020-01-01 18:04:51
【问题描述】:

我创建了免费的 IBM Cloud Kubernetes 集群,并尝试从 Docker 映像中部署我的 react pwa(使用 create-react-app 创建)。我设法在集群中部署了应用程序,但我的 service worker 不工作。

当我执行npm run buildserve -s build 时,应用程序中的一切都在 localhost:5000 中正常工作。

但在已部署的应用程序中,在导航器中找不到 serviceWorker,并且它从不注册 serviceWorker:

if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
...
//never goes in here
registerValidSW(swUrl, config);
});

在尝试访问已部署应用程序中的缓存时也会出现此错误:

Uncaught (in promise) ReferenceError: caches is not defined
at c (runtime.js:45)
at Generator._invoke (runtime.js:264)
at Generator.O.forEach.e.<computed> [as next] (runtime.js:98)
at r (asyncToGenerator.js:3)
at l (asyncToGenerator.js:25)
at asyncToGenerator.js:32
at new Promise (<anonymous>)
at t.getRequests (asyncToGenerator.js:21)
at t.value (index.js:142)

我的 Dockerfile 如下所示:

FROM node:alpine as builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx
EXPOSE 80
COPY --from=builder /app/build /usr/share/nginx/html

我的 deployment.yaml 看起来像这样:

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
  labels:
    app: app
spec:
  type: NodePort
  ports:
  - port: 80
    name: http
  selector:
    app: app
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: my-app
        image: us.icr.io/my-app-namespace/my-app
        ports:
        - containerPort: 8080

【问题讨论】:

    标签: reactjs kubernetes ibm-cloud service-worker progressive-web-apps


    【解决方案1】:
    • 你在本地安装了nodenpm哪个版本?

      你在你的Dockerfile设置了最新版的node

      FROM node:alpine as builder

    • 确定您在本地运行的版本: node --version

      现在在这里选择正确的版本Docker Hub node tags

      示例:如果您的节点版本是8,您需要设置一些版本,例如:

      FROM node:8-alpine

    【讨论】:

    • 我有npm 6.9.0node 12.6.0。我将Dockerfile 中的node 的版本更改为FROM node:12.6.0-alpine as builder,但没有帮助。还有其他想法吗?
    • 您设置了 `process.env.NODE_ENV === 'production',但没有在 Kubernetes 部署中设置环境变量?
    • 我检查了process.env.NODE_ENV它设置为生产。但是当我登录导航器时,它没有 serviceWorker。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2020-01-20
    • 2019-11-25
    相关资源
    最近更新 更多