Skip to content

Commit

Permalink
feat: updated to Nextjs 14
Browse files Browse the repository at this point in the history
  • Loading branch information
riipandi committed Sep 27, 2024
1 parent fee946f commit f4e5f78
Show file tree
Hide file tree
Showing 30 changed files with 2,987 additions and 4,106 deletions.
8 changes: 4 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/.cache
/public/build
/build
/tmp
build/
tmp/
.cache
.next
.fleet
.github
Expand All @@ -12,6 +11,7 @@
Dockerfile
node_modules
npm-debug.log
CHANGELOG.md
README.md
*.log
.env*
2 changes: 1 addition & 1 deletion .ncurc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"deep": true,
"install": "always",
"packageManager": "pnpm",
"reject": []
"reject": ["typescript", "eslint"]
}
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
.cache/
.fleet/
.github/
.next/
.vercel/
build/
dist/
out/
script/
node_modules/
# .eslintrc.json
package-lock.json
Expand Down
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"trailingComma": "es5",
"useTabs": false,
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindConfig": "./tailwind.config.js",
"overrides": [
{
"files": ["*.html", "*.css", ".sass"],
Expand Down
4 changes: 3 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"EditorConfig.EditorConfig",
"esbenp.prettier-vscode",
"foxundermoon.shell-format",
"unifiedjs.vscode-mdx"
"ms-playwright.playwright",
"tamasfe.even-better-toml",
"unifiedjs.vscode-mdx",
]
}
13 changes: 10 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
"request": "launch",
"name": "Debug Next.js",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["dev"],
"skipFiles": ["<node_internals>/**"],
"runtimeArgs": [
"dev"
],
"skipFiles": [
"<node_internals>/**"
],
// "serverReadyAction": {
// "action": "debugWithChrome",
// "pattern": "started server on .+, url: (https?://.+)",
Expand Down Expand Up @@ -51,7 +55,10 @@
"compounds": [
{
"name": "Start Development",
"configurations": ["Debug Next.js", "Browser Debug"],
"configurations": [
"Debug Next.js",
"Browser Debug"
],
"stopAll": true
}
]
Expand Down
45 changes: 33 additions & 12 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"typescript.enablePromptUseWorkspaceTsdk": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.fixAll.eslint": "always"
},
"eslint.workingDirectories": [{ "mode": "auto" }],
"eslint.workingDirectories": [
{
"mode": "auto"
}
],
"eslint.format.enable": true,
"eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"],
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"files.associations": {
"*.mdx": "markdown"
"*.css": "tailwindcss",
"*.mdx": "markdown",
"*.yml.example": "yaml"
},
"search.exclude": {
"**/.husky": true,
"**/.output": true,
"**/build": true,
"**/dist": true,
},
"tailwindCSS.emmetCompletions": true,
"tailwindCSS.experimental.classRegex": [
"tw`([^`]*)", // tw`...`
"tw\\.[^`]+`([^`]*)`", // tw.xxx<xxx>`...`
"tw\\(.*?\\).*?`([^`]*)", // tw(Component)<xxx>`...`
"cn\\(([^)]*)\\)",
"clx\\(([^)]*)\\)",
"cva\\(([^)]*)\\)"
[
"clsx\\(([^)]*)\\)",
"(?:'|\"|`)([^']*)(?:'|\"|`)"
],
"cn\\(([^)]*)\\)", // cn(xxx)
"clx\\(([^)]*)\\)", // clx(xxx)
"cx\\(([^)]*)\\)", // cx(xxx)
],
"[toml]": {
"editor.formatOnSave": false
"editor.formatOnSave": false,
"editor.defaultFormatter": "tamasfe.even-better-toml"
},
"[markdown]": {
"editor.defaultFormatter": "darkriszty.markdown-table-prettify"
}
}
101 changes: 59 additions & 42 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,69 +1,86 @@
# syntax=docker/dockerfile:1.4
# syntax=docker/dockerfile:1.7

# Arguments with default value (for build).
ARG RUN_IMAGE=gcr.io/distroless/nodejs20-debian12
ARG PLATFORM=linux/amd64
ARG NODE_VERSION=20
ARG NODE_ENV=production

# @reference: https://nextjs.org/docs/pages/api-reference/next-config-js/output
ARG HOSTNAME=localhost
ARG PORT=3000

# Arguments for envars in runner step.
ARG NEXT_PUBLIC_SITE_URL

