Preview and localhost resolution behavior

When browsing http://localhost:8080 in Preview, my local machine is accessed.
Is there a way to have it resolve to the theia container?

(Apologize if I mixing up something :D)

[original thread by Julien Eluard]

Do you mini-browser? it is iframe which relies on your browser to resolve urls. @kittaakos can correct me

[Julien Eluard]

Not sure which package it’s coming from, the Preview panel you get when using Open URL command.
It’s a bit unexpected for end users to have localhost resolved as expected in the terminal, but not while opening a URL.

[Julien Eluard]

Is that something that could be modified?

It could be done by hooking into fetch event inside of iframe and resolving it customly. You will need a custom extension which can hook in mini-browser iframe. I’m not sure whether it can be possible now. MiniBrowserContent is responsible to create such iframe: https://github.com/eclipse-theia/theia/blob/master/packages/mini-browser/src/browser/mini-browser-content.ts#L163

You can rebind it to your custom implementation.

about fetch event: https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent

maybe there is an easier way, i don’t know

[Julien Eluard]

Thanks! I will take a look

I am not sure if I understood the requirements correctly. Still, if you want to open http://your.theia.container.address:8080 instead of http://localhost:8080, when the user enters a location for the Open URL command, I would instead register a LocationMapper contribution that resolves to a higher priority than the “built-in” HttpLocationMapper. Note, there is a HttpsLocationMapper as well, although I do not know your use-case.

LocationMapper API: https://github.com/eclipse-theia/theia/blob/ec16330ba6934a307c15fa66e64c3dda9f749743/packages/mini-browser/src/browser/location-mapper-service.ts#L27-L41

[Julien Eluard]

Thanks, that sounds helpful!

My scenario is the following: I manage a fleet of remote theia containers (think gitpod.io). Users will launch local webservers there. It’s natural then to use the mini-browser to access it via localhost, similarly to accessing localhost via the terminal.

Is there some lower level API that would enable the same but globally? Say I am using another plugin that has the same behavior. I’d like to hook all localhost usages.

Probably such extension also can make use of LocationMapper then?

[Julien Eluard]

@kittaakos Sorry for the naïve question: how do you register with a higher priority?

Is there some lower level API that would enable the same but globally? Say I am using another plugin that has the same behavior. I’d like to hook all localhost usages.

BTW we have now ExternalUriService which should be implemented to map localhost to external URIs, Theia-based products can benefit by implementing it and then just use OpenerService to open URI if it is a URI to localhost then it will be properly mapped before opening, i.e. like in this PR to open localhost links from terminals: https://github.com/eclipse-theia/theia/pull/6663

For the mini-browser there is an idea to reimplement it with webviews: https://github.com/eclipse-theia/theia/issues/6562 then it will respect this service as well, but i don’t know whether we will go with it

[Julien Eluard]

Thanks @anton-kosyakov and @kittaakos !