Skip to content

Commit

Permalink
feat: update Dockerfile and middleware routing
Browse files Browse the repository at this point in the history
  • Loading branch information
PAYNEA03 committed Aug 2, 2024
1 parent 741bb9e commit 1cddf72
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
38 changes: 16 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
FROM node:18.18-alpine AS deps
# RUN apk add --no-cache libc6-compat gcompat
# RUN apk add --no-cache g++ make
# Stage 1: Build
FROM node:18.18-alpine AS builder

# Set working directory
WORKDIR /app

COPY package.json package-lock.json ./
RUN --mount=type=secret,id=FONTAWESOME_NPM_AUTH_TOKEN \
npm config set "@fortawesome:registry" https://npm.fontawesome.com/ && \
export FONTAWESOME_NPM_AUTH_TOKEN=$(cat /run/secrets/FONTAWESOME_NPM_AUTH_TOKEN) && \
npm config set "//npm.fontawesome.com/:_authToken" $FONTAWESOME_NPM_AUTH_TOKEN && \
npm install && \
echo "done"
# find /app/node_modules/ ! -user root | xargs chown root:root

# Copy package.json and package-lock.json
COPY package*.json ./

FROM node:18.18-alpine AS builder
# RUN apk add --no-cache libc6-compat gcompat
# RUN apk add --no-cache g++ make
# Install dependencies
RUN npm install

# RUN ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
# Copy all project files
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

# Build the application
RUN npm run build

# Stage 2: Run
FROM node:18.18-alpine AS runner

# Set working directory
WORKDIR /app

ENV NODE_ENV production
Expand All @@ -37,16 +30,17 @@ ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Copy only the necessary files from the build stage
COPY --from=builder /app/package*.json ./
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/server.js ./server.js
COPY --from=builder /app/middleware.ts ./

# Expose the port the app runs on
USER nextjs

EXPOSE 3000

ENV PORT 3000

# Command to run the app
CMD ["node", "server.js"]
9 changes: 9 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/resourcing')) {
return NextResponse.rewrite(new URL('/', request.url));
}
return NextResponse.next();
}
23 changes: 8 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,22 @@ const handler = app.getRequestHandler();
const nextUpgradeHandler = app.getUpgradeHandler();

app.prepare().then(() => {
const httpServer = createServer((req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname } = parsedUrl;

if (pathname.startsWith('/resourcing')) {
// Rewrite /resourcing to /
const newUrl = req.url.replace('/resourcing', '');
req.url = newUrl;
app.render(req, res, '/', parsedUrl.query);
} else {
handler(req, res, parsedUrl);
}
});
const httpServer = createServer(handler);

httpServer
.once('error', (err) => {
console.error(err);
process.exit(1);
})
// eslint-disable-next-line consistent-return
.on('upgrade', (request, socket, head) => {
// You may check auth of request here..
// See https://github.com/websockets/ws#client-authentication
/**
* @param {any} ws
*/
const { pathname } = parse(request.url || '/', true);

// Make sure we all for hot module reloading
if (pathname === '/_next/webpack-hmr') {
return nextUpgradeHandler(request, socket, head);
}
Expand All @@ -55,7 +49,6 @@ app.prepare().then(() => {
wss.emit('connection', ws, request);
};
wss.handleUpgrade(request, socket, head, handleAuth);
return undefined; // Explicitly return undefined to satisfy consistent-return
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
Expand Down

0 comments on commit 1cddf72

Please sign in to comment.