`$app/environment.browser` isn't reliable for libraries (packaging)
See original GitHub issueDescribe the bug
Every Svelte UI library that want to check if the current environment is the server or the client, uses often the variable $app/environment.browser
.
While it’s the recommended way for SvelteKit normal application, this is problematic when the library do svelte-kit package
, to get the library compiled output.
Any project using a UI library that written in SvelteKit, exported via svelte-kit package
, and use the browser
variable, will fail to be used in Svelte projects that are outside the SvelteKit environment (e.g. in REPL), since the output still use the browser
variable, and wait for the environment to compile it.
There are two possible solutions:
- Put the
browser
variable as part of the specific Svelte environment, instead of the specific SvelteKit environment. (Recommended) - When compiled via
svelte-kit package
, define the value ofbrowser
to be equal totypeof window !== 'undefined'
.
Sadly, I’ve seen too many libraries do this mistake, and proposed them to use typeof window !== 'undefined'
instead. However, the logic and the sanity are on their side 😃
Reproduction
Just use the browser
definition, export via svelte-kit package
and see the output isn’t compiled at all, the browser
stays in the output.
Logs
No response
System Info
N/A
Severity
annoyance
Additional Information
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:15 (14 by maintainers)
Not sure if related, but after upgrading to kit version
next.409
, I’ve been running into following issues withvitetest
.Closing in favor of https://github.com/sveltejs/kit/issues/8033. Please see the recommended solution there