lorca icon indicating copy to clipboard operation
lorca copied to clipboard

how to load local index.html?

Open rluisr opened this issue 5 years ago • 10 comments

use lorca with vue which is build by webpack. Is there any way to load local index.html from lorca?

rluisr avatar Apr 28 '19 15:04 rluisr

During development, you can point lorca to the localhost server like this:

ui.Load("http://localhost:8080")

If you want to serve the application built with npm run build, you can start a local server from Go like this:

// Start listener
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
	log.Fatal(err)
}
defer ln.Close()

// Serve 'dist' directory created when building your project
go http.Serve(ln, http.FileServer(http.Dir("/path/to/your/project/dist")))

// Load local server; this will display 'index.html' in the browser
ui.Load(fmt.Sprintf("http://%s", ln.Addr()))

If you want to build a single binary from Go that embeds your Vue client, you should take a look at the counter example in this repository.

velut avatar May 14 '19 17:05 velut

Is there an easy way to 'inject' this npm (webpack, brunch, etc.) generated code with the eval-function (like the description in the readme)?

AxelRHD avatar Jun 17 '19 09:06 AxelRHD

@AxelRHD I did not completely understand your question, but if you have a all-in-one HTML file (containing the necessary html, js, assets, ...) generated by npm you can load it at startup like this

myFileContents := // read your file here somehow

// Make your file contents loadable as html
loadableContents := "data:text/html," + url.PathEscape(myFileContents)

// Open Chrome and load your page into it.
ui, err := lorca.New(loadableContents, "", 1280, 720)

You can also instantiate lorca first and load the file later like this

// Start lorca; this opens Chrome with a blank page.
ui, err := lorca.New("", "", 1280, 720)

// Do something, like binding your go code to js.
// ...

// Finally load your HTML file, which will have access to the previous bindings.
myFileContents := // read your file here somehow

// Make your file contents loadable as html
loadableContents := "data:text/html," + url.PathEscape(myFileContents)

// Load your HTML file in Chrome; this replaces the blank page with your page.
err := ui.Load(loadableContents)

If you do not want to read a file from disk, you can embed it as a string like so

myFileContents := `<html><body>Hello</body></html>`

// Continue as before...

I still think it's better to start a local server for your dist directory or embed its contents like shown in the counter example in this repository (see my previous post in this issue).

velut avatar Jun 17 '19 10:06 velut

@AxelRHD I did not completely understand your question, but if you have a all-in-one HTML file (containing the necessary html, js, assets, ...) generated by npm you can load it at startup like this

myFileContents := // read your file here somehow

// Make your file contents loadable as html
loadableContents := "data:text/html," + url.PathEscape(myFileContents)

// Open Chrome and load your page into it.
ui, err := lorca.New(loadableContents, "", 1280, 720)

You can also instantiate lorca first and load the file later like this

// Start lorca; this opens Chrome with a blank page.
ui, err := lorca.New("", "", 1280, 720)

// Do something, like binding your go code to js.
// ...

// Finally load your HTML file, which will have access to the previous bindings.
myFileContents := // read your file here somehow

// Make your file contents loadable as html
loadableContents := "data:text/html," + url.PathEscape(myFileContents)

// Load your HTML file in Chrome; this replaces the blank page with your page.
err := ui.Load(loadableContents)

If you do not want to read a file from disk, you can embed it as a string like so

myFileContents := `<html><body>Hello</body></html>`

// Continue as before...

I still think it's better to start a local server for your dist directory or embed its contents like shown in the counter example in this repository (see my previous post in this issue).

I was just wondering if there is a "clean" possibility to inject the compiled (webpack, brunch, etc.) js and css files instead of linking them in the html file over a webserver. So I understand that the local webserver is the best way to work with that bundled stuff.

AxelRHD avatar Jun 18 '19 09:06 AxelRHD

I think the issue is probably with way content is displayed using data URI , which should work if you read your built index.html with the bundled js but not sure if everything will work the way you want.

vinchauhan avatar Jun 19 '19 07:06 vinchauhan

I am thinking about a "global" http server for JS and CSS content so I can run the client software without an own server. Just the HTML basic file injected to the UI.

AxelRHD avatar Jun 19 '19 08:06 AxelRHD

@AxelRHD Is there a reason for not using a local http server (one started by the same binary that starts lorca)?

velut avatar Jun 19 '19 23:06 velut

@AxelRHD Is there a reason for not using a local http server (one started by the same binary that starts lorca)?

No, it just would have been a prove of concept. I will proceed with a local webserver. Thanks for the support.

AxelRHD avatar Jun 22 '19 19:06 AxelRHD

https://github.com/zserge/lorca/issues/98

pjebs avatar Apr 15 '20 09:04 pjebs

I use this method to load the html page----Yeah ,just like loading the remoting web page .

在 2020-04-15 17:41:16,"pj" [email protected] 写道:

#98

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

liuzeng01 avatar Apr 23 '20 00:04 liuzeng01