Coder Social home page Coder Social logo

Comments (4)

reegodev avatar reegodev commented on May 31, 2024 3

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.

reegodev avatar reegodev commented on May 31, 2024 3

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.

nathanchase avatar nathanchase commented on May 31, 2024

@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.

nathanchase avatar nathanchase commented on May 31, 2024

Thanks for the discussion and insight! Will close the issue.

from vue-screen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.