Skip to content

fix: allow accessing snippet in script tag #15789

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

Merged
merged 8 commits into from
Apr 17, 2025
Merged

Conversation

paoloricciuti
Copy link
Member

Closes #15786

Initially i thought of just changing const with var and effectively the error is gone...however the runtime behaviour is not the same as the value will be undefined before initialisation.

So what i did is revert back to function declaration and then invoking $.prevent_snippet_stringification on the function declaration discarding the return value (i don't like this bit but i couldn't think of a way around)...it still works because we are still changing the toString of the function.

The small bit of inconsistency is that if you invoke toString in the script tag it will not error out but i would say is probably fine since the error is meant for the template rather than the script tag.

Is there a better solution to this?

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Apr 17, 2025

🦋 Changeset detected

Latest commit: 7d5045b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@paoloricciuti
Copy link
Member Author

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15789

@paoloricciuti
Copy link
Member Author

Uh right, I meant to remove the if and forgot about it

@Rich-Harris Rich-Harris merged commit 7f473f6 into main Apr 17, 2025
13 checks passed
@Rich-Harris Rich-Harris deleted the use-snippet-in-script branch April 17, 2025 21:51
@github-actions github-actions bot mentioned this pull request Apr 17, 2025
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

Successfully merging this pull request may close these issues.

Svelte 5.27.1 - Cannot access <snippet> before initialization on page refresh
2 participants