coen.bakker

coen.bakker

CONTEXT
I have a custom scrollbar that I control with JavaScript. This code needs to be executed without too much delay, because otherwise the user will see the elements that make up the scrollbar jump into their positions. The user should, instead, see the elements at their proper positions immediately.

The page I use the scrollbar on is part of a multi-page live session. I use live navigation (both pathing and redirecting) within this live session.

PROBLEM
When I use a mounted hook the JS script loads too late. When I import the script into my app.js (the ESbuild entry point), it is quick enough, but it load on all pages, not just the ones with the scrollbar on it – resulting in errors in the browser since it cannot always find the elements the script is referring to. If I create an extra ESbuild entry point and add a <script defer src=.../> to the LiveView that uses the scrollbar, the js script does load and also fast enough, but it only loads when I directly visit the URL that points to a page/component that includes the scrollbar. When I use live navigation (which a user will often), it does not load.

QUESTION
Any advice on how to include path-dependent JS scripts into a project would be very welcome. It’s not really about making my JS reference work properly, because I know I can find a way to do it. It’s more about finding a way that is pleasant to work with and fault tolerant. For example, solving the problem by including an event listener in my app.js that listens for navigation to specific paths, to then load additional scripts, seems maybe not the best possible option.

Thank you once again.

Showing Posts 1 to 10

cmo

cmo

You can have a placeholder where those elements will end up and fade the actual ones in when they load so it’s not so jarring? How big is this scrollbar JS that the load time is so significant?

There are several blogs on page specific JavaScript with liveview hooks and IIRC LiveBook does it too.

Here is one such blog: Page Specific Javascript with Phoenix LiveView and Esbuild · aswinmohan.me

al2o3cr

al2o3cr

This sounds like a variant of the “flash of unstyled content” issue, but with JS loading time as the issue instead of CSS loading time.

A similar solution to FOUC-prevention could work:

  • set the elements to display: none (directly or with CSS) in the rendered HTML
  • display those elements at the end of the mounted hook, maybe with a CSS transition
coen.bakker

coen.bakker OP

The scrollbar itself is computationally light. It’s the mounting of the LiveView that takes a bit of time. And since the mounted hook executes after that, the layout shift happens.

The initial paint (the first server request by the LiveView) of the page is fast. The second server request has the delay.

My idea was to have my scrollbar script execute between those two server requests. And that does work, but then the script does not load on live navigation – only on direct loading of the target url.

Edit: and I’ll have a (better) read of the link you sent.

coen.bakker

coen.bakker OP

I think this will work great for this specific use case. To keep the management of page specific JS simple (i.e. avoid it all together :stuck_out_tongue: )

coen.bakker

coen.bakker OP

Would a beforeMounted hook be a nice addition to the current set of hooks :thinking: ?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you elaborate on how that might work or when it would fire in the page lifecycle?

coen.bakker

coen.bakker OP

I hope to have the knowledge necessary to fully answer that question one day soon. For now I still have too much of my baby fur on me.

However, I can imagine there is an opportunity here:

On each mount LiveView establishes a web socket connection and replaces the initial paint. If there is a hook right before that, this would increase the control of developers.

chrismccord

chrismccord

Creator of Phoenix

This should suffice to handle both the immediate scrollbar on page ready, as well as after live navigations, without a hook in the mix:

Scrollbar = {
  init(){
    let el = doument.getElementById("scrollbar")
    if(!el || (this.el && this.el.isSameNode(el))){ return }
    this.el = el
    //...
  }
}
document.addEventListener("DOMContentLoaded", () => Scrollbar.init())
window.addEventListener("phx:page-loading-stop", info => Scrollbar.init())
coen.bakker

coen.bakker OP

An additional benefit of using hooks is that it can help with code organization. Using Surface UI I can colocate my hooks and my LiveView.

|
|__ example.ex
|
|__ example.sface
|
|__ example.hooks.js

For me that beats setting up additional entry points with ESbuild or referring to modules in app.js.

coen.bakker

coen.bakker OP

Quick update.

It still happens regularly that I wish I could access some non-existing life cycle hook. Before it was beforeMount, this time around I’m working on transitions and I was hoping for a beforeDestroy.

Would a pull request be excepted?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews