The process.env.variable works in local development but after deploying to Heroku it stops working. I use nuxt.js [closed] - heroku

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 13 days ago.
Improve this question
process.env.variable is working in local development but after deploying to Heroku its not working again. I use nuxt.js
The value shows as undefined:
https://prnt.sc/l1GMK6xAQP-V
The config var is correct. I restarted the app. still nothing.

Got it!
.env file:
BASE_URL=http://localhost:3000
ORIGIN_HEADER=localhost
nuxt.config.js
export default {
publicRuntimeConfig: {
baseUrl: process.env.BASE_URL,
originHeader: process.env.ORIGIN_HEADER
}
}
.vue component:
<template>
<div>
<p>Base URL: {{ baseUrl }}</p>
<p>Origin Header: {{ originHeader }}</p>
</div>
</template>
<script>
export default {
computed: {
baseUrl() {
return this.$config.baseUrl
},
originHeader() {
return this.$config.originHeader
}
}
}
</script>
publicRuntimeConfig is intended to be used for configuration values that are safe to be publicly exposed, such as API endpoints or public-facing URLs.
privateRuntimeConfig, on the other hand, is intended for configuration values that should be kept private and should not be publicly exposed, such as API keys or secrets.

Related

CF2021 Getting Started with WebSockets (cfwebsocket)

I am just getting started with cfwebsockets and I am having a bit of trouble. What I found from online, is that since this is public-facing application, it is best to use a websocket proxy.
CF2021, Version: 2021.0.03.329779
Ubuntu 20.04LTS
Steps so far:
I added the websocket package to my server and enabled web-sockets. Restarted Server.
Added this to my application.cfc:
<cfset this.wschannels = [ {name="chat"} ] />
(the application has a this.name= set from a variable as well
3. created a simple page that has this:
<cfwebsocket
name="wSocketObj"
onMessage="wsOnMessage"
onOpen="wsOnOpen"
onClose="wsOnClose"
onError="wsOnError"
subscribeTo="chat"
secure="true"/>
<script type="text/javascript">
wsOnMessage = function(aEvent,aToken) {
console.log('wsOnMessage',aevent);
var message = ColdFusion.JSON.encode(atoken);
var txt=document.getElementById("myDiv");
txt.innerHTML +=message +"<br>";
}
wsOnOpen = function() {
alert("wsOnOpen Connection is open");
}
wsOnClose = function() {
alert("wsOnClose Connection Closed");
}
wsOnError = function() {
alert("wsOnError!");
console.log(arguments);
}
sendMessage = function() {
var text = window.prompt("Enter some text","");
if (text) {
wSocketObj.publish("chat", text);
}
}
</script>
<cfdiv id="myDiv"></cfdiv>
<div id="myChatArea"></div>
<input type="text" id="myMessage" /><input id="myButton" type="button" value="Send Message" onClick="sendMessage()" />
got this in the Chrome Console:
WebSocket connection to 'wss://myhost.mydomain.com:8555/cfusion/cfusion' failed:
CFWebSocketWrapper.open # cfwebsocketCore.js:21
init # cfwebsocketChannel.js:49
_cf_websockets_init_6322652258206397 # client.cfm:35
fire # cfajax.js:1214
$E.windowLoadHandler # cfajax.js:1321
cfwebsocketCore.js:54 Uncaught TypeError: Cannot set properties of undefined (setting 'readyState')
at WebSocket.wsConnection.onerror (cfwebsocketCore.js:54:29)
thinking it was a firewall issue, I disabled it in Ubuntu
It was at this point that it seemed to be that I needed to configure a proxy.
I ran /opt/ColdFusion/cfusion/bin/wsproxyconfig.sh from the command line and verified that both the line in apache2.conf was there and the folder "/opt/ColdFusion/config/wsproxy/1/mod_wsproxy.conf" was created. Restarted apache2 and CF2021
Same Result
Realized I need to change the websocket config in CF Admin,
When I select the Use Proxy in CF Admin, the Save Changes button disappears and I cannot save it.
Obviously I am missing something very fundamental here. Any help that anyone can provide would be appreciated.
thanks in advance

Does not work image optimization on an external website for #nuxt/image

Can't optimize images on the fly from a remote website.
Example:
<nuxt-img
src="https://upload.wikimedia.org/wikipedia/ru/b/b7/Enterthematrix.jpg"
format="webp"
/>
// nuxt.config.js
export default {
image: {
domains: ['https://upload.wikimedia.org']
}
}
What am I doing wrong?
I had the same question, when I review the document and finally found out.
Your domain is not valid: you don't need to include the http protocol so you should write like this (without http protocol)
export default {
image: {
domains: ['upload.wikimedia.org']
}
}

how to access vue.js api key in laravel application

hello there i am trying to access my youtube api key located in the .env file from within this code:
<template>
<div class="YoutubeDash__wrapper">
<video-group :videos="videos"></video-group>
</div>
</template>
<script>
import VideoGroup from './VideoGroup.vue';
import Search from './Search';
export default {
components: {
VideoGroup
},
created(){
Search({
apiKey: process.env.VUE_APP_SECRET,
term: 'laravel repo'
}, response => this.videos = response);
},
data(){
return {
videos: null
}
}
}
</script>
According to the documentation using env variables with vue.js. Everything seems to be correct. In my .env file i say: VUE_APP_SECRET=xxxxxxxxxxxxxxx, what am i missing here ?
I get this error message:
app.js:37809 Error: YouTube search would require a key
Any tips are welcome! Thanks a lot!
We need to work with a small amount of information here so I am going to make a few assumptions (based on the tags) mostly that you are using laravel and laravel-mix to compile your resources.
For laravel(-mix) to make your .env variables accessible by JS you need to prefix them with MIX_ i.e. MIX_VUE_APP_SECRET. This will make your variable accessible as process.env.MIX_VUE_APP_SECRET.
I prefer excluding laravel-mix from this process.
Usually, in my blade entry-point I use meta tags:
<meta name="myVal" content="{{ config('<any-config-key>') }}">
<any-config-key> can be any laravel configuration key including those taken from .env.
Then, in my javascript I do something like:
const setVueGlobal = (metaHeaderName, vueGlobalName) => {
let value = document.head.querySelector('meta[name="' + metaHeaderName + '"]').content;
if (!value) {
console.error('some error msg');
return null;
}
Vue.prototype[vueGlobalName] = value;
return value;
};
setVueGlobal('myVal', '$myVal');
Finally, accessing using this.$myVal

Laravel environment variable with React: is this a good practice?

I have a React app which makes API requests to a Laravel backend.
My app is hosted on Heroku (I do not know if it changes something for my question).
I would like to differentiate a production and a local environment for these requests. I do it as follows.
In my "welcome.blade.php", I add this meta tag:
<meta name="app_env" content="<?php echo env("APP_ENV") ?>" />
The APP_ENV contains either "production" or "local".
In my React app, I have this script:
export let urlApi = (document.querySelector("[name=app_env]").content === "production" ) ?
"https://laravel-react.herokuapp.com/api"
:
"http://localhost/laravel_react/public/api"
;
And I import this function in each component which needs it:
import { urlApi } from './../findUrlApi';
// .....
return fetch(`${urlApi}/products`,{
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer "+localStorage.getItem("token")
}
})
It works fine.
But my question is, is it a good practice? (I am a beginner in React).
I don't think that it is a good practice. Environment variables (like APP_ENV and API URLs) should not reside in the source code.
However, you could store them like usual in Laravel .env file but by prefixing the key with MIX_. For example: MIX_API_URL=http://localhost. Every MIX_* variables in .env file will be exposed to your React application. Then, you could get MIX_API_URL value from your React application by calling process.env.MIX_API_URL.
Updated Laravel .env file
...
MIX_APP_ENV=production (or local) # Should be same as APP_ENV
MIX_API_LOCAL_URL=http://localhost/laravel_react/public/api
MIX_API_PRODUCTION_URL=https://laravel-react.herokuapp.com/api
In React components that need it
const { MIX_APP_ENV, MIX_API_LOCAL_URL, MIX_API_PRODUCTION_URL } = process.env;
const apiUrl = MIX_APP_ENV === 'local'? MIX_API_LOCAL_URL: MIX_API_PRODUCTION_URL;
return fetch(apiUrl + '/products', { ... });
If calling process.env.MIX_API_URL does not work and you are running npm run watch, try restarting npm run watch and hard reload your browser.
Reference
Laravel Documentation - Compiling Assets (Mix) - Environment
Variables

