RESOLVED: How to completely disable DDP?


#1

Hi all!

It could sound strange, but I need to completely disable or preferably remove DDP from the client side. I need that for client-only apps (better to call them web-sites). Is it possible?

Currently I use the trick with unresolvable DDP_DEFAULT_CONNECTION_URL, however DDP still makes the first try to connect to it and occupies some space in the production package.

My set of packages for the build time:

gadicc:ecmascript-hot  2.0.0-beta.3  Replacement ecmascript package providing react hotloading
juliancwirko:postcss   1.1.1  Minifier for Meteor with PostCSS processing - use Autoprefixer and others with ease
kadira:dochead         1.5.0  Isomorphic way to manipulate document.head for Meteor apps
kadira:flow-router     2.12.1  Carefully Designed Client Side Router for Meteor
meteor                 1.1.14  Core Meteor environment
okgrow:analytics       1.0.5  Complete Google Analytics, Mixpanel, KISSmetrics (and more) integration for Meteor
standard-minifier-js   1.0.6  Standard javascript minifiers used with Meteor apps by default.

#2

Just curious, why are you doing this? Are you trying to make it easier and more cost-effective to scale?

Also, this belongs in the help category.


#3

Just edited my question… I need that for client-only apps, which do not require server side nor any other connection to outside at all. In this case I am able to deploy apps (sites) to any static hosting.


#4

I see. Maybe somebody else knows a better way to do this, but IMO you’re much better off using S3 with a static React webpage.


#5

S3 is an example of a bunch of static hostings… Did you mean not to use Meteor at all?


#6

That’s exactly what I mean and exactly what S3 implies.


#7

Meteor is too good not to use it :slight_smile: I use Meteor in the majority of my projects and prefer to have just a few boilerplates for all of them. If I switch to, let’s say, Webpack, I’ll have to change and then maintain more pieces. And not all my client-only sites are made with React. Some of them are based on Blaze/Session/some other Meteor staff.


#8

The problem is that you rely on packages which in their turn rely on the ddp package.
E.g. the okgrow:analtyics and the kadira:flow-router package rely on the mongo package which relies on the ddp package


#9

gadicc:ecmascript-hot does as well. However, there are no subscriptions or anything that actually use ddp. So, the optimizer could remove it while building… So, the question still is, how to force DDP to be turned off from the very app start.


#10

I’ve done this with https://github.com/frozeman/meteor-build-client


#11

Thank you for the reply. Have you looks in the browser console after using this tool? I hardly believe it stays clean. All this tool does it builds the project, gets the content of web.browser and replaces the index.html with taken from the template. After the main Meteor .js file execution it runs Meteor.disconnect() (I do it even earlier, but it does not help). Here is the code from https://github.com/frozeman/meteor-build-client/blob/master/meteor.js, which does that:

...
if(program.url !== 'default')
    settings.ROOT_URL = program.url || '';

if(settingsJson.public)
    settings.PUBLIC_SETTINGS = settingsJson.public;

scripts = scripts.replace('__meteor_runtime_config__', '<script type="text/javascript">__meteor_runtime_config__ = JSON.parse(decodeURIComponent("'+encodeURIComponent(JSON.stringify(settings))+'"));</script>');

// add Meteor.disconnect() when no server is given
if(!program.url)
    scripts += '        <script type="text/javascript">Meteor.disconnect();</script>';
...

My own script (it is in BASH, so it is several times smaller) works a bit differently:

1. copy the full project to tmp;
2. remove webapp and hot-code-push modules;
3. build the project;
4. add webapp module;
5. run the project (with production settings);
6. grab index file (or a set of files if the app is router-enabled) by PhantomJS;
7. replace paths to .js and .css files and the value of __meteor_runtime_config__ within grabbed files. DDP_DEFAULT_CONNECTION_URL is set to https://none here;
8. put the resulting html files together with .css, .js and content of public directory into the output directory.

As the result I have a set of pages, which work even when javascript is disabled on the client (well, with understandable limitations). This set of files is ready to be deployed to S3 or GitHub Pages or GitLab Pages (as I do) or anything else.
However, I have an error in the browser console, which tells that none server-name could not be resolved. It does not look nice ;). To prevent further attempts to connect I have the following lines right in the beginning on the client index:


/* Disconnect from the server as the first step when running in production env */
/* global meteorEnv */
if( meteorEnv.NODE_ENV === "production" ) Meteor.disconnect()

Other files are imported as modules, so I am sure that these lines are executed before my other code. But unfortunately, meteor packages’ code is executed even further. I considered putting these lines in a separate package, however I doubt it will work. I suppose I cannot call disconnect() before ddp module is loaded as it will be unavailable yet. So, I have to dig further :slight_smile:

If someone is interested in the script, I will “prettify” it and publish somewhere.


Managing a static site with Meteor
#12

I forgot to mention, that when grabbing pages with PhantomJS, I mark all the non-script tags in the body root with the no-script class. In the client index I have the following:

Meteor.startup( () => {
    /*
        Remove static blocks after the page is generated.
        It is supposed that these blocks were marked by PhantomJS with 'no-script' class.
    */
    let noScriptDivs = document.body.getElementsByClassName( "no-script" )
    for( let i = noScriptDivs.length - 1; i >= 0; --i ) noScriptDivs[i].remove()
} )

This approach prevents blocks duplication when JavaScript is enabled on the client.


#13

Meteor Build Client is used in production here: https://recipes.sanpellegrino.com/

The browser console is empty.


#14

@GeoffreyBooth, thank you for the link! Hover, this website is made with Meteor 1.1, while I use 1.3.2.x/1.3.3-beta. Possibly DDP changed a bit since 1.1. As for meteor-build-client, it does not set DDP_DEFAULT_CONNECTION_URL value att all, so I tried to do the same - does not help.

The most strange thing is that I get the error message in the console AFTER executing Meteor.disconnect() (I have added debug message for that). Here is the website built with my script https://4k-studio.ru/.

DDP stopped.
GET https://4k-studio.ru/sockjs/info?cb=ox0v08l4dx 404 ()

#15

Yes, I encountered that issue as well. Our solution is in this discussion:


#16

WOW! You are genius :slight_smile: Let me just add this to my script.


#17

Finally, here is the working script - this version eliminates all the console errors and does not require any manual changes in the project code. Currently all the settings (very few) are within the file, so it still has to be improved with command line support.

Later I will publish all my GitLab CI / GitLab Pages automation examples together with this script into a separate repository.

Edit: Forgot to give a link to the PhantomJs part of the script, it is required if someone tries to use my build process :slight_smile: Here it is.