-
Notifications
You must be signed in to change notification settings - Fork 51
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
window is not defined error when used with SSR #1
Comments
I have 0 experience in SSR, and am quite new to the Vue ecosystem, so I could use some help with this one. I've spent some time investigating this and the issue is how vue-typer's CSS is being loaded. Here is a summary of what is going on:
I can think of some hacky workarounds, none of which are ideal: Approach 1: Fork style-loader (or vue-style-loader?) to add some error checking before trying to access
Caveat: I don't think anyone wants to keep this fork up-to-date. I know I don't. Approach 2: Output two vue-typer bundles. One 'server-side-bundle' that uses Caveat: Essentially doubles the vue-typer package size due to 2 near-duplicate bundles. This is also a huge pain for the isomorphic apps because they'll either have to build 2 separate bundles (one client bundle importing vue-typer's client bundle, and another for the server bundle), or add some detection code to see if they're running in the browser vs server before importing vue-typer. For the latter, I'm pretty sure they can't use the ES6 Approach 3: Use extract-text-webpack-plugin to pull the CSS out of vue-typer into a separate CSS file. Force all users to import the CSS file along with the vue-typer JS bundle. Caveat: Super ugly and onerous for users. What if users aren't using webpack or browserify? Linking vue-typer's CSS themselves will be annoying. (Note that I tried to prove this out on Vue Hackernews 2.0, but I couldn't get it working. It could be due to some combination of extract-text-webpack-plugin, minified CSS, vue-loader scoped rules, autoprefixer, and/or css-loader/css-nano options, especially when run again on the consuming app's side.) Again, these are just some workarounds I'm tossing out as someone who has no experience in the area. There must be a better solution, so I look forward to hearing some ideas and PRs. |
I got this working with the Vue SSR framework Nuxt. I bet you can do something similar in Vue's regular SSR but I'm unfamiliar with is... I wrote a simple plugin file vue-typer.js import Vue from 'vue'
import VueTyperPlugin from 'vue-typer'
window.onNuxtReady(() => {
Vue.use(VueTyperPlugin)
}) And then in my nuxt.config.js file module.exports = {
...
plugins: [
...
{ src: 'plugins/vue-typer.js', ssr: false}
],
...
} Hope this helps somebody... |
This is another approach if you only want to load it in a single component in Nuxt. at the top of your component.vue file if (process.browser) {
var VueTyper = require('vue-typer').VueTyper
} load the component components: {
VueTyper
}, in the template <no-ssr>
<vue-typer text='your text' ></vue-typer>
</no-ssr> |
I tried to use vue-typer with Vue hackernews 2.0 (which uses SSR), and I get an error from vue-typer when attempting to import it:
The text was updated successfully, but these errors were encountered: