-
Notifications
You must be signed in to change notification settings - Fork 11
New injections for forked elm packages, and add hot reloading #65
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
Draft
lydell
wants to merge
64
commits into
lamdera-next
Choose a base branch
from
lydell-upgrade-native
base: lamdera-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+654
−841
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lydell
commented
Aug 10, 2025
lydell
commented
Aug 10, 2025
Conflicts: extra/Lamdera/Injection.hs
lydell
commented
Dec 20, 2025
Note that I’m unsure about the try-catch. Conflicts: extra/Lamdera/Injection.hs
supermario
reviewed
Dec 23, 2025
…t a direct dependency we want
lydell
commented
Jan 1, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This adds https://github.com/lydell/elm-safe-virtual-dom to Lamdera, simplifies the internal implementation, and changes Freeze mode to use elm-watch-style hot reloading (which preserves things like scroll position) when possible during development.
Details
I’ve added hot reloading and stopping of apps directly to elm/core. Read about it here:
Forks:
As you can see above, lydell/core has a lamdera branch:
errorHandlerargument to apps. (Used by the new REPL stuff.)lydell/browser also has a lamdera branch:
Browser.Navigation.Key, to make it transferable from an old app to a new app. Random note:Browser.Navigation.Keyis no longer a special type and can even be sent to the backend and back to the frontend.Benefits of this PR:
lamdera live.TODO
Testing!
lamdera livehot reloading.lamdera live).Installing the forked packages
Here’s the plan: Start out simple and make things more complicated in the future if needed.
ByteStringof alternate file content. Used to do the replacements.Validate versions
In the TemplateHaskell-created map, each elm/* package that we replace specifies a version. That’s the version that the forked package was based on. In elm.json, there will still be lines like
"elm/core": "1.0.5". What if the version in elm.json and the “fork version” do not match?First, let’s go through the scenarios where the fork version is higher than the version specified in elm.json:
1.xversions exists of all elm/* packages we replace. But we could code it to be a hard error.1.0.xversions exists of all elm/* packages we replace. But we could code it to be a hard error."elm/virtual-dom": "1.0.3"in their elm.json, and haven’t bothered updating to the recent 1.0.4 version. If we made this a hard error, it would be annoying for lots of people. I think it’s better to simply allow the fork patch version to be greater than specified. If we want to, we could inform about this somehow (log message, or maybe the comments in the compiled JS shown below is enough?).Then, let’s go through the opposite scenarios, where the fork version is lower than the version specified in elm.json:
"elm/virtual-dom": "1.0.5"in their elm.json, I think it should be a hard error, informing them that you can’t go above 1.0.4 with this release of the Lamdera compiler. (Silently using 1.0.4 anyway would be misleading, leading to a false sense of security.) They need to wait for a new Lamdera compiler version that has pulled in the security fix.lamdera inituses the known versions of elm/virtual-dom etc.In the compiled JS, it might be nice adding comments for debugging:
Replacements
When reading files from disk in
ELM_HOME, check if the file is in the replacement maps. If so, go with that file content instead of the actual files.This requires two changes to caches:
ELM_HOMEthere areartifacts.datfiles, which Lamdera already callartifacts.x.dat. Now, they’ll be called for exampleartifacts.lamdera-1.3.3-0.19.1.dat. This is because we can’t trust an artifacts file made by a previous Lamdera version. It might have had different package replacements.elm-stuff/there’s usually a0.19.1folder with artifacts. Now there will beelm-stuff/lamdera-1.3.3-0.19.1/instead. This is again because cached stuff from a previous Lamdera version cannot be trusted – they might contain the wrong package replacement code. (We don’t use the same approach forELM_HOME–~/.elm/lamdera-1.3.3-0.19.1/– because IDE:s expect packages to be in~/.elm/0.19.1/.)Pros and cons
The benefit of having hard-coded (TemplateHaskell) maps of replacements is simplicity. We know that the provided packages are going to work with Lamdera and hot reloading.
The downside is that we need to make a new compiler release if there’s a hotfix in one of the replaced elm/* packages. However, that happens so infrequently that it feels safe to try out this approach.
Hot reloading
In this PR, I tried to change as little as possible. Both in terms of code, and in terms of user experience.
The default is still to refresh the page when an Elm file changes. But in Freeze mode, we now do hot reloading instead of page refresh when possible.
I decided to change as little as possible on the Haskell side, and implemented almost everything needed for hot reloading in Live.elm and live.js.
The web socket server still just sends an
rmessage when an Elm file has changed. When handling that message, the following happens:/to get the page as HTML, with the inline<script>tag.// lamdera-elm-js-startand// lamdera-elm-js-endmarkers.init. If not, refresh the page and letinitreset and notify about this.Previous (maybe future) idea
The last commit of #29 adds two interesting things:
maintype hasn’t changed.) It does that by adding a script tag pointing to the URL/_x/js./_x/js.If the endpoint returns JavaScript like this, hot reload should Just Work: