Skip to content
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

Support --host for Node adapter preview #6928

Merged
merged 20 commits into from
Aug 10, 2023

Conversation

JerryWu1234
Copy link
Contributor

@JerryWu1234 JerryWu1234 commented Apr 28, 2023

Changes

Testing

Tested manually

Docs

Bug fix! We already say that the server.host setting changes astro dev and astro preview behavior.

@changeset-bot
Copy link

changeset-bot bot commented Apr 28, 2023

🦋 Changeset detected

Latest commit: 982a478

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

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) labels Apr 28, 2023
@bluwy
Copy link
Member

bluwy commented Apr 28, 2023

I don't think this is the right fix, otherwise this PR makes --host only listens to one network host, instead of all available network host. Plus it gives preference to ipv4 only too. I think the issue is with the logging, and you should be able to get the information after the server starts with server.server.address() and log with that information.

@JerryWu1234
Copy link
Contributor Author

JerryWu1234 commented Apr 28, 2023

I don't think this is the right fix, otherwise this PR makes --host only listens to one network host, instead of all available network host. Plus it gives preference to ipv4 only too. I think the issue is with the logging, and you should be able to get the information after the server starts with server.server.address() and log with that information.

server.server.address() will return an undefined.

https://github.com/wulinsheng123/astro/blob/2a33667e94ba60dd40322ad468762743dc001c15/packages/integrations/node/src/http-server.ts#L17
This host always gets undefined when we have passed --host. in nodejs adapter
https://github.com/wulinsheng123/astro/blob/2a33667e94ba60dd40322ad468762743dc001c15/packages/astro/src/core/preview/util.ts#L3
because this function only returns undefined to host.
so this createserver can't get right way

So I even pass a network address

I wanted to do like Vite https://github.com/vitejs/vite/blob/447df7cba987b30e3621076a74e2227f8232f64a/packages/vite/src/node/utils.ts#L803
But it was asynchronous to get address

@bluwy
Copy link
Member

bluwy commented May 17, 2023

Sorry for the late reply. When I log before here with console.log(server.server.address()), I got this though:

> astro preview "--host"

{ address: '::', family: 'IPv6', port: 3000 }
Preview server listening on http://undefined:3000

nodejs also documented it here so I'm not sure why it isn't working. But either way I think we should still leave host undefined so the server listens on all active connections rather than a specific one. We can move the fix solely in the nodejs integration only.

Maybe we can re-implement how Vite does it here?

@JerryWu1234
Copy link
Contributor Author

Sorry for the late reply. When I log before here with console.log(server.server.address()), I got this though:

> astro preview "--host"

{ address: '::', family: 'IPv6', port: 3000 }
Preview server listening on http://undefined:3000

nodejs also documented it here so I'm not sure why it isn't working. But either way I think we should still leave host undefined so the server listens on all active connections rather than a specific one. We can move the fix solely in the nodejs integration only.

Maybe we can re-implement how Vite does it here?

actually, I simulate the code of Vite.
Let me conclude.

  1. move the code to the node adapter
  2. re-implement code that got a network address

@github-actions github-actions bot removed the pkg: astro Related to the core `astro` package (scope) label May 19, 2023
@JerryWu1234 JerryWu1234 marked this pull request as draft May 20, 2023 00:38
@JerryWu1234
Copy link
Contributor Author

image

@JerryWu1234
Copy link
Contributor Author

image

@JerryWu1234 JerryWu1234 marked this pull request as ready for review May 23, 2023 03:31
@JerryWu1234
Copy link
Contributor Author

image

@JerryWu1234
Copy link
Contributor Author

image

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) and removed pkg: astro Related to the core `astro` package (scope) labels May 24, 2023
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code changes look alright to me, but I'm not familiar enough with Node's networking logic to know for sure if this is the correct approach.

Would love @bluwy to re-review!

@natemoo-re natemoo-re changed the title supporting a network address access a website when an user set host =… Support --host for Node adapter preview Aug 10, 2023
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up the formatting, LGTM!

@natemoo-re natemoo-re mentioned this pull request Aug 10, 2023
1 task
@natemoo-re natemoo-re merged commit b16cb78 into withastro:main Aug 10, 2023
@astrobot-houston astrobot-houston mentioned this pull request Aug 10, 2023
@beltonk
Copy link

beltonk commented May 21, 2024

astro preview "--host" is not working
npm run preview -- --host
also does not expose host

ematipico pushed a commit that referenced this pull request Feb 5, 2025
* supporting a network address access a website when an user set host = true in Node environment

* fix bug

* sumbit test code

* optimism

* delect white space

* test

* fix test

* fix test error

* test

* test

* test

* fix test error

* Optimizing code based on the comments

* optimize test

* fix: rebase issues

* chore: format

* chore: add changeset

* chore: format

* chore: format

* chore: lint

---------

Co-authored-by: wuls <[email protected]>
Co-authored-by: Nate Moore <[email protected]>
ematipico pushed a commit that referenced this pull request Feb 5, 2025
* supporting a network address access a website when an user set host = true in Node environment

* fix bug

* sumbit test code

* optimism

* delect white space

* test

* fix test

* fix test error

* test

* test

* test

* fix test error

* Optimizing code based on the comments

* optimize test

* fix: rebase issues

* chore: format

* chore: add changeset

* chore: format

* chore: format

* chore: lint

---------

Co-authored-by: wuls <[email protected]>
Co-authored-by: Nate Moore <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

host: true with Node adapter
5 participants