# -----------------------------------------------------------------------------
# This is base image with `pnpm` package manager
# Base image with pnpm package manager.
# -----------------------------------------------------------------------------
FROM node:${NODE_VERSION}-alpine AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN apk update && apk add --no-cache jq libc6-compat
RUN corepack enable && corepack prepare pnpm@latest-8 --activate
WORKDIR /app
FROM --platform=$PLATFORM node:${NODE_VERSION}-bookworm-slim AS base
ENV PNPM_HOME="/pnpm" PATH="$PNPM_HOME:$PATH" COREPACK_ENABLE_DOWNLOAD_PROMPT=0
ENV LEFTHOOK=0 PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=true NEXT_TELEMETRY_DISABLED=1
RUN corepack enable && corepack prepare pnpm@latest-9 --activate
WORKDIR /srv

# -----------------------------------------------------------------------------
# Build the application
# Install dependencies and some toolchains.
# -----------------------------------------------------------------------------
FROM base AS builder
ENV NEXT_TELEMETRY_DISABLED 1

ENV NEXT_PUBLIC_SITE_URL $NEXT_PUBLIC_SITE_URL
# Required for building the application.
ENV NEXT_PUBLIC_SITE_URL=http://localhost:3000

# Copy the source files
COPY --chown=node:node . .
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm add sharp
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --no-optional
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm build

# Install dependencies for production
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
RUN apt update && apt -yqq install tini jq
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install \
--ignore-scripts && pnpm add sharp && pnpm build

# -----------------------------------------------------------------------------
# Production image, copy build output files and run the application
# Compile the application and install production only dependencies.
# -----------------------------------------------------------------------------
FROM node:${NODE_VERSION}-alpine AS runner
LABEL org.opencontainers.image.source="https://github.com/riipandi/next-start"
FROM base AS pruner

ENV NEXT_PUBLIC_SITE_URL $NEXT_PUBLIC_SITE_URL
# Required generated files
COPY --from=builder /srv/.next/standalone /srv/.next/standalone
COPY --from=builder /srv/.next/static /srv/.next/standalone/.next/static
COPY --from=builder /srv/public /srv/.next/standalone/public

ENV HOSTNAME $HOSTNAME
ENV NODE_ENV $NODE_ENV
ENV PORT $PORT
# Required metadata files
COPY --from=builder /srv/package.json /srv/package.json
COPY --from=builder /srv/pnpm-lock.yaml /srv/pnpm-lock.yaml
COPY --from=builder /srv/next.config.mjs /srv/next.config.mjs
COPY --from=builder /srv/.npmrc /srv/.npmrc

WORKDIR /app
# Install production dependencies and cleanup node_modules.
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install \
--prod --frozen-lockfile --ignore-scripts && pnpm prune --prod \
--ignore-scripts && pnpm dlx clean-modules clean --directory \
".next/standalone/node_modules" --yes

# Don't run production as root, spawns command as a child process.
RUN addgroup --system --gid 1001 nonroot && adduser --system --uid 1001 nonroot
RUN apk update && apk add --no-cache tini
# -----------------------------------------------------------------------------
# Production image, copy build output files and run the application.
# -----------------------------------------------------------------------------
FROM --platform=$PLATFORM $RUN_IMAGE AS runner
LABEL org.opencontainers.image.source="https://github.com/riipandi/next-start"

# ----- Read application environment variables --------------------------------

ARG NEXT_PUBLIC_SITE_URL
ENV NEXT_PUBLIC_SITE_URL=$NEXT_PUBLIC_SITE_URL

# ----- Read application environment variables --------------------------------

# Copy the build output files from the pruner stage.
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nonroot:nonroot /app/.next/standalone ./
COPY --from=builder --chown=nonroot:nonroot /app/.next/static ./.next/static
COPY --from=builder --chown=nonroot:nonroot /app/public ./public
COPY --from=builder --chown=nonroot:nonroot /app/next.config.mjs .
# @ref: https://nextjs.org/docs/app/api-reference/next-config-js/output
COPY --from=pruner --chown=nonroot:nonroot /srv/.next/standalone /srv
COPY --from=pruner --chown=nonroot:nonroot /srv/next.config.mjs /srv/next.config.mjs

# Copy some utilities from builder image.
COPY --from=builder /usr/bin/tini /usr/bin/tini

