This article mainly introduces the Cookies issue of Vue SSR. The content is quite good. I will share it with you now and give it as a reference.
Once a website involves multiple users, it is difficult to escape from cookies. The cookies of Vue SSR are really a big problem. From the beginning of playing SSR to now, I have come up with a total of 3 solutions, from the earliest one to inject Cookies into the state, to the first one to inject Cookies into global, to the current one to inject Cookies into the asyncData method of the component.
With the upgrade of Vue, the first solution has been It is no longer applicable. The second option also has many limitations, so I thought of the third option. Let’s talk about the specific implementation method:
The first option
The first solution is no longer applicable, I won’t go into details here
The second solution
Idea: Inject cookies into ssr context, then read it when requesting the api, and then append it to the header of axios
1, first add cookies to the context in server.js
1 2 3 4 5 6 7 8 9 10 11 12 |
|
After that, Vue will add context to global.__VUE_SSR_CONTEXT__
2, and read cookies in api.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Why can this be done?
By default, Vue for each rendering, the bundle renderer will create a new V8 context and re-execute the entire bundle. The application code is isolated from the server process, so each accessed user context is independent and will not affect each other.
But starting from Vue@2.3.0, runInNewContext is added to the options of the createBundleRenderer method. option, use runInNewContext: false, the bundle code will run in the same global context as the server process, so we can no longer put cookies in global, because this will cause all users to share the same cookies.
Why not do this now?
Then let’s continue setting runInNewContext to true, wouldn’t it? Of course it is possible, but re-creating the context and executing the entire bundle is still quite expensive , especially when the application is very large.
Take my own blog as an example. Before, I only rendered 5 routing components, and the rps of loadtest was about 50. But later, I added 12 routing components in the background. After adding SSR, the rps dropped directly to single digits...
So the third solution appeared
The third solution
Idea: Inject Cookies as parameters into the asyncData method of the component, and then use the method of passing parameters to pass the cookies to the API. I have to say that this method is very troublesome, but this is the better thing I can think of. Method
Step 1:
Still in server.js, inject cookies into context
1 2 3 4 5 6 7 8 9 10 11 |
|
Step 2 :
In entry-server.js, pass cookies as parameters to the asyncData method
1 2 3 4 5 6 7 8 9 10 11 |
|
Step 3:
In the component, use cookies as parameters for Vuex actions
1 2 3 4 5 6 7 8 |
|
Step 4:
Use cookies as parameters in Vuex For api
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
, you must pay attention here. The state must be initialized with the function return value, otherwise it will cause all users to share the state
Step 5:
Receive cookies in the api and add them to the headers of axios
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
The fourth option
Step 1:
Still in server.js, inject cookies into context
1 2 3 4 5 6 7 8 9 10 11 |
|
Step 2 :
In entry-server.js, pass cookies as parameters to the api.setCookies method. What is api.setCookies followed by
1 2 3 4 5 6 7 8 9 10 |
|
Step 3:
Rewrite api.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Step 4:
No more step 4 , just introduce the api call directly
If you have not re-packaged axios, you can also omit the fifth step and directly give the cookies to axios in the fourth part
Option 2 is specific Example: https://github.com/lincenying/mmf-blog-vue2-ssr
Option 3 Specific example: https://github.com/lincenying/mmf-blog-vue2-pwa-ssr
Specific example of option 4: https://github.com/lincenying/mmf-blog-vue2-pwa-ssr
In summary, if your project is not big, just use option 2. , the project has many pages, and most of the pages are the same for every user, you can consider option 3, or if you have any better methods, welcome to discuss
Vue SSR needs SEO, and every user The content you see is consistent. With caching, it will be a very good experience...
The above is the entire content of this article. I hope it will be helpful to everyone's learning. More related Please pay attention to the PHP Chinese website for content!
Related recommendations:
About the method of Vue2 SSR caching Api data
vue2.0 project implements routing jump Method introduction
The above is the detailed content of Analyze issues related to Cookies of Vue SSR. For more information, please follow other related articles on the PHP Chinese website!