-
Notifications
You must be signed in to change notification settings - Fork 22
NEX-195: Allow the frontend to access the backend via internal url in runtime #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…main * for now in silta it's set to the same as the external one
…uild, but then the internal one at runtime
…uild, but then the internal one at runtime - new try
…uild, but then the internal one at runtime - third try
…llow images from the internal url in silta while building in circleci
…a container as well.
I liked also this explanation from Slack thread:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, not the greatest at configs but this looks clear enough. Where exactly do we retrieve and see these internal urls? It would be great to document this with a short readme eventually.
Fixed in b5cfb18 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach here is now working nicely on a live site too! A few comments from that implementation to consider, then this should be good to go
DRUPAL_BASE_URL_INTERNAL: http://appserver_nginx.nextdrupalstarterkit.internal | ||
DRUPAL_BASE_URL_INTERNAL_IMAGES: http://appserver_nginx.nextdrupalstarterkit.internal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In lando also just http://appserver_nginx
should work, I don't think the .nextdrupalstarterkit.internal
part is needed. I suppose it's one less thing that would need to be changed later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 339c79a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi! unfortunately with this change the lando setup did not work anymore (could not get data from the backend). I reverted it in 4fc3bfb and now it works again.
next/next.config.mjs
Outdated
}, | ||
{ | ||
protocol: imageHostnameInternal[0] === "https" ? "https" : "http", | ||
hostname: imageHostnameInternal[1], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In another project, we kept logic for this in one place like this:
remotePatterns: [
process.env.DRUPAL_BASE_URL_INTERNAL_IMAGES,
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
].map((url = "") => {
const [protocol, hostname] = url.split("://");
if (!hostname || (protocol !== "https" && protocol !== "http")) {
throw new Error(`Invalid images URL "${url}" in next.config.ts`);
}
return {
protocol,
hostname,
pathname: "**",
};
}),
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 70bd4b6
@@ -34,7 +34,7 @@ const options: HTMLReactParserOptions = { | |||
if (isRelative(src)) { | |||
return ( | |||
<Image | |||
src={`${env.NEXT_PUBLIC_DRUPAL_BASE_URL}${src}`} | |||
src={`${env.DRUPAL_BASE_URL_INTERNAL}${src}`} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we should actually use NEXT_PUBLIC_DRUPAL_BASE_URL
, or are we 100% sure this component will only ever load from the server?
In another project (pages router but otherwise very similar) where this line in FormattedText caused an error "Attempted to access a server-side environmental variable on the client", so had to change this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 035ebbe
This avoids the need for projects to update the value later
This avoids the possibility of trying to access a server env var on the client
This PR introduces the concept of an internal url to access drupal from the frontend.
The backend needs to be accessible by a public URL for these reasons:
But, it would be useful for the node.js process running in next.js to access the backend also with an internal URL. In SILTA, this means that the data flows inside the same cluster, and does not involve our CDN.
One thing that we had to take into consideration was the fact that we are using
next-image
, and both the internal and external urls need to be allowed as source for the images.I have added the same concept to the lando and ddev setups, because hopefully this would allow us to catch subtle bugs in case we are using the wrong internal/external url for drupal there.
Summary of the changes:
Configuration Updates:
.circleci/config.yml
: Added environment variablesDRUPAL_BASE_URL_INTERNAL
andDRUPAL_BASE_URL_INTERNAL_IMAGES
to handle internal URLs and ensure they are used within CircleCI. [1] [2] [3].ddev/config.yaml
: AddedDRUPAL_BASE_URL_INTERNAL
andDRUPAL_BASE_URL_INTERNAL_IMAGES
for local development..lando.yml
: AddedDRUPAL_BASE_URL_INTERNAL
andDRUPAL_BASE_URL_INTERNAL_IMAGES
for Lando environments.silta/silta-next.yml
: AddedDRUPAL_BASE_URL_INTERNAL
andDRUPAL_BASE_URL_INTERNAL_IMAGES
for Silta environments.Code Updates:
next/next.config.mjs
: Updated the image hostname handling to use both external and internal Drupal URLs, ensuring valid URLs are provided. [1] [2]next/src/app/api/health/route.ts
: Changed the health check endpoint to use the internal Drupal URL.next/src/auth.ts
: Updated OAuth token fetching to use the internal Drupal URL. [1] [2]next/src/components/formatted-text.tsx
: Modified image source URLs to use the internal Drupal URL.next/src/env.ts
: Added internal Drupal URL environment variables to the environment configuration. [1] [2]next/src/lib/drupal/drupal-client.ts
: Updated the GraphQL client to use the internal Drupal URL.