-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
docs(isBrowser/isServer): explain why those helpers are useful #6085
Conversation
👷 Deploy request for qwik-insights pending review.Visit the deploys page to approve it
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, but the test is actually not done at runtime. The qwik plugin sets the variables at build time so that the minification can eliminate the unused code entirely. The code you found is a fallback that never gets used.
function getQwikBuildModule(isSSR: boolean, target: QwikBuildTarget) {
const isServer = isSSR || target === 'test';
const isDev = opts.buildMode === 'development';
return `// @builder.io/qwik/build
export const isServer = ${JSON.stringify(isServer)};
export const isBrowser = ${JSON.stringify(!isServer)};
export const isDev = ${JSON.stringify(isDev)};
`;
}
Ah interesting, thanks! So for example if I was building an app with 2 versions - one SSR for the website, one CSR for the mobile app - then the bundler would be able at build time to remove the If yes, do you see any more common use cases or more straightforward benefits that I could outline in the docs? |
Maybe that this is also used to remove imports on the CSR build. The optimizer assumes that all named imports are side-effect free on the client and so if something is only used inside server$, it won't be imported on the client. |
Oh I think I get it! thanks, I'll try to outline it in the docs :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it 👏 Thanks @maiieul
looks good to me can we merge this? |
Overview
What is it?
Description
A lot of developers are used to simply using if(typeof window !== 'undefined'). I tried to improve the docs on why are isServer/isBrowser needed.
Use cases and why
Checklist: