Skip to content

Cannot use untrack in a .svelte.js or .svelte.ts file #14883

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

Closed
feus4177 opened this issue Jan 2, 2025 · 3 comments
Closed

Cannot use untrack in a .svelte.js or .svelte.ts file #14883

feus4177 opened this issue Jan 2, 2025 · 3 comments

Comments

@feus4177
Copy link

feus4177 commented Jan 2, 2025

Describe the bug

Want to use explicit dependencies with $effect like demonstrated here. Since this will be used across the app, it would be nice to factor it out into a .svelte.js module. However, the untrack function does not seem to get added correctly to those files. As a result an error gets thrown:

ReferenceError: untrack is not defined
    at $effect (playground:output:3369:4)
    at update_reaction (playground:output:1917:57)
    at update_effect (playground:output:2043:19)
    at flush_queued_effects (playground:output:2152:7)
    at flush_queued_root_effects (playground:output:2131:5)
    at process_deferred (playground:output:2183:3)

Reproduction

https://svelte.dev/playground/f9fec2106b954d02b23e7f710aa62e80?version=5.16.0

Logs

No response

System Info

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.53 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.9.0 - /opt/homebrew/bin/node
    npm: 10.8.3 - /opt/homebrew/bin/npm
    pnpm: 9.5.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.205
    Safari: 18.2
  npmPackages:
    svelte: ^5.11.1 => 5.11.1

Severity

blocking an upgrade

@Conduitry
Copy link
Member

untrack isn't a global. https://svelte.dev/docs/svelte/svelte

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2025
@feus4177
Copy link
Author

feus4177 commented Jan 2, 2025

My apologies. I had looked at https://svelte.dev/docs/svelte/svelte#untrack and there was no import in the example so I just got confused.

@braebo
Copy link
Member

braebo commented Feb 14, 2025

Yea throwing the docs at @feus4177 as if everything they need to know is in there isn't entirely fair when this is literally the entire doc entry:

When used inside a $derived or $effect, any state read inside fn will not be treated as a dependency.

$effect(() => {
	// this will run when `data` changes, but not when `time` changes
	save(data, {
		timestamp: anytimestamp: untrack(() => time)
	});
});

Considering how often Svelte 5 code actually ends up looking like this:

import { untrack } from 'svelte'

let count = $state(0);

$effect(() => {
  count;
  untrack(() => {
    doStuff();
  });
});

untrack really should be covered in the $effect section of the docs, with a full example including the import.

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