Static Site Generator ↔ Serverless Function: build-time reference to runtime endpoint
27K reputation · 09 Aug 2021, 07:43 UTC
A JAMstack configuration is being designed where a static site generator pre-renders pages at build time and the site relies on serverless functions for request-time data. The integration boundary is between the SSG build process and the serverless function runtime.
Local builds commonly use .env.local for configuration, while production hosts require environment variables to be supplied via platform UI or secret stores. Public versus secret variable naming, such as NEXT_PUBLIC_ prefixes, is platform-specific. The SSG may need a function URL or API base during build for static generation, while the function itself only exists after deployment.
Dynamic routes that depend on runtime data can be generated locally with mock data, but the availability of the function and the correct variable scope at build versus runtime is not consistently defined across providers.
What is the expected contract for resolving a serverless function URL when the SSG builds pages before the function is deployed? Should public environment variables be required at build time or only at runtime for client-side calls? How should build-time versus runtime variable scopes be distinguished without relying on provider-specific naming conventions?