Skip to content
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

Snippet export - compiler treats $page as local declaration #14693

Open
notramo opened this issue Dec 12, 2024 · 4 comments
Open

Snippet export - compiler treats $page as local declaration #14693

notramo opened this issue Dec 12, 2024 · 4 comments

Comments

@notramo
Copy link

notramo commented Dec 12, 2024

Describe the bug

There is only a <script module> and no <script> in the component, but the snippet can not be exported, because the compiler treats the $page subscription as if it were local. If I remove the $ from $page, it can be exported.

Reproduction

<script module>
import { page } from '$app/stores';
export { exampleSnippet }; // type checker error: exampleSnippet not found (undefined export)
</script>

{#snippet exampleSnippet()}
  {JSON.stringify($page.data)}
{/snippet}

Logs

No response

System Info

-

Severity

annoyance

@trueadm
Copy link
Contributor

trueadm commented Dec 12, 2024

I don't believe this can work, as the template needs to create a subscription to the store and the only way we can do that is by using the component's effect to dispose of it. So in this case, you can't reference a store from within a snippet that needs to be exported using the $ prefix.

@Thiagolino8
Copy link

The auto subscription with the $ prefix is tied to the component lifecycle

@Leonidaz
Copy link

as a workaround, maybe just have the snippet take page as a parameter and pass $page when you're doing {@render exampleSnippet($page)}

<script module>
  import { page } from '$app/stores';
  export { exampleSnippet }; 
</script>

{#snippet exampleSnippet(page)}
  {JSON.stringify(page.data)}
{/snippet}

I think this should work with the page from the proposed $app/states sveltejs/kit#13140 that uses signals instead of stores.

@notramo
Copy link
Author

notramo commented Dec 12, 2024

@Leonidaz, indeed, passing page to the snippet works, even with the $app/stores version.

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

4 participants