# Define the host and port to listen on.
ARG NODE_ENV=production HOSTNAME=0.0.0.0 PORT=3000
ENV NEXT_TELEMETRY_DISABLED=1 TINI_SUBREAPER=true
ENV NODE_ENV=$NODE_ENV HOSTNAME=$HOST PORT=$PORT

WORKDIR /srv
USER nonroot:nonroot
EXPOSE $PORT

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "server.js"]
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/nodejs/bin/node", "/srv/server.js"]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<p align="center"><img src="./public/images/banner.svg" width="500" height="150" alt="Project Logo"></p>
<p align="center">
<a href="https://github.com/riipandi/next-start/pulse">
<img src="https://img.shields.io/badge/Contributions-welcome-blue.svg?style=flat-square" alt="Contribution welcome">
<img src="https://img.shields.io/badge/Contributions-welcome-blue.svg" alt="Contribution welcome">
</a>
<a href="https://github.com/riipandi/next-start">
<img src="https://img.shields.io/github/languages/top/riipandi/next-start?style=flat-square" alt="Top language">
<img src="https://img.shields.io/github/languages/top/riipandi/next-start" alt="Top language">
</a>
<a href="https://aris.mit-license.org">
<img src="https://img.shields.io/github/license/riipandi/next-start?style=flat-square" alt="License">
<img src="https://img.shields.io/github/license/riipandi/next-start" alt="License">
</a>
<a href="https://github.com/sponsors/riipandi">
<img src="https://badgen.net/badge/icon/sponsors?icon=github&label&color=green&labelColor=black&style=flat-square" alt="Sponsor" />
Expand Down Expand Up @@ -83,7 +83,7 @@ If you are using monorepo you will need to configure a few things:

#### Cloudflare Deployment

You need to add `NODE_VERSION` with value `18.16.0` or greater than version 18 on the environment variables setting.
You need to add `NODE_VERSION` with value `20.16.0` or greater than version 20 on the environment variables setting.
Visit [Cloudflare pages docs](https://developers.cloudflare.com/pages/platform/build-configuration/) for more information.

For the preview branch:
Expand Down
55 changes: 55 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json

services:
pgsql:
image: postgres:17-alpine
container_name: next_start_pgsql
hostname: postgres
restart: unless-stopped
ports:
- 5432:5432
volumes:
- pgsql_data:/var/lib/postgresql/data
- ./scripts/pgsql-multidb.sh:/docker-entrypoint-initdb.d/multidb.sh
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: securedb
POSTGRES_MULTIPLE_DATABASES: next_start,extra_db
command: ['postgres', '-c', 'wal_level=logical']
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres -d next_start']
interval: 30s
timeout: 20s
retries: 3

pgweb:
image: sosedoff/pgweb:latest
container_name: next_start_pgweb
restart: always
ports:
- 54321:8081
environment:
PGWEB_DATABASE_URL: 'postgres://postgres:securedb@pgsql:5432/next_start?sslmode=disable'
depends_on:
pgsql:
condition: service_started

mailpit:
image: axllent/mailpit:latest
container_name: next_start_mailpit
restart: unless-stopped
volumes:
- mailpit_data:/data
ports:
- 1025:1025 # port the SMTP server should be accessible on
- 8025:8025 # port the web interface should be accessible on
environment:
# https://github.com/axllent/mailpit/wiki/Runtime-options
# MP_UI_AUTH: 'user1:password1 user2:password2'
TZ: Asia/Jakarta

volumes:
pgsql_data:
driver: local
mailpit_data:
driver: local
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
13 changes: 5 additions & 8 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ const isProduction = process.env.NODE_ENV === 'production' || process.env.IS_VER

/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [{ protocol: 'https', hostname: '**' }],
},
output: 'standalone',
reactStrictMode: true,
poweredByHeader: false,
output: 'standalone',
cleanDistDir: true,
images: { remotePatterns: [{ protocol: 'https', hostname: '**' }] },
// @ref: https://nextjs.org/blog/next-14-1#improved-self-hosting
// cacheHandler: require.resolve('./cache-handler.js'),
// cacheMaxMemorySize: 0, // disable default in-memory caching
eslint: { ignoreDuringBuilds: isProduction },
typescript: { ignoreBuildErrors: isProduction },
logging: {
fetches: { fullUrl: true },
},
logging: { fetches: { fullUrl: true } },
rewrites() {
return [{ source: '/health', destination: '/api/health' }]
return [{ source: '/healthz', destination: '/api/healthz' }]
},
}

Expand Down
Loading

0 comments on commit f4e5f78

Please sign in to comment.