How do I deploy a react pwa to IBM Cloud Kubernetes cluster?

1/1/2020

I have created the free IBM Cloud Kubernetes cluster and try to deploy my react pwa(created with create-react-app) in it from Docker image. I managed to deploy the app in the cluster, but my service worker doesn't work.

When I do npm run build and serve -s build everything in the app is working fine in localhost:5000.

But in the deployed app the serviceWorker is not found in the navigator and it never register the service worker:

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

And also getting this error when trying to access caches in the deployed app:

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)

My Dockerfile looks like this:

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

And my deployment.yaml is looking like this:

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
-- MJICT
ibm-cloud
kubernetes
progressive-web-apps
reactjs
service-worker

1 Answer

1/1/2020
  • Which node and npm version do you have installed locally?

    You set up in your Dockerfile the latest version of node:

    FROM node:alpine as builder

  • Identify the version that you have locally running: node --version

    Now select the right version here Docker Hub node tags

    Example: If your node version is 8, you need set some version like:

    FROM node:8-alpine

-- Jhonn Frazão
Source: StackOverflow