Fix Astro.url.protocol when using the @astrojs/node SSR adapter with HTTPS#5992
Merged
matthewp merged 1 commit intowithastro:mainfrom Jan 26, 2023
Merged
Fix Astro.url.protocol when using the @astrojs/node SSR adapter with HTTPS#5992matthewp merged 1 commit intowithastro:mainfrom
Astro.url.protocol when using the @astrojs/node SSR adapter with HTTPS#5992matthewp merged 1 commit intowithastro:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 299ad49 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
matthewp
approved these changes
Jan 26, 2023
Contributor
matthewp
left a comment
There was a problem hiding this comment.
This is a fantastic PR. thank you!
Merged
ematipico
pushed a commit
that referenced
this pull request
Feb 5, 2025
ematipico
pushed a commit
that referenced
this pull request
Feb 5, 2025
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.
Changes
This PR fixes #5890.
When using Astro with the
@astrojs/nodeSSR adapter,Astro.url.protocolis 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.TLSSocketor if theX-Forwarded-Protoheader 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.Serveror 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/nodetests in aurl-protocolsuite using a fixture rendering a page withAstro.url.protocoland 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.protocolcorrectly returnhttps:when running withSERVER_KEY_PATH=./key.pem SERVER_CERT_PATH=./cert.pem node ./dist/server/entry.mjsDocs
I don't think this change needs to be documented.