R
R
Roman2019-06-07 10:01:36
Vue.js
Roman, 2019-06-07 10:01:36

How to pass data from Nuxt (universal) serverMiddleware "to the top" to display on the page?

Good afternoon.
I made a custom middleware on the server side following the example https://nuxtjs.org/api/configuration-servermiddleware/ and I have a few questions:
1. Let's say I generated data inside the middleware - how can I display it on the browser page now? How to pass above?
2. Tried through $store (Vuex) - but nothing happened. I understand that I don’t understand something basic here, but I don’t understand what exactly))
3. Why do I have a variable in my middleware (in the example, this is the api/logger.js file) why is this? Googled, people write that it seems to work for them ... but this is not accurate)) Tell me which way to dig, or maybe a link to an example. Pts thank you in advance.
process.server == undefined

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dima Pautov, 2019-06-07
@bootd

There are 2 kinds of middleware - it's just middleware and serverMiddleware.
The usual middleware is for vue router, both on the client and on the server, i.e. work only for vue, serverMiddleware for express, which we have under the hood initially.
But first the express and its serverMiddleware (if you have one) work, and only then vue and its router.
Nothing can be passed from serverMiddleware to vue and similar modules, because vue itself is not yet initialized so to speak.
From the middleware you can pass data to vue storage
process.server only works when the content is generated on the server. If you have itundefined, so the variable worked on the client, and it’s either impossible to use process.server in serverMiddleware (I didn’t check it), or it’s pointless, because this is a server and only a server

R
Roman Tataurov, 2020-06-04
@webhive

It's probably a bit late, but there is a method in Store nuxtServerInitthat passes store and server context as parameters. There you can just put the data from the server in the store.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question