meteorhacks/meteor-inject-initial

Name: meteor-inject-initial

Owner: meteorhacks

Description: Allow injection of arbitrary data to initial Meteor HTML page

Created: 2014-03-22 07:37:11.0

Updated: 2018-03-09 05:36:43.0

Pushed: 2016-03-24 11:33:50.0

Homepage: null

Size: 32

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

inject-initial

API to modify the initial HTML sent by Meteor to the client. An abstraction of the approach first used by Arunoda Susiripala in fast-render.

Preview release but is already being used in a number of projects.

Released under the MIT license (see the LICENSE file).

How, when, why, etc?

This script is targetted at advanced smart package developers. Sometimes we want to pass data with the original HTML request to make the site available quicker. Note, if you're just wanting to accelerate data that is made available via publish/subscribe functions, use fast-render.

Put api.use('inject-initial', ['client', 'server']); in package.js. The injected data is (in the higher level methods) inserted at the top of the HEAD element, and BEFORE any other scripts are called. Thus, you can access this data when your script is loaded, without needing to wait for anything or use any callbacks (like a return from a Meteor.call() to get initial data).

API (from highest to lowest level)

Common arguments:

Methods:

Meteor.isServer)
ject.obj('myData', myData);

lways available immediately
Meteor.isClient)
r myData = Injected.obj('myData');
ct.rawModHtml('doSomething', function(html) {
return html.replace(/blah/, 'something');

Example of a “per-request” handler:

Meteor.isServer) {
 (!Package.appcache)
bApp.connectHandlers.use(function(req, res, next) {
if(Inject.appUrl(req.url)) {
  Inject.obj('myData', makeDataFor(req), res);
}
next();
;


Meteor.isClient) {
 available immediately
r myData = Injected.obj('myData');

App Cache

In general, you probably don't want to pass any information if appcache is enabled, since you'll never be able to refresh it until you update your app again and force a HCP. However, we'll leave these in your hands to decide… maybe that behvaviour is ok. But consider including appcache as a weak dependency, checking for Package.appcache, and avoiding injecting when present (instead use whatever method you used previously to pass data to the client).

Security

This script is very useful for passing general info on the initial request, and fast-render is very useful for passing authenticated publication info, with the additional security checks in place.

To that end, if you are attempting to use this script to pass priviledged information to the client, be aware of the kinds of issues pointed out by Emily Stark here and take the necessary precautions in the callbacks you pass to inject-initial.

Roadmap

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.