Skip to content

Commit

Permalink
added standalone test for NextJS, making the image much smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Annie Sexton authored and Annie Sexton committed Apr 16, 2024
1 parent ab1e663 commit e21ac3b
Show file tree
Hide file tree
Showing 7 changed files with 5,211 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ export class GDF {
return !!this.#pj.dependencies?.next
}

get standaloneNextjs() {
if (!this.nextjs) return false;

Check failure on line 158 in gdf.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon

if (fs.existsSync(path.join(this._appdir, `next.config.mjs`))) {

Check failure on line 160 in gdf.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote
const config = fs.readFileSync(path.join(this._appdir, `next.config.mjs`), 'utf-8')

Check failure on line 161 in gdf.js

View workflow job for this annotation

GitHub Actions / test

Strings must use singlequote
return /output\s*:\s*(["'`])standalone\1/.test(config)
} else return false;

Check failure on line 163 in gdf.js

View workflow job for this annotation

GitHub Actions / test

Extra semicolon
}

// Does this application use nuxt.js?
get nuxtjs() {
return !!this.#pj.dependencies?.nuxt
Expand Down
6 changes: 6 additions & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ COPY --from=build /app/dist /app/dist
ENV PORT=4321
ENV HOST=0.0.0.0
<% } else if (standaloneNextjs) { -%>
COPY --from=build /app/.next/standalone /app
COPY --from=build /app/.next/static /app/.next/static
COPY --from=build /app/public /app/public
<% } else { -%>
COPY --from=build /app /app
<% } -%>
Expand Down Expand Up @@ -233,6 +237,8 @@ EXPOSE <%= port %>
<% } -%>
<% if (foreman) { -%>
CMD [ "<%= npx %>", "foreman", "start", "--procfile", "Procfile.prod" ]
<% } else if (standaloneNextjs) { -%>
CMD [ "node", "server.js" ]
<% } else if (Array.isArray(startCommand)) { -%>
CMD <%- JSON.stringify(startCommand, null, 1).replaceAll(/\n\s*/g, " ") %>
<% } else { -%>
Expand Down
36 changes: 36 additions & 0 deletions test/frameworks/next-standalone/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
47 changes: 47 additions & 0 deletions test/frameworks/next-standalone/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=20.9.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Next.js"

# Next.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY --link . .

# Build application
RUN npm run build

# Remove development dependencies
RUN npm prune --omit=dev


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app/.next/standalone /app
COPY --from=build /app/.next/static /app/.next/static
COPY --from=build /app/public /app/public

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "node", "server.js" ]
6 changes: 6 additions & 0 deletions test/frameworks/next-standalone/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: "standalone"
};

export default nextConfig;
Loading

0 comments on commit e21ac3b

Please sign in to comment.