-
Notifications
You must be signed in to change notification settings - Fork 244
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
How to cache get request on SSR? #99
Comments
Hey. Actually, there was a long discussion #8 about caching at axios level. This is a won't fix for several reasons. I highly recommend utilizing a caching layer (Like nginx) in the top of your API server. Cache is so hard to manage for production. Especially for dynamic content like API. |
@pi0 Thank you for your fast reply. I have nginx proxy but I wanted to speed it up even more. No request is faster than request to proxy. But I understand the complexity of this and your response. Just for curiosity, is it possible to do this just with interceptors? |
Yes. axios-extensions suggested by @homerjam can be used. I've not tested this solution but may work: Please confirm if you succeed. We ca add a wiki page for axios level caching: yarn add axios-extensions
npm i axios-extensions
import { cacheAdapterEnhancer, throttleAdapterEnhancer } from 'axios-extensions'
export default function ({ app }) {
const defaults = app.$axios.defaults
// https://github.com/kuitos/axios-extensions
defaults.adapter = throttleAdapterEnhancer(cacheAdapterEnhancer(defaults.adapter, true))
} |
Closing due inactivity. |
@pi0 import { cacheAdapterEnhancer } from "axios-extensions"
import LRUCache from "lru-cache"
const ONE_HOUR = 1000 * 60 * 60
export default function({ app }) {
const defaultCache = new LRUCache({ maxAge: ONE_HOUR })
const defaults = app.$axios.defaults
defaults.adapter = cacheAdapterEnhancer( defaults.adapter, false, "useCache", defaultCache )
} What I also noticed that above function is being call on every call, that means cache keeps being reset each time. Where is the best place to initialise axios I use nuxt-axios with nuxt-proxy and axios-extensions and do call like this:
|
@husayt Were you or anyone else able to solve the caching? |
Here is what I have working:
and this is the call
|
@husayt i've tried your solution but i'm still seeing requests to my api made. |
@joernroeder try this syntax
|
@husayt implemented your solution and it seems to be working well ! |
@husayt How Is it possible to know if the response is coming from the cache or the server? How we can verify?
|
Hi @hardikshah91, DISCLAIMER: The informations I'm going to provide you are the one I've understood on my own and might eventually be incorrect. There is going to be two caches, one server side and one client side. The one server side is going to be used and the data are going to be stored in for server-side requests, such as the The requests that are going to be executed client side are going to be able to take advantage of the cache data from the server if the same request was previously made. If the request is made by the client the cache is going to be client side and thus only profitable to a specific client. To ensure the cache was working, since the cache is supposed to be much faster you're supposed to be seeing a noticeable difference, you could use Hope I've answered some of your questions ! |
Hello, My axios plugin
In my method
And I can't switch off the cache. What I am missing? |
browser works fine but server-side seems to run the entire plugin file so it still reset cache, does anyone know why? |
fixed by passing on defaultCache from koa server to nuxt plugin |
@wizardpisces could you share how you did that? |
@mlg-dan the anwer is just a scroll away to the top of the page |
@felixdenoix I am seeing very inconsistent usage of the cache on the server (call in nuxtServerInit). sometimes its takes a few requests before it starts returning from cache, other times I can never get cache to return. it looked like @wizardpisces was seeing the same thing and fixed it by moving defaultCache out of the plugin, but im not sure how he did that. |
@mlg-dan the cache is only serving data in case of server-side requests: see this response #99 (comment) |
Hi guys, so the only way I found to use global cache in nuxt is to add express middlware adding cache object to request like this, so you can use it in plugin ...
anyone has better ideas? |
Hi, I'm sorry I don't get it. Each time I call the server, the cache is reinitialized. Has anyone a good example to give ? Thanks. |
create cache and pass it with in context
add to axios instance
|
I have no 'server' directory and if I add one and create index.js in it, it doesn't seem to be taken into account. |
you need to use custom server for that you can create new nuxt app, and select custom server feature to get all boilerplate code |
exactly! |
Ok thanks. I undertand better. |
Here is working solution with latest Nuxt 2.11, using locally defined module. First add a local module to nuxt.config.js modules: [
"@/modules/axCache",
...
] Then // modules/axCache.js
import LRU from "lru-cache"
export default function(_moduleOptions) {
const ONE_HOUR = 1000 * 60 * 60
const axCache = new LRU({ maxAge: ONE_HOUR })
this.nuxt.hook("vue-renderer:ssr:prepareContext", ssrContext => {
ssrContext.$axCache = axCache
})
} and // plugins/axios.js
import { cacheAdapterEnhancer } from "axios-extensions"
import LRU from "lru-cache"
const ONE_HOUR = 1000 * 60 * 60
export default function({ $axios, ssrContext }) {
const defaultCache = process.server
? ssrContext.$axCache
: new LRU({ maxAge: ONE_HOUR })
const defaults = $axios.defaults
// https://github.com/kuitos/axios-extensions
defaults.adapter = cacheAdapterEnhancer(defaults.adapter, {
enabledByDefault: false,
cacheFlag: "useCache",
defaultCache
})
} Note, this works for both server/client sides and can be configured to work only on one side. |
hi @husayt i tried that, client side is working but not working on server side. when i refresh page still calling api. |
@husayt I tried both solutions on static generated mode but my build still calls my API like crazy @pi0 I can't use nginx on my client infrastructure but I can probably cache my /generate Wordpress endpoint using Transient API or WP REST Cache plugin but it will still look like my servers is DDOS'ed right? |
@pi what is the best up to date solution? |
Here is how I made it work with axios-cache-adapter. modules/axios-cache.jsimport { setupCache } from 'axios-cache-adapter'
export default function () {
const cache = setupCache({
// See https://github.com/RasCarlito/axios-cache-adapter/#options
})
this.nuxt.hook('vue-renderer:ssr:prepareContext', (ssrContext) => {
ssrContext.$axiosCache = cache
})
} plugins/axios-cache.server.jsexport default function ({ $axios, ssrContext }) {
$axios.defaults.adapter = ssrContext.$axiosCache.adapter
} nuxt.config.js{
plugins: [
'~/plugins/axios-cache.server.js',
],
modules: [
'~/modules/axios-cache.js',
],
} ExplanationWe cannot use a plugin alone because it's called for each SSR request; that's why we need the module, which is only called once at startup. Note: all the credit goes to this answer, I only adapted it to axios-cache-adapter. |
It is dos't work for me. Each new request leads to calls to the server. The settings have been adjusted, but it doesn't help. Please help me where to dig.
|
How can set dynamic |
For anyone interested, I ended up discarding @nuxtjs/axios and using directly Axios myself. This way I can write the plugin as follows (here it installs HTTP and DNS caches): // plugins/axios.server.js
import axios from "axios"
import { registerInterceptor } from 'axios-cached-dns-resolve'
import { cacheAdapterEnhancer } from 'axios-extensions';
var myaxios = null
export default function({ $config }, inject) {
if (!myaxios) {
myaxios = axios.create({
baseURL: $config.baseURL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter)
})
registerInterceptor(myaxios)
console.log("Axios caches installed")
}
inject("axios", myaxios)
} This way the Axios instance is available as $axios in the Nuxt context, as before. Note that the Axios API differs a bit from @axiosjs/nuxt API. Quite hacky if you ask me, but it works nevertheless. Probably it would be a good idea to somehow integrate this further in Nuxt or @nuxtjs/axios, because "nuxt generate "usually performs a lot of redundant DNS and HTTP queries. For example Node never caches DNS queries. |
axios-extensions |
1 similar comment
axios-extensions |
Hi!
I want to speed up server side rendering.
What is the best way to cache GET request on the Server Side?
I want axios to:
Any hints would be helpful, thanks!
The text was updated successfully, but these errors were encountered: