fix(hydrate): keep options.layers alive for the app's lifetime - #78
Merged
Conversation
Deploying effex with
|
| Latest commit: |
49af20d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://873b9dab.effex.pages.dev |
| Branch Preview URL: | https://fix-hydrate-scoped-layer-lif.effex.pages.dev |
Deploying effex-api with
|
| Latest commit: |
49af20d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://9c2f444c.effex-api.pages.dev |
| Branch Preview URL: | https://fix-hydrate-scoped-layer-lif.effex-api.pages.dev |
`Effect.provide(element, scopedLayer)` uses `scopedWith(scope => ...)` internally, which scopes the layer to the effect's lifetime. Since element functions return synchronously after building the DOM, every scoped resource — Navigation's popstate listener, SubscriptionRef PubSub subscribers, cache entries — was torn down before the user could interact. Browser back/forward, reactive updates, and anything relying on Effect.addFinalizer silently no-op'd. Fixed by building the merged layers as a Context in hydrate's outer program scope (kept alive by Effect.never) before providing. Includes: - packages/dom: the fix + regression test that would have caught this - packages/create-effex: fix SSG template (was missing router layer entirely) and SPA template (passed options to `mount` which doesn't accept them, so nothing mounted). - apps/docs: switch to `options.layers` pattern (was using the buggy `Effect.provide(App(), layer)` bake-in). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
jonlaing
force-pushed
the
fix/hydrate-scoped-layer-lifetime
branch
from
August 3, 2026 20:58
5614574 to
49af20d
Compare
This was referenced Aug 3, 2026
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
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
Effect.provide(element, scopedLayer)usesscopedWith(scope => ...)internally (effect/internal/layer.js:516), which creates a fresh scope tied to the effect's lifetime. Because element functions return synchronously after building the DOM, every scoped resource — Navigation'spopstatelistener,SubscriptionRefPubSub subscribers, cache entries — was torn down before the user could interact. Browser back/forward, reactive updates, and anything relying onEffect.addFinalizersilently no-op'd.Fix in
packages/dom/src/Render/hydrate/index.ts: build the merged layers as aContextin hydrate's outer program scope (kept alive byEffect.never), then provide the context.Investigation timeline
<Link>worked.Effect.runSynctoRuntime.runFork. Correct fix for that async-set problem, but symptom persisted.Layer.mergeAll(navLayer, dataProviderLayer)insidemakeClientLayer. Reproduced in isolation → mergeAll delivered signal updates fine, exonerated.@effex/vite-plugin'sstripStaticConfigtransform. The workspacepackages/vite-plugin/dist/index.jswas stale (source fix from b1241f4 wasn't re-committed), producingRoute.render(() => (fn)(undefined)). Portfolio's npm-installed@effex/vite-plugin@1.1.2had the correct dist. Rebuilding the workspace dist unblocked further diagnosis.Navigation.maketo log build & finalize.[FINALIZER]fired immediately after registration, before the user could click anything. Root cause:hydrate's innerEffect.providewas scoping the layer to the short-lived element.Also fixed (found during investigation)
create-effexSSG template: calledhydrate(App(), root)with no layers even thoughAppusesOutlet/Link. Now passesPlatform.makeClientLayer(router)viaoptions.layers.create-effexSPA template: passed{ layers: ... }as a third arg tomount, which doesn't accept options — the arg was silently dropped and the returnedEffectwas never run. Now usesrunApp(mount(...), { layer: ... }).apps/docs/src/client.ts: was using the buggyEffect.provide(App(), navLayer)bake-in. Switched tooptions.layers.Regression test
packages/dom/src/Render/hydrate/hydrate.test.tsadds a test that builds a scoped layer with a finalizer, hands its element tohydratewithoptions.layers, and asserts the finalizer has NOT fired 10ms after the initial render. Confirmed to fail against the pre-fix code and pass after.Test plan
pnpm --filter @effex/dom test— 17/17 hydrate tests pass, regression test presentpackages/dom+packages/routersuite: 465 passed, 2 skipped, 0 failedapps/docsboots and renders (keptNavigation.makeLayerviaoptions.layers)🤖 Generated with Claude Code