question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Organizing the services in your project" example code is not SSR compatible

See original GitHub issue

I believe it’s not a bug in Feathers-Vuex itself and just a problem with the example code.

Expected behavior

The guide provides a code example for Organizing the services in your project with the use of require.context.

https://feathers-plus.github.io/v1/feathers-vuex/common-patterns.html#Organizing-the-services-in-your-project

Actual behavior

It works fine in the browser but fails on the server when used in SSR mode.

When you run the server, then open the page in browser and hit refresh, the server tries to redefine store property on the services managed by this feature. The result is:

{ TypeError: Cannot redefine property: store
    at Function.defineProperties (<anonymous>)
    at setupStore (/home/gusto/rpg/services-test/node_modules/feathers-vuex/lib/service-module/service-module.js:128:14)

Using service('users') without require.context behaves properly.

I did try to fix it but failed.

Repro

https://github.com/gustojs/feathers-vuex-services-test

System configuration

Linux Mint Feathers-Vuex 1.3 Nuxt 1.4 Node 10.1 npm 6.0.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
JaneOricommented, Oct 18, 2018

Just ran into this same problem and now have a minimal solution.

Documentation code issues:

Issue 1 is that the main file exports new Vuex.Store(...) directly but should be exporting a function, createStore that returns the instance. (Also the Nuxt docs say it should be called index.js instead of store.js but maybe there is another way to set it up that I haven’t run into yet)

Issue 2 is calling the service function outside of that createStore function and reusing the first instance on a subsequent call.

Solution:

  1. Wrap the main export new Vuex.Store(...) to a createStore function and export default createStore instead
  2. In the service file, change: const servicePlugin = service(servicePath, { to this: const servicePlugin = () => service(servicePath, {
  3. in the main file, change: modulePath => requireModule(modulePath).default to this: modulePath => requireModule(modulePath).default()
  4. Move const servicePlugins = line into the createStore function that’s exported so the calls to service functions only happen during the execution of createStore

image

image

I am green here so if any of this is problematic in some other way, please let me know.

Hope it helps!

2reactions
DreaMindercommented, Feb 10, 2019

In nuxt@3 classic vuex usage new Vuex.Store will be deprecated. There has to be another solution…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side rendering support / SSR · Issue #412 - GitHub
I created a web app using Amplify+React. Now I need to work on the SEO, but this requires server-side rendering (I am using ......
Read more >
Angular 7 - Service Worker PWA + SSR not working on server
The problem is, service worker is tried to be registered on the server side, which is not supported. What you can do is...
Read more >
How to implement server-side rendering in your React app in ...
In this tutorial, we'll use server-side rendering to deliver an HTML response ... SSR can improve performance if your application is small.
Read more >
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
Nuxt.js is based off an implementation of SSR for the popular React library called Next. ... Not much different from creating a Vue...
Read more >
Getting Started with Server-Side Rendering (SSR) - JavaScript
The withSSRContext utility creates an instance of Amplify scoped to a single request ( req ) using those cookie credentials. For client-side code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found