i18n wants to load an unspecified translation

I'm currently working on an Aurelia project (web framework like Angular2).
I followed the guide on their github account but encountered a problem.
First, the browser returned me this error:
GET http://localhost:9000/src/locale/nl/translation.json?_=1450946571510 404 (Not Found)
Secondly, I'm using two languages in my application: Dutch (nl-BE) and French (fr-BE).
Here is how my folder structure looks like:
src (inside root)
.. locale
..... fr-BE
........ translation.json
..... nl-BE
........ translation.json
Here is what my full main.js file looks like:
import 'bootstrap';
import {I18N} from 'aurelia-i18n';
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.globalResources('converters/dateFormat')
.plugin('components/index')
.plugin('plugins/index')
.plugin('aurelia-i18n', (instance) => {
instance.setup({
resGetPath: 'src/locale/__lng__/__ns__.json',
lng: 'nl-BE',
attributes: ['t', 'i18n'],
getAsync: true,
sendMissing: false,
fallbackLng: 'fr-BE',
debug: false
});
});
aurelia.start().then(a => a.setRoot());
}
I'm trying to setup a hello world where I have my view and viewmodel set like this:
import {inject} from 'aurelia-framework';
import {I18N} from 'aurelia-i18n';
#inject(I18N)
export class EntryDetails {
constructor(i18n){
this.i18n = i18n;
this.i18n.setLocale('nl-BE').then(() => console.log('test'));
}
}
And my view:
<template>
<span t="hello"></span> <span t="world"></span>
</template>
The problem is not that it's not working. The problem is that I'm getting an error that states my folder nl is missing in my locale folder. But I never specified that anywhere..
That is how i18next resolves translation files.
The lookup order for keys is always:
nl-BE language + country
nl language only
fallback thats defined in options.fallbackLng (en) (string or array of fallback language)
loaded resources:
locale/en/translation.json
locale/nl/translation.json
locale/nl-BE/translation.json
http://i18next.com/translate/#resolve
So you need to have nl/translation.json, even if it is not specified in config. It can be just empty but valid json file with content {}

Resources