Fix Astro.url.protocol
when using the @astrojs/node SSR adapter with HTTPS
#5992
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.
Changes
This PR fixes #5890.
When using Astro with the
@astrojs/node
SSR adapter,Astro.url.protocol
is alwayshttp:
even when the server is running with HTTPS.When creating the request used during rendering based on the Node.js request, the protocol is currently hardcoded to
http:
.This pull request fixes this issue by checking if the Node.js request is using HTTPS and updating the protocol accordingly. To do so, we check if the request socket is either an instance of
tls.TLSSocket
or if theX-Forwarded-Proto
header is set tohttps
.While testing this, I also realised that when starting the server, the console is always logging a URL with the HTTP protocol, e.g.
Server listening on http://127.0.0.1:3000
. I decided to include a small change in this PR to log the proper protocol by checking if the server is an instance ofhttps.Server
or not. I thought this was not worth a different PR or even mentioning it in the changeset but maybe I was wrong?Testing
I added new
@astrojs/node
tests in aurl-protocol
suite using a fixture rendering a page withAstro.url.protocol
and checking the rendered HTML based on the protocol of the Node.js request.I also manually tested locally this change in a local repro using the
file:
pnpm protocol and confirmed that loggingAstro.url.protocol
correctly returnhttps:
when running withSERVER_KEY_PATH=./key.pem SERVER_CERT_PATH=./cert.pem node ./dist/server/entry.mjs
Docs
I don't think this change needs to be documented.