10 - Integrating a server-side-rendered reactApp Flashcards
sample request-response flow with React APp
- browser opens index page
- html response sent
- browser requests JS files
- JS files response sent
- some data is requested
- order data sent
nextjs project setup basics
install react, react-dom, next
set script dev - next
how is rounting handled in nextjs
file-based routing from the pages folder
sample dockerfile to build a an express server image
FROM node:alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD [“npm”, “run”, “dev”]
client kubernetes deployment with networking service
apiVersion: apps/v1
kind: Deployment
metadata:
name: client-deployment
labels:
app: client
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
containers:
- name: client
image: node:14 # Replace with your own image
ports:
- containerPort: 3000
resources:
limits:
memory: “512Mi”
cpu: “500m”
command: [“node”, “app.js”]
apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
ports:
- name: client
- protocol: TCP
port: 3000 # The port to expose the service on
targetPort: 3000 # The port the container is listening on
nodePort: 30080 # The port exposed on the node (for NodePort only)
type: NodePort
adding client images to skaffold setup
artifacts:
- image: imageName/auth
context: auth
docker:
dockerfile: Dockerfile
sync:
manual:
- src: ‘src//.ts’
dest: .
- image: imageName/client
context: client
docker:
dockerfile: Dockerfile
sync:
manual:
- src: ‘**/.js’
dest: .
ingress config path
spec:
rules:
- host: ticketing.dev
http:
paths:
- path: /api/users/?(.)
backend:
serviceName: auth-srv
servicePort: 3000
- path: /?(.)
backend:
serviceName: client-srv
servicePort: 3000
nextjs filechange detection config
module.exports = {
webpackDevMiddleware: config => {
config.watchOptions.poll = 300;
return config;
}
}
bootstrap setup for SSR
import ‘bootstrap/dist/css/bootstrap.css’;
export default ({ Component, pageProps }) => {
return <Component { … pageProps} />
}
handling signup inputs, email, password
password, email, errors, onSubmit
useRequest hook flow
params: url, request method, request body
onSuccess callback to route to homepage
const { doRequest, errors } = useRequest({
url: ‘/api/users/signup’,
method: ‘post’,
body: {
email, password
},
onSuccess: ( ) => Router.push(“/”)
});
cross namespace service communication
getInitialProps in k8s, ingress