Comments (4)
TL:DR Use v-show
instead of v-if
to show/hide components based on resolution when server rendering.
Full explanation
When server rendering a Vue application and you conditionally display a node in the DOM and tie that condition to the screen dimensions, you have to remember that the server must "choose" arbitrarily a default window resolution, because there is no such thing as window
on the server.
This library uses a resolution of 410x730 when server rendering, which is the default resolution of GoogleBot. I decided to go with these dimensions to force the mobile-first approach that also most CSS frameworks adopted and is considered good practice.
I've also considered exposing a config to allow users to alter the server resolution, but as mentioned above i'd like to force a mobile-first approach, and did not find any compelling use cases where you'd like to render for desktop rather than for mobile.
That being said, if you really need custom server resolution i'm open for discussion
from vue-screen.
You are right about loading unneeded components, with v-show
you get to load everything even if you don't really need it, and that is indeed a waste of resources for the end user.
If you really wanna avoid loading unneeded components in Nuxt, you can try this:
// Index.vue
<template>
<div>
<client-only>
<MyComponent v-if="$screen.lg" />
</client-only>
</div>
</template>
<script>
export default {
components: {
MyComponent: () => import(/* webpackChunkName: "my-component" */ '~/components/MyComponent.vue'),
}
}
</script>
Pros:
- The
<client-only>
component won't render it on the server, avoiding hydration issues - The condition on a lazy loaded component will make the browser download the component only when the condition is true
Cons:
- MyComponent contents will never be indexed by crawlers
I'm afraid the point really lies on SEO concerns: If you want your component contents to be indexed, then your users must be penalised by downloading potentially unneeded scripts. If you don't care about indexing, then your component can just be rendered on demand on the client.
from vue-screen.
@reegodev Thanks very much for the explanation!
That does make sense to err on the side of a mobile viewport width for a potential Googlebot (Smartphone) crawl. I think ultimately perhaps it makes the use of this type of library unhelpful to achieve the goal of only lazily loading components for a specific user depending on their viewport (For example: a TabBar component that only loads and shows for a mobile user, but it's unneeded and never loaded for a desktop user).
Using a v-show
is not much different than just using CSS classes to show/hide elements, if all the components load in the background anyways.
It's a struggle, in the sense that there are definitely different amounts of content that needs to be loaded and displayed for a mobile user (for UX and bandwidth reasons). If only we had something that was native within the browser like srcset
that could dynamically load Vue components (instead of images) according to device resolution - and that it would work when using a SSR framework (in my case, Nuxt).
from vue-screen.
Thanks for the discussion and insight! Will close the issue.
from vue-screen.
Related Issues (20)
- The default tailwind breakpoint is incorrect HOT 2
- Remove spread operators HOT 1
- How to detect that lib was initialized successfully? HOT 1
- Get values of breakpoints through $screen object HOT 3
- Vue 3 support HOT 4
- [nuxtjs] vue-screen breaks on ios 12,13 HOT 2
- nuxtjs - $screen is not resolved after build HOT 1
- onUnmounted is called when there is no active component instance to be associated with HOT 3
- Adding conditionnals breakpoints HOT 3
- Using matchMedia instead of window.resize HOT 1
- small suggested change in docs for Nuxt integration HOT 1
- wrong breakpoint even when the condition is correct HOT 5
- Leftover console.log in package version HOT 5
- Width and Height on mobile view on Chrome is not correct HOT 1
- Property $screen does not exist. HOT 5
- dont work correctly on nuxt 3 3.4.2 HOT 1
- Bootstrap breakpoints HOT 3
- Support for vue-test-utils HOT 4
- grid is not reactive in vite , screen still works though HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-screen.