Skip to content

Commit

Permalink
Added standalone test for NextJS, making the image much smaller (#59)
Browse files Browse the repository at this point in the history
* added standalone test for NextJS, making the image much smaller

* added linting fixes

* handle next.config.js too

* whoops, fix wonky indentation

---------

Co-authored-by: Annie Sexton <[email protected]>
Co-authored-by: Sam Ruby <[email protected]>
  • Loading branch information
3 people authored Apr 23, 2024
1 parent 22dc04e commit 177c820
Show file tree
Hide file tree
Showing 7 changed files with 5,214 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ export class GDF {
return !!this.#pj.dependencies?.next
}

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

if (fs.existsSync(path.join(this._appdir, 'next.config.mjs'))) {
const config = fs.readFileSync(path.join(this._appdir, 'next.config.mjs'), 'utf-8')
return /output\s*:\s*(["'`])standalone\1/.test(config)
} else if (fs.existsSync(path.join(this._appdir, 'next.config.js'))) {
const config = fs.readFileSync(path.join(this._appdir, 'next.config.js'), 'utf-8')
return /output\s*:\s*(["'`])standalone\1/.test(config)
} else return false
}

// 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 @@ -142,6 +142,10 @@ ENV PORT=4321
ENV HOST=0.0.0.0
<% } else if (svelte) { -%>
COPY --from=build /app/build /app/build
<% } 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 @@ -235,6 +239,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 177c820

Please sign in to comment.