Open-next Cloudflare / Docker / Setup (#248)
* Add Cloudflare generator with Wrangler and OpenNext support This update introduces a new Cloudflare generator to streamline configuration and deployment via Wrangler and OpenNext. It registers the necessary templates, modifies project files, and adds Cloudflare-specific scripts and dependencies to the package.json. Additionally, .hbs files are updated in .prettierignore for formatting consistency. * Add GitHub username prompt and improve setup scripts Introduce a prompt for GitHub username to personalize project setup. Enhance the setup scripts by adding PNPM verification, configuring `upstream` remote, and removing the `origin` remote. Adjust health check and error handling for better reliability. * Add Dockerfile generator to turbo generators Introduced a new generator to create Dockerfile configurations for standalone Next.js applications. This includes modifying `next.config.mjs` for standalone output, updating dependencies in `package.json`, and adding a Dockerfile template. The generator is now registered in the turbo setup. * Add console-based logger implementation. This is required for edge environments such as Cloudflare. * Remove deprecated Supabase client utilities The `server-actions-client`, `route-handler-client`, and `server-component-client` utilities have been removed in favor of `getSupabaseServerClient`. This simplifies and consolidates the API, ensuring consistency across server-side usage. Version bumped to 2.9.0 to reflect breaking changes.
This commit is contained in:
committed by
GitHub
parent
76bfeddd32
commit
4cfb4f936f
69
turbo/generators/templates/docker/Dockerfile.hbs
Normal file
69
turbo/generators/templates/docker/Dockerfile.hbs
Normal file
@@ -0,0 +1,69 @@
|
||||
# syntax=docker.io/docker/dockerfile:1
|
||||
FROM node:20-alpine AS base
|
||||
|
||||
# Install dependencies only when needed
|
||||
FROM base AS deps
|
||||
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
|
||||
RUN apk add --no-cache libc6-compat
|
||||
WORKDIR /app
|
||||
|
||||
# Install global dependencies
|
||||
RUN corepack enable pnpm
|
||||
RUN npm install -g turbo
|
||||
|
||||
# Copy the entire project for dependency installation
|
||||
# This ensures all packages are available for resolution
|
||||
COPY . .
|
||||
|
||||
# Install dependencies using lockfile
|
||||
RUN pnpm install --frozen-lockfile
|
||||
RUN npm rebuild lightingcss --build-from-source --verbose
|
||||
|
||||
# Rebuild the source code only when needed
|
||||
FROM base AS builder
|
||||
WORKDIR /app
|
||||
|
||||
# Install global dependencies for the build
|
||||
RUN corepack enable pnpm
|
||||
RUN npm install -g turbo
|
||||
|
||||
# Copy over everything including dependencies
|
||||
COPY --from=deps /app ./
|
||||
|
||||
# Disable telemetry during the build
|
||||
ENV NEXT_TELEMETRY_DISABLED=1
|
||||
|
||||
# Build the project
|
||||
RUN turbo run build --filter=web...
|
||||
|
||||
# Production image, copy all the files and run next
|
||||
FROM base AS runner
|
||||
WORKDIR /app
|
||||
|
||||
ENV NODE_ENV=production
|
||||
ENV NEXT_TELEMETRY_DISABLED=1
|
||||
|
||||
# Create a non-root user
|
||||
RUN addgroup --system --gid 1001 nodejs
|
||||
RUN adduser --system --uid 1001 nextjs
|
||||
|
||||
# Copy public assets
|
||||
COPY --from=builder /app/apps/web/public ./apps/web/public
|
||||
|
||||
# Leverage output traces to reduce image size (standalone output)
|
||||
COPY --from=builder --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
|
||||
COPY --from=builder --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
|
||||
|
||||
# Switch to non-root user
|
||||
USER nextjs
|
||||
|
||||
# Set server port and host
|
||||
EXPOSE 3000
|
||||
ENV PORT=3000
|
||||
ENV HOSTNAME="0.0.0.0"
|
||||
|
||||
HEALTHCHECK --interval=90s --timeout=5s --retries=3 \
|
||||
CMD curl -f http://localhost:3000/healthcheck || exit 1
|
||||
|
||||
# Start the server
|
||||
CMD ["node", "apps/web/server.js"]
|
||||
80
turbo/generators/templates/docker/generator.ts
Normal file
80
turbo/generators/templates/docker/generator.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { PlopTypes } from '@turbo/gen';
|
||||
import { execSync } from 'node:child_process';
|
||||
import * as os from 'node:os';
|
||||
|
||||
export function createDockerGenerator(plop: PlopTypes.NodePlopAPI) {
|
||||
plop.setGenerator('docker', {
|
||||
description: 'Dockerfile generator',
|
||||
actions: [
|
||||
{
|
||||
type: 'modify',
|
||||
path: 'apps/web/next.config.mjs',
|
||||
transform(content) {
|
||||
// Check if the output is already set to standalone
|
||||
if (content.includes('output: "standalone"')) {
|
||||
return content;
|
||||
}
|
||||
|
||||
return content.replace(
|
||||
'const config = {',
|
||||
'const config = { output: "standalone",',
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'modify',
|
||||
path: 'apps/web/package.json',
|
||||
transform(content) {
|
||||
const pkg = JSON.parse(content);
|
||||
const deps = getDeps();
|
||||
|
||||
if (deps.length === 0) {
|
||||
return content;
|
||||
}
|
||||
|
||||
for (const dep of deps) {
|
||||
pkg['devDependencies'][dep] = 'latest';
|
||||
}
|
||||
|
||||
return JSON.stringify(pkg, null, 2);
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
templateFile: 'templates/docker/Dockerfile.hbs',
|
||||
path: 'Dockerfile',
|
||||
},
|
||||
async () => {
|
||||
execSync('pnpm i', {
|
||||
stdio: 'inherit',
|
||||
})
|
||||
|
||||
execSync('pnpm format:fix', {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
|
||||
return 'Dockerfile generated';
|
||||
},
|
||||
],
|
||||
prompts: [],
|
||||
});
|
||||
}
|
||||
|
||||
function getDeps() {
|
||||
const arch = os.arch();
|
||||
|
||||
if (arch === 'arm64') {
|
||||
return [
|
||||
'lightningcss-linux-arm64-musl',
|
||||
'@tailwindcss/oxide-linux-arm64-musl',
|
||||
];
|
||||
} else if (arch === 'x64') {
|
||||
const isMusl = process.config?.variables?.hasOwnProperty('musl');
|
||||
|
||||
return isMusl
|
||||
? ['lightningcss-linux-x64-musl', '@tailwindcss/oxide-linux-x64-musl']
|
||||
: ['lightningcss-linux-x64-gnu', '@tailwindcss/oxide-linux-x64-gnu'];
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user