idi527
Has anyone used lazy loading in a phoenix live view project?
I’m currently in a process of optimising the client part of a liveview app and wondered if anyone had tried using lazy loading with liveview.
My current approach is:
- Import heavy dependencies like charting libs with dynamic imports in liveview hooks:
async function createChart(config, el) {
const [am4core, am4charts] = await Promise.all([
import(/* webpackChunkName: "amcharts4-core" */ "@amcharts/amcharts4/core"),
import(/* webpackChunkName: "amcharts4-charts" */ "@amcharts/amcharts4/charts"),
]);
return am4core.createFromConfig(config, el, am4charts.XYChart);
}
const ChartHook = {
config() {
return JSON.parse(this.el.dataset.config);
},
async mounted() {
this.chart = await createChart(this.config(), this.el);
},
beforeDestroy() {
this.chart.dispose();
},
};
export default ChartHook;
-
Add
chunkFilename: "[id].[contenthash].js"option to webpack config to allow for cache busting when the chunks change, note that this doesn’t affect the filenames of the entry points likeapp.jssince the hash there is added bymix phx.digest -
(Optional) Compress
/\.(js|css|svg)$/via webpack using brotli, because why not (and in part thanks to #3840). This would mostly affect the chunks. -
Update
Plug.Staticoptions inendpoint.exto includegzip: true, brotli: true
Most Liked
idi527
I think it’s easy enough to do without any extensions from liveview so I made a small example which roughly follows the steps outlined in OP: https://github.com/syfgkjasdkn/lazy-live-view/commits/master.
sfusato
My entry file as well as the app’s css file would benefit from the brotli compression. I ended up writing a mix task that will add the hash to the .br files generated by Webpack.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









