Skip to content

getContext is undefined when called inside an element attribute. #7549

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
schurhammer opened this issue May 18, 2022 · 5 comments
Closed

getContext is undefined when called inside an element attribute. #7549

schurhammer opened this issue May 18, 2022 · 5 comments
Assignees
Milestone

Comments

@schurhammer
Copy link

Describe the bug

I have some text put into the context of a page. When reading it with getContext in a child component into the href of a link, the value is undefined.

The SSR handles it as expected but in the client it becomes undefined (only during hydration I think, not sure).

Note that it is only undefined when called inside an href or other html attribute. When called as part of some text it is the expected value.

Reproduction

https://github.com/schurhammer/sveltekit-context-bug

Logs

Server logs
example
example

Browser logs
Test.svelte? [sm]:5 example
Test.svelte? [sm]:5 undefined

System Info

System:
    OS: Linux 5.15 Manjaro Linux
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 9.27 GB / 31.04 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Chromium: 101.0.4951.54
    Firefox: 100.0
  npmPackages:
    @sveltejs/adapter-node: next => 1.0.0-next.73 
    @sveltejs/kit: next => 1.0.0-next.335 
    svelte: ^3.44.0 => 3.48.0

Severity

serious, but I can work around it

Additional Information

No response

@mrkishi
Copy link
Member

mrkishi commented May 19, 2022

This would be an issue with Svelte itself, though it's unclear whether it's supposed to be one. Did Svelte ever guarantee expressions in the template happen during component initialization?

The problem here appears to be that hydration isn't considered part of component initialization, which is why this works in SSR and the repl. It may be possible to change this cleanly, but I'm not sure if there would be other implications.

@Rich-Harris Rich-Harris transferred this issue from sveltejs/kit May 19, 2022
@Rich-Harris
Copy link
Member

We don't make that guarantee. It's the SSR behaviour that's wrong, in fact, though I haven't looked into how straightforward that would be to change

@Conduitry
Copy link
Member

I was thinking that this should probably be throwing an exception in both DOM and SSR mode, and it sounds like Rich is leaning in that same direction. We already have a function in svelte/internal that's supposed to throw when called other than in top-level component initialization code, but that's apparently not working correctly (or consistently) in this case.

@Rich-Harris
Copy link
Member

closed via #11061

@Rich-Harris
Copy link
Member

(or rather, this works as expected in Svelte 5 — not certain if this was ever fixed in 4 or below)

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 a pull request may close this issue.

5 participants