Skip to content

Conversation

@eduardoboucas
Copy link
Member

@eduardoboucas eduardoboucas commented Jun 17, 2025

Currently, NetlifyDev accepts a serverAddress option in the constructor. When used in the Vite plugin, this property needs to match the address of the Vite server. This is a problem in some Vite-based frameworks (like Astro), because when we initialise the plugin, the server isn't running yet, so we don't know what port it uses.

With this PR, we're able to defer the definition of serverAddress to the handle method. At that point, we know for a fact that the Vite server is already running, so we grab the port and set it on the handle options.

I have kept the serverAddress option in the constructor for backwards-compatibility; if both are supplied, the one in handle takes precedence.

@eduardoboucas eduardoboucas requested a review from a team as a code owner June 17, 2025 12:29

next()
})
logger.log(`Middleware loaded. Emulating features: ${netlifyDev.getEnabledFeatures().join(', ')}.`)
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see the value of this. It won't mean anything to anyone using the Vite plugin through a meta-framework.

Copy link
Member

Choose a reason for hiding this comment

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

Could you move this change to a separate PR so we can discuss it separately?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@serhalp serhalp left a comment

Choose a reason for hiding this comment

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

LGTM - just would rather decouple the logging change

Comment on lines +125 to +130
/**
* If your local development setup has its own HTTP server (e.g. Vite), you
* can supply its address here. It will override any value defined in the
* top-level `serverAddress` setting.
*/
serverAddress?: string
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: document in packages/dev/README.md

}

if (this.#server instanceof HTTPServer) {
return await this.#server.start()
Copy link
Member

Choose a reason for hiding this comment

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

question: it's surprising that a getServerAddress method has the behaviour/side-effect of starting a server. is there an easy way to avoid that?

e.g. I would expect that it would be idempotent but it will throw on subsequent calls (I think)

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed. i wonder if HTTPServer should have a .serving() promise to await here, and #server.start() should be run somewhere else? or, if this is definitely the right place then perhaps a more dramatic verb than get

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't actually need to start a server in getServerAddress. I think it may have been a transient state while developing this that I forgot to clean up.

Fixed in ecef1bf.

@eduardoboucas eduardoboucas merged commit fa811f2 into main Jun 17, 2025
9 of 10 checks passed
@eduardoboucas eduardoboucas deleted the feat/astro branch June 17, 2025 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants