I'm trying to hide a URL from the user that tells the app which endpoint it should use.
In nuxt, I have 2 plugins:
[
"~/endpoint.server.js",
"~/api.js",
]
In endpoint.server.js, I'm fetch an endpoint for all subsequent requests to run on if my app in production. I don't want this URL (private.com below) to be public, hence running it on the server only.
import axios from 'axios';
export default async function ({ $axios, redirect, app }, inject) {
// Set staging
let staging = !!app.$env.STAGING;
app.$staging = staging;
inject('staging', staging);
if(app.$staging){
inject('baseURL', "https://staging-data.com");
}else{
// console.log("Setting custom endpoint");
let { data } = await axios.get("http://private.com");
inject('baseURL', data.endpoint);
}
}
Note how I inject the $baseURL. Then, inside of api.js I have the following:
export default async function ({ $axios, redirect, app }, inject) {
// Create a custom axios instance
const api = $axios.create();
// Set the base URL
api.setBaseURL(app.$baseURL);
// Inject to context as $api
inject('api', api);
}
In all of my components I then use this special axios instance:
async asyncData({ $axios, route, app }) {
const page = await app.$api.$get( "/" );
return { page }
},
On first page load, this all works great. However, on subsequent page loads, the $api baseURL is set to localhost in the client.
I'm pretty sure in endpoint.server.js I'm correctly using the combined inject – https://nuxtjs.org/guide/plugins/#combined-inject – how come my axios instance can't use the baseURL?