Is it possible to auto-import file into every server endpoint for Nuxt3 - nuxtjs3

I'm really liking Nuxts new server/api folder, but i've been trying to figure out if there is anyway to import a module that is available for use in every endpoint (so I don't have to import it everytime).
This is particularly useful for modules that are used in almost every endpoint (like moment for example)
For example in a file like this
/server/api/create-event.js
import moment from 'moment'
export default defineEventHandler(async event => {
console.log(moment().format(), 'creating event at this time');
})
I would want to simplify it down to:
export default defineEventHandler(async event => {
console.log(moment().format(), 'creating event at this time');
})
So there is no need for an import moment in every single endpoint.
I tried setting up the server middleware but that seems to apply more to the routes then the server function. I'm not sure if it's even possible at the moment but would be very useful.

Related

How to retain state during navigation with Next.js and Redux?

Using Next.js next/router will cause a reload or it will retain Redux state?
import { useRouter } from 'next/router'
const DashboardSidebar = ({ mobile = false }: Props) => {
const router = useRouter()
router.push(
`/${lang}/dashboard/organizations/${organizationId}/events/${id}`
)
Or I should use redux-persist to retain state? When I follow this setup:
https://redux-toolkit.js.org/usage/usage-guide#use-with-redux-persist
I got this error:
redux-persist failed to create sync storage. falling back to noop storage.
Do I get it because I use Next.js and SSR?
Shall I move forward and use next-redux-wrapper?
https://github.com/kirill-konshin/next-redux-wrapper
Here are some instructions: https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering#server-side-rendering-with-nextjs
As the user changes routes, the store needs to be rehydrated each time before the html is generated and sent to the client.
I am not a fan of this architecture. It seems strange that the store needs to be filled back up with its state (rehydrated) every time the user needs a new render. Where are you going to persist the store in the meantime? What is its lifecyle?
In a SPA, the lifecycle of the store is when the page refreshes (which would require a programmatic invocation, or the user navigating away and coming back, or pressing Ctrl+R). Conceptually, this is much simpler.

Using vuex with Vue 3

Last year I spent some time learning Vue 2. I really enjoyed the framework but did not move forward with a project. I now have time for a project but I'd like to use Vue 3 with the composition API. I'll be using Firebase as the backend. I've seen conflicting techniques on whether or not to use Vuex.
For example, I'd like to store a "currentUser" object in global state that can be accessed from any component in the app. Normally, it would be done using Vuex. I'd have a getter to return the object, an async action to get the data from firebase and a mutation to set the state.
However, I've seen several Vue 3 code examples that do not use Vuex at all, instead they do something like this to get/set a currentUser in an app wherever it is needed for example in a navbar component.
composables/getUser.js
import { ref } from 'vue'
import firebase from 'firebase/app'
// refs
const user = ref(firebase.auth().currentUser)
// auth changes
firebase.auth().onAuthStateChanged(_user => {
console.log('User state change. Current user is:', _user)
user.value = _user
});
const getUser = () => {
return { user }
}
export default getUser
With this little bit of code above, I'm able to import getUser.js and access the currently logged in user using code like this. Also, the user object is now reactive:
<script>
import getUser from '../composables/getUser'
export default {
setup() {
const { user } = getUser()
return { user }
}
}
</script>
It seems I can use these little functions to get data from db directly without the need to use the Vuex pattern, which I find to be a bit more complicated.
So, my question is - if I'm starting a new Vue 3 project, is it ok to create "composable" functions to get/set data and import them into my components instead of using Vuex? Are there any downsides to this method? Or should I just stick with Vuex?
Short answer - You don't need it.
Long answer - It depends.
It depends mostly on your app and how often do you use "currentUser" data inside your components. If it's in 2 or more components, how often do you want to perform actually fetching from backend?
Once on app-init/login or every time each component mounts?
(probably once)
Does it need to be reactive? If yes - then you'll probably use centralized data pattern, your own or a library. Taken that into consideration it's probably more simple to just use Vuex.

How can I manually compile a svelte component down to the final javascript and css that sapper/svelte produces?

Our company produces an automation framework that is written in svelte/sapper. One feature is that developers can create custom ui widgets, currently using plain js/html/css and our client side api. These widgets are stored in the database and not on the file system.
I think it would be a big plus to allow them to create widgets as svelte components since it contains all of the markup, js and css in one location and would give them all of the benefits of svelte's reactivity.
I have gotten as far as creating an endpoint that compiles components using svelte's server API but that just seems to generate a module that is ready for rollup-plugin-svelte/sapper/babel to finish the job of producing something the browser can use.
How can I manually compile a svelte component down to the final javascript and css that sapper/svelte produces.
Ouch, tough one. Hang tight.
What you're missing actually is the "linking", that is resolving import statements in the compiled code to something the browser can use. This is the work that is typically done by the bundler (e.g. Rollup, Webpack...).
These imports can come from user (widget developer) code. For example:
import { onMount } from 'svelte'
import { readable } from 'svelte/store'
import { fade } from 'svelte/transition'
import Foo from './Foo.svelte'
Or they can be injected by the compiler, depending on the features that are used in your component. For example:
// those ones are inescapable (bellow is just an example, you'll
// get different imports depending on what the compiled component
// actually does / uses)
import {
SvelteComponent,
detach,
element,
init,
insert,
noop,
safe_not_equal,
} from 'svelte/internal'
Svelte compiles .svelte to .js and, optionally, .css, but it doesn't do anything with imports in your code. On the contrary, it adds some (but still, doesn't resolve them, it's out of its scope).
You'd need to parse the compiled code to find those imports that, raw from the compiler, probably points to paths on your file system and your node_modules directory, and rewrite them to something that makes sense for the browser -- that is, URLs...
Doesn't seem much fun, does it? (Or too much of it, depending on how you see things...) Fortunately, you're not alone with this need and we've got pretty powerful tooling dedicated precisely to this task: enter the bundler!
Solving the linking problem
One relatively straightforward approach to this problem (more to come, don't get too excited too early) is to compile your widgets, not with Svelte's compiler API, but with Rollup and the Svelte plugin.
The Svelte plugin essentially does what you were doing with the compiler API, but Rollup will also do all the hard work of rewiring imports and dependencies in order to produce a neat little package (bundle) that is consumable by the browser (i.e. that doesn't rely on your file system).
You can compile one widget (here Foo.svelte) using some Rollup config like this:
rollup.config.Foo.js
import svelte from 'rollup-plugin-svelte'
import commonjs from '#rollup/plugin-commonjs'
import resolve from '#rollup/plugin-node-resolve'
import css from 'rollup-plugin-css-only'
import { terser } from 'rollup-plugin-terser'
const production = !process.env.ROLLUP_WATCH
// include CSS in component's JS for ease of use
//
// set to true to get separate CSS for the component (but then,
// you'll need to inject it yourself at runtime somehow)
//
const emitCss = false
const cmp = 'Foo'
export default {
// our widget as input
input: `widgets/${cmp}.svelte`,
output: {
format: 'es',
file: `public/build/widgets/${cmp}.js`,
sourcemap: true,
},
// usual plugins for Svelte... customize as needed
plugins: [
svelte({
emitCss,
compilerOptions: {
dev: !production,
},
}),
emitCss && css({ output: `${cmp}.css` }),
resolve({
browser: true,
dedupe: ['svelte'],
}),
commonjs(),
production && terser(),
],
}
Nothing very extraordinary here... This is basically the config from the official Svelte template for Rollup, minus the parts pertaining to the dev server.
Use the above config with a command like this:
rollup --config rollup.config.Foo.js
And you'll get your browser-ready compiled Foo widget in public/build/Foo.js!
Rollup also has a JS API so you could run this programmatically as needed from a web server or whatever.
Then you'll be able to dynamically import and then use this module with something like this in your app:
const widget = 'Foo'
const url = `/build/widgets/${widget}.js`
const { default: WidgetComponent } = await import(url)
const cmp = new WidgetComponent({ target, props })
Dynamic imports will probably be necessary in your case, because you won't know about the widgets at the time you build your main app -- hence you will need to construct the import URLs dynamically like above at runtime. Note that the fact that the import URL is a dynamic string will prevent Rollup from trying to resolve it at bundle time. This means the import will end up as written above in the browser, and that it must be an URL (not a file path on your machine) that the browser will be able to resolve.
That's because we're consuming the compiled widget with a browser native dynamic import that we need to set output.format to es in the Rollup config. The Svelte component will be exposed with export default ... syntax, that modern browsers natively understand.
Dynamic imports are very well supported by current browsers. The notable exception is the "old" Edge (before it essentially became Chrome). If you need to support older browsers, polyfills are available (many of them actually -- e.g. dimport).
This config can be further automatized to be able to compile any widget, not just Foo. For example, like this:
rollup.config.widget.js
... // same as above essentially
// using Rollup's --configXxx feature to dynamically generate config
export default ({ configWidget: cmp }) => ({
input: `widgets/${cmp}.svelte`,
output: {
...
file: `public/build/widgets/${cmp}.js`,
},
...
})
You can then use it like this:
rollup --config rollup.config.widget.js --configTarget Bar
We're making progress, yet there remains a few caveats and hurdles to be aware of (and maybe optimize further -- your call).
Caveat: shared dependencies
The above approach should give you the compiled code for your widgets, that you can run in the browser, with no unresolved imports. Good. However, it does so by resolving all dependencies of a given widget when it is built, and bundling all these dependencies in the same file.
Said otherwise, all dependencies that are shared between multiple widgets will be duplicated for every widget, very notably the Svelte dependencies (i.e. imports from svelte or svelte/*). This is not all bad, because it gives you very standalone widgets... Unfortunately, this also add some weight to your widgets code. We're talking something like maybe 20-30 kb of JS added to each widgets that could be shared between all of them.
Also, as we will see soon, having independent copies of Svelte internals in your app has some drawbacks we need to take into consideration...
One easy way to extract common dependencies so they can be shared instead of duplicated is to bundle all your widgets in one pass. This might not be practicable for all the widgets of all your users, but maybe it can be doable at individual user level?
Anyway, here's the general idea. You would change the above Rollup configs to something like this:
rollup.config.widget-all.js
...
export default {
input: ['widgets/Foo.svelte', 'widgets/Bar.svelte', ...],
output: {
format: 'es',
dir: 'public/build/widgets',
},
...
}
We're passing an array of files, instead of just one, as the input (you would probably automatize this step by listing files in a given directory), and we're changing output.file to output.dir, since now we're gonna have several files generated at once. Those files will include common dependencies of your widgets that Rollup will have extracted, and that all your widgets will share between them for reuse.
Further perspectives
It would be possible to push even further, by extracting some shared dependencies (say, Svelte...) yourself and make them available as URLs to the browser (i.e. serve them with your web server). This way, you could rewrite those imports in your compiled code to those known URLs instead of relying on Rollup to resolve them.
This would reduce code duplication entirely, saving weight, and also this would allow to have a single version of those dependencies shared among all the widget that use them. Doing so would also relieve the need to build all widgets that share dependencies in one go at the same time, which is alluring... However, this would be pretty (!) complicated to setup, and you would actually hit diminishing returns fast.
In effect, when you're bundling a bunch of widgets together (or even just one) and let Rollup extract the dependencies, it is possible for the bundler to know what parts of the dependencies are actually needed by the consuming code and skip the rest (keep in mind: Rollup was built with tree shaking as one -- if not the one -- of its main priority, and Svelte was built by the same guy -- meaning: you can expect Svelte to be very tree shaking friendly!). On the other hand, if you extract some dependencies manually yourself: it relieves the need to bundle all consuming code at once, but you will have to expose the whole of the consumed dependencies, because you won't be able to know in advance the parts from them that will be needed.
It's a balance you need to find between what is efficient and what is practical, accounting for the added complexity of each solution to your setup. Given your use case, my own feeling is that the sweet spot is either bundling each widget entirely independently, or bundling a bunch of widgets from, say, the same user together to save some weight, as described above. Pushing harder would probably be an interesting technical challenge, but it would reap just little extra benefits, but somewhat exploding complexity...
OK so we now know how to bundle our widgets for the browser. We even have some degree of control on how to pack our widgets entirely standalone, or take on some extra infrastructure complexity to rather share dependencies between them and save some weight. Now, we've got a special dependency to consider, when we decide how we make our pretty little packets (err, bundles): that's Svelte itself...
Mind the trap: Svelte can't be duplicated
So we understand that when we're bundling a single widget with Rollup, all of its dependencies will be included in the "bundle" (just the one widget file in this case). If you bundle 2 widgets in this way and they share some dependencies, those dependencies will be duplicated in each of those bundles. In particular, you'd get 2 copies of Svelte, one in each widget. Likewise, the dependencies of your "main" app that are shared with some widgets will nonetheless be duplicated in the browser. You'll have multiple copies of the same code that will be used by those different bundles -- your app, different widgets...
However, there is something special about Svelte that you need to know: it doesn't support being duplicated. The svelte/internal module is stateful, it contains some global variables that would be duplicated if you have multiple copies of this code (see above). What this means, in practice, is that Svelte components that don't use the same copie of Svelte internals can't be used together.
For example, if you have a App.svelte component (your main app) and a Foo.svelte component (e.g. a user widget) that have been bundled independently, then you can't use Foo in App, or you'd get weird bugs.
This wouldn't work:
App.svelte
<script>
// as we've seen, in real life, this would surely be a
// dynamic import but whatever, you get the idea
import Foo from '/build/widgets/Foo.js'
</script>
<!-- NO -->
<Foo />
<!-- NO -->
<svelte:component this={Foo} />
That's also the reason why you have this dedupe: ['svelte'] option in the official Svelte template's Rollup config... This is intended to prevent bundling different copies of Svelte, which would happen if you ever used linked packages, for example.
Anyway, in your case it is kind of unescapable to end up with multiple copies of Svelte in the browser, since you're probably not wanting to rebuild your whole main app anytime a user adds or changes one of their widget... Except going to great lengths to extract, centralize, and rewrite the Svelte imports yourself; but, as I said, I don't believe this would be a reasonable and sustainable approach.
And so we're stuck.
Or are we?
The problem of duplicated Svelte copies only occurs when the conflicting components are part of the same components tree. That is, when you let Svelte create and manage the component instances, like above. The problem doesn't exist when you create and manage the component instances yourself.
...
const foo = new Foo({ target: document.querySelector('#foo') })
const bar = new Bar({ target: document.querySelector('#bar') })
Here foo and bar will be entirely independent component trees, as far as Svelte is concerned. Code like this will always work, irrelevantly of how and when (and with which Svelte version, etc.) Foo and Bar were compiled and bundled.
As I understand your use case, this is not a major hurdle. You won't be able to embed your users' widgets into your main app with something like <svelte:component />... However, nothing prevents you from creating and managing the widget instances in the right place yourself. You can create a wrapper component (in your main app) to generalize this approach. Something like this:
Widget.svelte
<script>
import { onDestroy } from 'svelte'
let component
export { component as this }
let target
let cmp
const create = () => {
cmp = new component({
target,
props: $$restProps,
})
}
const cleanup = () => {
if (!cmp) return
cmp.$destroy()
cmp = null
}
$: if (component && target) {
cleanup()
create()
}
$: if (cmp) {
cmp.$set($$restProps)
}
onDestroy(cleanup)
</script>
<div bind:this={target} />
We create a target DOM element from our main app, render an "external" component in it, pass down all the props (we're proxying reactivity), and don't forget to cleanup when our proxy component is destroyed.
The main limitation of such an approach is that Svelte context (setContext / getContext) of the app won't be visible to the proxied components.
Once again, this doesn't really seem like a problem in the widget use case -- maybe even better: do we really want the widgets to have access to every bits of the surrounding app? If really needed, you can always pass bits of context down to the widget components via props.
The above Widget proxy component would then be used like this in your main app:
<script>
import Widget from './Widget.svelte'
const widgetName = 'Foo'
let widget
import(`/build/widgets/${widgetName}.js`)
.then(module => {
widget = module.default
})
.catch(err => {
console.error(`Failed to load ${widgetName}`, err)
})
</script>
{#if widget}
<Widget this={widget} prop="Foo" otherProp="Bar" />
{/if}
And... Here we are? Let's sum it up!
Summary
Compile your widgets with Rollup, not Svelte compiler directly, to produce browser ready bundles.
Find the right balance between simplicity, duplication and extra weight.
Use dynamic imports to consume your widgets, that will be built independently of your main app, in the browser.
Do not try to mix together components that don't use the same copy of Svelte (essentially means bundled together, except if you've launched into some extraordinary hack). It might looks like it works at first, but it won't.
Thanks to the detailed post by #rixo I was able to get this working. I basically created a rollup.widget.js like this:
import json from '#rollup/plugin-json';
import resolve from '#rollup/plugin-node-resolve';
import commonjs from '#rollup/plugin-commonjs';
import svelte from 'rollup-plugin-svelte';
import path from "path";
import fs from "fs";
let basePath = path.join(__dirname,'../widgets');
let srcFiles = fs.readdirSync(basePath).filter(f=>path.extname(f) === '.svelte').map(m=> path.join(basePath,m ));
export default {
input: srcFiles,
output: {
format: 'es',
dir: basePath,
sourcemap: true,
},
plugins: [
json(),
svelte({
emitCss: false,
compilerOptions: {
dev: false,
},
}),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs()
]
}
Then generate the svelte components from the database and compile:
const loadConfigFile = require('rollup/dist/loadConfigFile');
function compile(widgets){
return new Promise(function(resolve, reject){
let basePath = path.join(__dirname,'../widgets');
if (!fs.existsSync(basePath)){
fs.mkdirSync(basePath);
}
for (let w of widgets){
if (w.config.source){
let srcFile = path.join(basePath,w.name + '.svelte');
fs.writeFileSync(srcFile,w.config.source);
console.log('writing widget source file:', srcFile)
}
}
//ripped off directly from the rollup docs
loadConfigFile(path.resolve(__dirname, 'rollup.widgets.js'), { format: 'es' }).then(
async ({ options, warnings }) => {
console.log(`widget warning count: ${warnings.count}`);
warnings.flush();
for (const optionsObj of options) {
const bundle = await rollup(optionsObj);
await Promise.all(optionsObj.output.map(bundle.write));
}
resolve({success: true});
}
).catch(function(x){
reject(x);
})
})
}
And then consume the dynamic widget as #rixo proposed:
<script>
import {onMount, onDestroy, tick} from 'svelte';
import Widget from "../containers/Widget.svelte";
export let title = '';
export let name = '';
export let config = {};
let component;
let target;
$: if (name){
loadComponent().then(f=>{}).catch(x=> console.warn(x.message));
}
onMount(async function () {
console.log('svelte widget mounted');
})
onDestroy(cleanup);
async function cleanup(){
if (component){
console.log('cleaning up svelte widget');
component.$destroy();
component = null;
await tick();
}
}
async function loadComponent(){
await cleanup();
let url = `/widgets/${name}.js?${parseInt(Math.random() * 1000000)}`
let comp = await import(url);
component = new comp.default({
target: target,
props: config.props || {}
})
console.log('loading svelte widget component:', url);
}
</script>
<Widget name={name} title={title} {...config}>
<div bind:this={target} class="svelte-widget-wrapper"></div>
</Widget>
A few notes/observations:
I had much better luck using rollup/dist/loadConfigFile than trying to use rollup.rollup directly.
I went down a rabbit hole of trying to create both client and server globals for all of the svelte modules and marking them as external in the widget rollup so that everything used the same svelte internals. This ended up being a mess and gave the widgets access to more than I wanted.
If you try to embed your dynamically compiled widget in your main app with <svelte:component it will sort of work but give you the dreaded outros.c undefined error if you try to reference a dynamic widget from another. After this happens reality breaks down and the app is in a strange state.
#rixo is always right. I was warned about each of these things in advance and the result was exactly as predicted.

Send a constant from server to client in meteorjs

Scenario: You want to put the build id in the footer of a meteor app. The client will need a reference to process.env.BUILD_ID from the server.
Common answer: Save it in a collection. On meteor start, save the current build id to a collection and load that on the client.
Problem with the common answer: What if you have multiple containers running? Say for instance you're using kube to update the version. You have version abc running, and then kube starts pushing up def. In the process of starting each def container, it sets the current version in the db to def. Yet the abc containers are still running. If you hit one of those containers, it will report def as the build id. Even though thats wrong. Additionally, what if the kube update fails, and you decide to cancel the push to the new version and keep the old containers running. Until the next container restart, youll be serving the abc content but telling the user its version def.
What would be the proper way to store a constant and match it on the client without lying about its true value?
Meteor methods are the way to get data from the server (as opposed to the using publications to get from the database). I recommend using ValidatedMethod for extra modularity and nicer validation.
Define the following method in your project and make sure it is imported to the server on server startup:
import { Meteor } from 'meteor/meteor';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
const getAppVersion = new ValidatedMethod({
name: 'getAppVersion',
validate: null,
run({}) {
return process.env.BUILD_ID;
},
});
export default getAppVersion;
Then in the frontend code:
import getAppVersion from '/imports/api/methods/getAppVersion.js';
getAppVersion.call({}, (err, res) => {
if (res) setFooterVersion(res);
});
You can throw it in a store:
// in store.js
export default const store = {
buildId: '',
};
// then in buildId.js:
import store from './store';
Meteor.call('getBuildId', (res, err) => store.buildId = res);
// in someplace like server/methods.js:
Meteor.methods({
'getBuildId'() {
return process.env.BUILD_ID;
},
});
// then, anywhere you need the build id:
import store from './store';
store.buildId; // outputs build id
If you need to make it reactive, you can make the store object a ReactiveDict, then put the Meteor.call part within Tracker.autorun for what you want it to react to. You can alternatively put it in something like a setInterval to run every 10 minutes or so. Of course, you can always store it in the db too -- but for your scenario, you can try to create an additional column like kubeNode and pass in the server id from kubernetes into the db, so when you call it again, you call it with a reference to the appropriate k8s node.

How to have multiple reducers trigger updates based on a common set of actions without repeating yourself?

I would like many different redux actions in my app to all trigger common functionality in a specific reducer. I would like to avoid having to either repeat some flag in every action creator (like doThing: true) that the reducer looks for. I also don't want to have to have the reducer just look for every individual action that falls into this category, since that also requires someone to remember to do this every time they add a new action, like adding the flag.
I was thinking of dispatching a second action every time one of these actions is going to be dispatched. This would not be hard to do, but I'd rather not have 2 actions dispatched every time one thing happens. It seems like it would pollute the state history.
Is there a common way of solving this problem?
For more context to my specific problem, the specific feature is related to the API client my app uses to talk to our API. On every successful response, we'd like to do something in a reducer to update the state, and on every failed response, we'd like to do something else.
There are many different success and failure actions (such as ITEM_FETCH_SUCCESS or WIDGET_UPDATE_FAILURE), and adding a flag to all of them would be hard to remember to do when new ones are added.
Since all api requests go through a single function, that function COULD dispatch generic REQUEST_SUCCESS and REQUEST_FAILURE actions. But this would mean every response from the server would dispatch 2 actions (REQUEST_SUCCESS and ITEM_FETCH_SUCCESS). This is obviously not ideal since it would mean many more actions in my state history.
Assuming the generic REQUEST_SUCCESS and REQUEST_FAILURE actions are updating their own specific portions of the state-tree then it is fine to dispatch them as distinct actions. Doing this does not necessarily imply the pollution of your state history but can simply be a better description of the app's intentions.
ITEM_FETCH_SUCCESS: Change state for item
REQUEST_SUCCESS: Change state for request
WIDGET_UPDATE_FAILURE: Change state for widget
REQUEST_FAILURE: Change state for request
You can see that whilst the actions are intimately related, they are not necessarily the same thing as they change different parts of the state tree.
Accepting this, the question is: How best to implement the action-pairs so that adding new actions does not mean remembering to add its corresponding REQUEST_* partner?
I would consider applying a simple redux middleware component. This could intercept the return from your api and dispatch the appropriate REQUEST_* action automatically.
Here is an example from some live code. This middleware intercepts a disconnect event raised by a websocket and automatically dispatches a custom action as a result. It at least shows the principle:
//Dispatch a disconnect action when the websocket disconnects
//This is the custom action provided by the middleware
import io from 'socket.io-client'
import { actions } from './action'
const websocket = ({ websocketUrl }) => store => {
const socket = io(websocketUrl)
socket.on('disconnect', () => store.dispatch(actions.disconnect()))
}
export default websocket
//Apply the custom middleware via the redux createStore function
//Also include the thunk middleware because it is useful
import { applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import websocket from './middleware'
function websocketize (opts) {
return createStore => (reducers, initial, enhancer) => {
const middleware = applyMiddleware(thunk, websocket(opts))
return createStore(reducers, initial, middleware)
}
}
export default websocketize
// Create the top-level redux store passing in the custom middleware enhancer
const opts = {websocketUrl: env.WEBSOCKET_URL}
const store = createStore(reducers, websocketize(opts))
This implementation keeps everything inside your reducers as opposed to having logic outside in an interception(middleware). Both ways are valid.
Try a sub-reducer pattern. I usually feel gross when I see it used(because it is usually used wrong), but your situation sounds perfect.
Extract duplicate functionality out of your reducers to one single
sub-reducer.
Then pass that reducer as a function to all others that need it.
Then pass the action and state onto the sub-reducer.
The sub-reducer does it's thing and returns that slice of state to
your parent reducer to allow you to do whatever you want with it
there (ie return it, mutate some more, some logic).
Also if you are tired of worrying about typing out "all the stuff" for async then I highly recommend you try out redux-crud.js
It also is possible and a simple way to do that would be to give every action to one reducer and let it do that common mutation, in a single case:
case actionOne
actionTwo
actionThree
actionFour: {
//do common stuff here
}
. But you said it is not duplicated, it is similar, which means your case becomes complicated by branching logic. I also don't recommend this. Keep cases simple so you can easily catch invalid mutations. This should be a super power of redux that it is easy to catch mutation errors. And for this reason and many others I don't recommend normalizing data in the front end.

Resources