How to separate Meteor project into modules - meteor

My Meteor project has two part:
Home page: can be visited by anyone.
Admin Console: provide some management tools to administrator only
When I running this project in production mode:
$ meteor --production
Meteor will combine all the client side code into one big file, that make "Admin Console" code also delivered to normal visitors.
Is there any way to "minify" javascript/css files into two files?
one for normal visitor, one for admin?
I also pack this project into Android package, "Admin Console" code also exists in apk file, how can I exclude them ?
Thanks.

You can use meteor dynamic imports to only download code to the client based on the view the client is using. I have not tried this yet, but it should be possible to not load files dynamically based on the user's role.
Something like:
//must be called inside a meteor method or publish function
async function adminOnlyApi() {
if(Roles.userIsInRole(Meteor.userId, ['admin']) {
return await import('path/to/lib');
}
return {};
}
Assuming the lib exports a default object with a key 'foo' and value 'bar', calling adminOnlyApi.foo when logged in as admin should return 'bar', and return undefined when logged in with any other user...

Related

Getting process.env to work with HERE API keys

I'm building a React geographic todo list, similar to this one. I was using a Mapbox API for the mapping component, but I'd like to switch it over to HERE's Map Image API. I've set up the map on my app, but I'd like to hide my app_id and app_code in the .env file I had previously created. Is this possible? I know it's also possible to restrict which domains have access to the key/code, but I wanted to keep the keys hidden in a .env file for peace of mind.
Here is my .env file (without the keys, of course):
REACT_APP_HERE_ID={my app_id}
REACT_APP_HERE_CODE={my app_code}
And here is where I'm introducing it into my App:
const app_id = process.env.REACT_APP_HERE_ID;
const app_code = process.env.REACT_APP_HERE_CODE;
When I console.log app_id and app_code, they both return undefined. The previous API key I had from Mapbox worked fine in my .env file, but I can't seem to get the app_id and app_code from HERE to be recognized the same way.
Am I missing something? Is it easier to just limit which domains are able to use my id & code?
I assume you are using 'react-scripts' module for building/starting your project. Also as per above description, I can see you are using correct prefix 'REACT_APP_' to declare any variable in your .env file.
Just do a fresh build of app using command:
npm run-scripts build
or Just restart your app using :
npm react-scripts start
I think it should work fine.
And, Yes you can limit which domain should use your AppID and AppCode, using the feature "Secure app credentials against a specific domain" provided at https://developer.here.com/, just below the place where you see your id and code.
Hope this helps.

Firebase Deployed URL not working?

So I have built a simple firebase and javascript app that uses firebase database and hosting.
I have successfully built the app and deployed to Firebase hosting, however when the cmd provides the url that leads to the app it takes me to some completely random firebase landing page.
All deployment is correct and I know all my code is correct, but it keeps sending me to the random landing page: https://firebase.google.com/docs/hosting/
When you first create a project on firebase it asks you for the public directory: What do you want to use as your public directory? public
Usually the default its public, so the firebase generates a random welcome index.html there.
When you setup the firebase init configure the public directory to your files and you should see your app instead of the welcome index file, or simply put all your files inside the public directory you've chosen above.
Here I found a very simple tutorial on how to get started on that: https://www.brooks-patton.com/deploying-a-static-website-to-firebase/
Another way - you can just edit firebase.json file, in the line with "public" change the path where is your code was built.

Conditionally load internal assets in Meteor

Is there a way to prevent automatic loading of all assets in my /client folder?
I am looking at a scenario where my login page uses different css/js files than my registration or view users folders. What is the best way to do this?
The closest I have come to a solution is this but that still doesn't solve my problem.
Or the best approach is to host my files externally then user the external-file-loader with conditional statements?
I have just published modules smart package which more or less does the job for you. You can add it to your project with
mrt add modules
Then, you will need to change the extensions of all files that you want to load asynchronously to .module_js or .module_html (currentlly we do not support css). Now suppose that the structure of your directory is more or less
modules
module1
file1.module_js
file2.module_js
module2
file1.module_js
file2.module_js
client
main.js
server
public
...
Initially only main.js will be present in your application. To load additional code for the client use the asynchronous require call:
require('module1', function () {
console.log('the files contained in module1 folder are now loaded');
});

Meteor - Create a file to be downloaded (without triggering meteor to restart)

I want to create a file and then serve it using Meteor, but I don't want the server to restart when I create/update the file in the public directory.
The user will click on a button to create a config file on the server and I want the user to be able to download that config file.
Is there a way to do this without triggering the server to restart?
I have tried creating a link to the file and creating a hidden file but nothing has worked.
Thanks for your time.
Try meteor run --production. That might solve your problem.
Server restarts because you are running it in Development mode,
When it runs in production, it doesn't restart on content changes.
To run in production, only way I know is, after bundling application,
Have a look here: http://docs.meteor.com/#deploying
If you doesn't want to run in production mode, here is a workaround:
In order to prevent reloading, you have to generate your files in a folder that is located outside of your project's repository.
Then you will have your meteor app to serve the content of that folder.
Here is an example that uses the connect npm repository to make your local folder /meteor/generated_files served under the url hostname.com/downloads/:
var connect = Npm.require('connect');
var fs = Npm.require('fs');
function serveFolder(urlPath, diskPath){
if(!fs.existsSync(diskPath))
return false;
RoutePolicy.declare(urlPath, 'network');
WebApp.connectHandlers.use(urlPath, connect.static(diskPath));
return true;
}
serveFolder('/downloads', '/meteor/generated_files/');
I published the very primitive package I have that does just that.

Silverstripe on souceforge project web

how do I install silverstripe on sourceforge for a project. I know I need a synlink...but I don't know how to?
I have a htdocs folder that is read only (once on the server) that I can access via sftp
it is accsesable via url
I have a persistent folder that is rightable (once on the server) that I can access via sftp
it is not accsesable via url
I have a mysql credentials that are accseped during install but can't be finished because of no right accsess
So you're trying to install SilverStripe on sourceforge? Well, okay.
I guess you need to check that the MySQL user you're using has write access to the database. Also, that you got the database name right in the installation process: if you didn't, the installer will try to create that database, and if you don't have the necessary permission (usually the case on shared hosting setups), you'll get an error complaining about the CREATE DATABASE statement.
So do I understand it right that your problem is that you can upload SilverStripe, but you can not install it, because the installer wants to write the config file?
Well, in this case there is actually a way to get SilverStripe running without using the installer. Just enter the database information into your mysite/_config.php file.
It should look something like this:
<?php
global $project;
$project = 'mysite';
global $databaseConfig;
$databaseConfig = array(
"type" => 'MySQLDatabase',
"server" => 'localhost',
"username" => 'myuser',
"password" => 'mypass',
"database" => 'mydatabasename',
"path" => '',
);
MySQLDatabase::set_connection_charset('utf8');
// This line set's the current theme. More themes can be
// downloaded from http://www.silverstripe.org/themes/
SSViewer::set_theme('blackcandy');
// Set the site locale
i18n::set_locale('en_US');
// enable nested URLs for this site (e.g. page/sub-page/)
SiteTree::enable_nested_urls();
Director::set_environment_type('dev');
// Director::set_environment_type('live');
please note that SilverStripe by default requires write permission on the assets/ folder, not only for uploading files, it also requires it for saving CSS files if the environment type is set to live, because SilverStripe wants to fetch all JS and CSS files, combine them into 1 single JS and 1 CSS file and saves them into the assets/ folder.
If this is not possible, the admin will simply not load, you can also work around this by letting SilverStripe generate those files on another server (your local dev server) and then upload the files.

Resources