Skip to content

fix: switch getStaticProps to getServerSideProps to avoid build-time MongoDB errors#49

Merged
choden-dev merged 5 commits into
mainfrom
fix/ssr-mongodb-build-env
Apr 17, 2026
Merged

fix: switch getStaticProps to getServerSideProps to avoid build-time MongoDB errors#49
choden-dev merged 5 commits into
mainfrom
fix/ssr-mongodb-build-env

Conversation

@choden-dev

Copy link
Copy Markdown
Owner

Problem

Pages using getStaticProps (contact.tsx, index.tsx) attempted to connect to MongoDB during pnpm build inside the Docker builder stage. At build time, DATABASE_URI is not available — env vars are only injected at container runtime by the Cloudflare Worker (container-worker.js).

This caused the deployment to fail with:

Error: cannot connect to MongoDB. Details: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"

Solution

  • pages/contact.tsx: getStaticPropsgetServerSideProps
  • pages/index.tsx: getStaticPropsgetServerSideProps
  • Added Cache-Control headers (s-maxage + stale-while-revalidate) to preserve CDN caching equivalent to the old revalidate intervals:
    • Contact: 24 hours (was revalidate: 86400)
    • Index: 1 hour (was revalidate: 3600)

Why this works

getStaticProps runs at build time (no env vars in Docker builder) and then revalidates at runtime.
getServerSideProps runs only at request time (env vars available via container-worker.js → container runtime).

The Cache-Control headers ensure Cloudflare's CDN caches the responses, so performance is equivalent to the old ISR setup.

- Import env from 'cloudflare:workers' at module level
- Convert envVars from constructor assignment to class field
- Remove constructor override, simplifying the Container subclass
…MongoDB errors

Pages using getStaticProps attempted to connect to MongoDB during
`pnpm build` inside the Docker builder stage, where DATABASE_URI is
not available (env vars are only injected at container runtime by the
Cloudflare Worker).

- contact.tsx, index.tsx: getStaticProps → getServerSideProps
- Added Cache-Control headers (s-maxage + stale-while-revalidate)
  to preserve CDN caching behaviour equivalent to the old revalidate
  intervals (24h for contact, 1h for index).
@netlify

netlify Bot commented Apr 17, 2026

Copy link
Copy Markdown

Deploy Preview for stalwart-otter-3d2436 ready!

Name Link
🔨 Latest commit 60beebf
🔍 Latest deploy log https://app.netlify.com/projects/stalwart-otter-3d2436/deploys/69e1fa8404b392000886e91a
😎 Deploy Preview https://deploy-preview-49--stalwart-otter-3d2436.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@choden-dev
choden-dev merged commit 549c807 into main Apr 17, 2026
2 checks passed
@choden-dev
choden-dev deleted the fix/ssr-mongodb-build-env branch April 17, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant