Skip to content

Commit

Permalink
Added support for Astro SSR using the node adapter (#52)
Browse files Browse the repository at this point in the history
* Added support for Astro SSR using the node adapter

* bump to 0.5.2 for Astro SSR support

* added tests for astro

---------

Co-authored-by: Annie Sexton <[email protected]>
Co-authored-by: Annie Sexton <[email protected]>
  • Loading branch information
3 people authored Feb 9, 2024
1 parent ad1832b commit ab1e663
Show file tree
Hide file tree
Showing 11 changed files with 13,247 additions and 5 deletions.
16 changes: 14 additions & 2 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ export class GDF {
return !!(this.#pj.dependencies?.astro)
}

get astroSSR() {
return !!(this.#pj.dependencies?.astro) &&
!!(this.#pj.dependencies?.['@astrojs/node'])
}

get astroStatic() {
return this.astro && !this.astroSSR
}

get vite() {
return !!(this.#pj.scripts?.dev === 'vite')
}
Expand Down Expand Up @@ -675,8 +684,10 @@ export class GDF {

if (this.gatsby) {
return [this.npx, 'gatsby', 'serve', '-H', '0.0.0.0']
} else if (this.vite || this.astro) {
} else if (this.vite || this.astroStatic) {
return ['/usr/sbin/nginx', '-g', 'daemon off;']
} else if (this.astroSSR) {
return ['node', './dist/server/entry.mjs']
} else if (this.runtime === 'Node.js' && this.#pj.scripts?.start?.includes('fastify')) {
let start = this.#pj.scripts.start
if (!start.includes('-a') && !start.includes('--address')) {
Expand Down Expand Up @@ -752,7 +763,8 @@ export class GDF {
let port = 3000

if (this.gatsby) port = 8080
if (this.runtime === 'Vite' || this.astro) port = 80
if (this.runtime === 'Vite' || this.astroStatic) port = 80
if (this.astroSSR) port = 4321

return port
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flydotio/dockerfile",
"version": "0.5.1",
"version": "0.5.2",
"description": "Dockerfile generator",
"main": "./index.js",
"bin": {
Expand Down
10 changes: 8 additions & 2 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ WORKDIR /app
# Set production environment
<%- emitEnv(baseEnv) %>
<% } else if (vite || astro) { -%>
<% } else if (vite || astroStatic) { -%>
FROM nginx
<% } else { -%>
FROM base
Expand All @@ -132,8 +132,14 @@ RUN <% if (options.cache) { %><%= pkg_cache %>
<% } -%>
# Copy built application
<% if (vite || astro) { -%>
<% if (vite || astroStatic) { -%>
COPY --from=build /app/dist /usr/share/nginx/html
<% } else if (astroSSR) { -%>
COPY --from=build /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist
ENV PORT=4321
ENV HOST=0.0.0.0
<% } else { -%>
COPY --from=build /app /app
<% } -%>
Expand Down
21 changes: 21 additions & 0 deletions test/frameworks/astro-ssr/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# build output
dist/

# generated types
.astro/

# dependencies
node_modules/

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

# environment variables
.env
.env.production

# macOS-specific files
.DS_Store
46 changes: 46 additions & 0 deletions test/frameworks/astro-ssr/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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="Astro"

# Astro 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

# Copy application code
COPY --link . .

# Build application
RUN npm run build


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist

ENV PORT=4321
ENV HOST=0.0.0.0

# Start the server by default, this can be overwritten at runtime
EXPOSE 4321
CMD [ "node", "./dist/server/entry.mjs" ]
Loading

0 comments on commit ab1e663

Please sign in to comment.