-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
$app/environment.browser
isn't reliable for libraries (packaging)
#5879
Comments
$app/env.browser
for libraries, via packaging$app/env.browser
isn't reliable for libraries (packaging)
It's defined as I'm not super familiar with the REPL, but it looks like it calls the compiler directly: https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/bundler/index.js I wonder if we should define |
Do you think it wouldn't be inlined even after optimization&minimization?
I'm not familiar with REPL internal as well, I have just seen this bug in libraries over and over again when using REPL or Sapper(which although obsolete it proves that it wouldn't work outside SvelteKit) for example.
Is it problematic to have some environment variables in the Svelte language by itself? |
@TorstenDittmann I think you are seeing similar to Issue #6259, please see my comment there for a (temporary?) solution. edit: sorry, Issue #6259 |
$app/env.browser
isn't reliable for libraries (packaging)$app/environment
isn't reliable for libraries (packaging)
closing as duplicate of #1950 |
$app/environment
isn't reliable for libraries (packaging)$app/environment.browser
isn't reliable for libraries (packaging)
Ah, yeah, I suppose this one is a bit different than the rest I think you'd need to bundle it twice, once with |
That's not a great method in my opinion, because:
You can decide that on Vite and Rollup plugin of svelte, there will be defined the "environment" like SvelteKit. |
That's incorrect. The SSR and browser builds are entirely separate invocations of Vite
That's worse for the user as it results in larger bundle sizes since it prevents dead code removal, etc. Using the |
I have two ideas how we may solve this problem and makes everyone happy, and they are both using a similar idea that raises an some issue, so I'll first say the idea and then will describe the concrete solution. IdeaDuring the packaging process,
Sadly for implementing 3, the problem in this solution is that Concrete Solutions to The ProblemHere are 4 possible concrete implementation of the idea that I believe are friendly to minification (i.e. resolving the const browser = ( () => {
try {
return import.meta.env.SSR ?? (typeof window !== 'undefined');
} catch (e) {
return (typeof window !== 'undefined');
}
}); Didn't test it, but it seems to be easy to be minified as well. export const browser = import.meta.env?.SSR ?? (typeof window !== 'undefined'); This will work well everywhere, and will be known at compile time only on Vite. The issue is that defining |
Additional suggestion before releasing 1.0(assuming we're going in the path I mentioned), so there will be less breaking changes, one of the two:
|
Hi there I need Will the solution you are working on for 1.0 (@Tal500 ) make it possible to use this module in a package as well? Otherwise, I'd like to open a new issue on this part. ps. this library is to enrich the ecosystem with Svelte magic powered stuff - so to stay as closely as possible to this library in terms of the API compared to React/Vue/Angular, I really need this svelte-module to work. ps2. I did try to reference the navigation package via |
No way that the navigation part will be part of Svelte core, so this is too much to ask. However, I can suggest you two simple logical solutions to your case(you can use both of them together!):
I am not currently working on a solution, I was only throwing suggestions to the air.
The Will be happy to suggest more things if you tag me on an issue you'll open on your Github project. |
Hi @Tal500 - thx for this - then I know what to do for the library. Not a huge loss - but good to know and inform the developer to be using beforeNavigate themselves instead of relying on the API from the documentation. |
Closing in favor of #8033. Please see the recommended solution there |
Describe 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 thebrowser
variable, will fail to be used in Svelte projects that are outside the SvelteKit environment (e.g. in REPL), since the output still use thebrowser
variable, and wait for the environment to compile it.There are two possible solutions:
browser
variable as part of the specific Svelte environment, instead of the specific SvelteKit environment. (Recommended)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 viasvelte-kit package
and see the output isn't compiled at all, thebrowser
stays in the output.Logs
No response
System Info
Severity
annoyance
Additional Information
No response
The text was updated successfully, but these errors were encountered: