Running a Content SDK app behind a corporate proxy
Running a Content SDK app behind a corporate network requires handling outbound proxies, custom certificate authorities, and stricter runtime controls. There are three methods for running a Content SDK app behind a corporate proxy:
-
Configure Node.js at the environment level
-
Initialize proxy behavior early with an instrumentation file
-
Customize the
SitecoreClientdirectly
Use Node.js environment configuration (recommended)
See the official guide from Node.js to configure enterprise networks. You can add the environment variables to the dev script inside package.json or to the .env file.
See the following example to update the dev script:
Use instrumentation with Undici
Another option is to initialize proxy behavior during server startup using Next.js instrumentation. Next.js supports adding an instrumentation.ts|js file at the project root. It contains a register function that runs once when the server starts. This makes it ideal to set up runtime-wide networking behavior before your app begins handling requests.
See the following example of an instrumentation.ts file:
The register() function runs when the server starts. In this implementation, it only applies proxy configuration in development, based on the NODE_ENV check.
It reads proxy settings from standard environment variables (HTTPS_PROXY or HTTP_PROXY) and, if configured, creates a proxy dispatcher using undici. Calling setGlobalDispatcher(dispatcher) sets this proxy agent as the default dispatcher, ensuring that outgoing requests, including those made with Node.js fetch, are routed through the proxy.
The call to install() ensures that the global fetch implementation is provided by undici and uses the configured dispatcher consistently across the application.
Once you have the instrumentation file, you can add the register function in your sitecore.cli.config.ts file. This allows the CLI to use the proxied fetch from undici.
Ensure that you consider any customizations you may have in your existing implementation when using the proxied fetch from undici.
Customizing SitecoreClient
See Customizing SitecoreClient for more information on extending the default SitecoreClient behavior.