feat: use private network to call API in SSR#1097
Open
ThibaudDauce wants to merge 2 commits into
Open
Conversation
nicolaskempf57
approved these changes
May 28, 2026
Contributor
nicolaskempf57
left a comment
There was a problem hiding this comment.
Seems great ! Can we test it on demo before merging ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Configuration
This is enabled by a single optional environment variable:
NUXT_API_BASE_PRIVATE_NETWORK— the private-network API base used for server-side(SSR / Nitro) requests, e.g.
http://udata-api:7000. It maps to the privateruntimeConfig.apiBasePrivateNetworkkey (camelCase →NUXT_-prefixedSCREAMING_SNAKE_CASE).
When it is unset (default, including dev and CI), behaviour is unchanged: server-side
requests fall back to
NUXT_PUBLIC_API_BASE. Set it only in production to route SSRtraffic through the internal network. The browser is never affected — client-side requests
keep using
NUXT_PUBLIC_API_BASE(which is also why this key is private and not exposed tothe client).
Known limitations
Only calls made through the shared
$apiclient (anduseAPI, which builds on it) arerouted to the private-network base during SSR. datagouv-components is configured with
customUseFetch: useAPI(seeapp.vue), so its server-side data.gouv API calls arecovered as well.
Code that creates its own
$fetch/ofetchinstance instead of using$api/useAPIis not rewritten and would still hit the public API. Today this is limited to
$chartsApi(ChartConfigurator.vue) and a couple of CSV-export / tabular helpers indatagouv-components — all triggered by client interaction, so there is no SSR leak in
practice. New server-rendered code should go through
$api/useAPI.Implementation note
The rewrite runs in ofetch's
onRequesthook and relies on ofetch applyingonRequestbefore
baseURL(verified with ofetch 1.5.1 / ufo 1.6.3, wherewithBaseleavesabsolute URLs untouched). This ordering is an internal detail of ofetch, not part of its
public contract, so it could change on a major upgrade.