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

How to prevent redundant data fetching when the page is fully loaded? #998

Open
slegrand45 opened this issue Oct 21, 2024 · 5 comments
Open

Comments

@slegrand45
Copy link

Hi,

First of all, congratulations for this amazing package. It's really an impressive work. go-app certainly deserves to be better known.

I'm currently developing an application where I need to fetch data from the backend server. I'm using [Connect] (https://connectrpc.com/) for this purpose.

According to my understanding, the data fetching code should reside within either the OnMount() or OnNav() methods of the components. However, I've observed that when the page fully loads, the fetching code gets called twice: once during what I presume to be pre-rendering, and again when OnMount()/OnNav() is triggered on the client-side.

Is there a way to prevent this redundant data fetching? This issue, of course, doesn't occur after the initial application load as pre-rendering is no longer a factor.

Thank you for your help.

@maxence-charriere
Copy link
Owner

I think a more suitable would be to use action and state. Available from context

@maxence-charriere
Copy link
Owner

Thanks for the kind words too.

@oderwat
Copy link
Contributor

oderwat commented Oct 22, 2024

@slegrand45 do you need a fetch at pre-render (in the back end)?

I think you could check app.IsServer() / app.IsClient() or use different code for the server and the backend routes. The latter is what we do for basically all application we made with go-app. See this example (which also demonstrates NATS, which we prefer over other ways to connect to a backend): https://github.com/oderwat/go-nats-app

@slegrand45
Copy link
Author

Thank you very much for your responses. @oderwat, I'll definitely examine the go-nats-app example. Ideally, I'd like to set the value of a component field within the OnPreRender() method and then be able to retrieve that value on the client-side within the OnMount() or OnNav() methods. Something like this:

type hello struct {
	app.Compo
	Name string
}

func (h *hello) OnPreRender(ctx app.Context) {
    h.Name = "myvalue"
}

func (h *hello) OnMount(ctx app.Context) {
    // display "myvalue" even when running on the client
    // so no need to call the server again to retrieve that value
    app.Log(h.Name)
}

I may be wrong but that seems impossible at this time.

@oderwat
Copy link
Contributor

oderwat commented Oct 22, 2024

@slegrand45 What you do in PreRender is not accessible by the frontend. It will be replaced with the actual loader if the JS is run. PreRender is meant for SEO only.

Ways to share variables between backend and frontend (after compilation) which I am aware of are:

  • app.Handler { Env: map[string]string{ "Test": "Value" } } with app.GetEnv("Test")
  • You could add something to app.Handler { RawHeaders: "<script>document.GlobalTest</script>" } which then could be picked up through something likeapp.Get("document").Get("GlobalTest").String() (or without the document. Not sure)
  • Making a call to the backend through HTTP/S (JS fetch) or a WebSocket (WASM implementations).

There is nothing else how you could get data to the frontend after compilation afaik.

Using "Env" or "RawHeaders" will also only work on installation/updating of the App in the Hosts Browser Engine (not on reloads). This means: You can't change this in the backend without bumping app.Handler { Version string } and have the App(s) update to this new version. If you needed a lot of data to be updated you could also compile a new WASM file and bake data into that and have the server send a new version.

Just as a reminder: This is a framework for PWAs and therefore will use caching of all the data in such a way, that it works without a network connection after it got installed. So any kind of "backend" access is basically an "add on" which needs to be maintained by the developer of the application itself.

@maxence-charriere I hope I am right with those claims. Please confirm and correct me when needed.

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

No branches or pull requests

3 participants