Skip to content
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

nuxt / ssr workaround #65

Open
flowen opened this issue Dec 19, 2020 · 3 comments
Open

nuxt / ssr workaround #65

flowen opened this issue Dec 19, 2020 · 3 comments

Comments

@flowen
Copy link

flowen commented Dec 19, 2020

yo @shshaw & @notoriousb1t
just wanted to share my fugly workaround for anyone else in nuxt:

html:

<template>
   <h1 class="title" v-html="title" />
</template

computed:

  computed: {
    title: function() {
      const text = `split me baby`;

      if (typeof window !== `undefined` || typeof document !== `undefined`) {
        const Splitting = require("splitting");
        return Splitting.html({ content: text, by: "chars" });
      }

      return null;
    }
  },

nuxt.config.js:

  css: ["splitting/dist/splitting.css"],
  plugins: [{ src: "~/plugins/splitting.client.js", ssr: false }],
}

splitting.client.js:

import Vue from "vue";
import Splitting from "splitting";

Vue.use(Splitting);

last edit with a tip:
If you want to style this make sure you've got no scoped attribute

@flowen
Copy link
Author

flowen commented Feb 11, 2021

You can put the above in a mixin function and have text as parameter.

also figured you can do it as easy as this:

<template>
    <h1 data-splitting="chars">Split me baby</h1>
</template>

  mounted() {
    if (typeof window !== `undefined` || typeof document !== `undefined`) {
      const Splitting = require("splitting");
      Splitting();
    }
  },

The benefit is, that with JS disabled, the title will still show. With the previous method, it won't.
It does give a warning if you're animating charsMismatching childNodes vs. VNodes:. So depending on what you do, it can be a dealbreaker.

If someone knows how to fix showing the title in #1 case or knows how to fix the warning, please let me know 🙏

@P-at
Copy link

P-at commented Jun 24, 2021

If someone knows how to fix showing the title in #1 case or knows how to fix the warning, please let me know 🙏

Wrapping it in a <client-only> component should fix the warning.

@craigrileyuk
Copy link

If you're running Nuxt with Vue 3, we've just created a lite adaptation of Splitting designed for Vue 3 which is fully SSR compatible (that's why we made it)

https://www.npmjs.com/package/vue3-splitting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants