Skip to content
This repository was archived by the owner on Oct 24, 2021. It is now read-only.

Commit d98f0af

Browse files
Merge pull request #738 from brucejo75/brucejo75/hookRuntimeConfig
Document addRuntimeConfigHook
2 parents 6e0eff4 + f61748f commit d98f0af

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

source/packages/webapp.md

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,8 @@ WebApp.connectHandlers.use('/hello', (req, res, next) => {
2525
});
2626
```
2727

28-
`WebApp.connectHandlers.use([path], handler)` has two arguments:
29-
30-
**path** - an optional path field.
31-
This handler will only be called on paths that match
32-
33-
this string. The match has to border on a `/` or a `.`. For example, `/hello`
34-
will match `/hello/world` and `/hello.world`, but not `/hello_world`.
35-
36-
**handler** - this is a function that takes three arguments:
37-
38-
- **req** - a Node.js
39-
[IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
40-
object with some extra properties. This argument can be used to get information
41-
about the incoming request.
42-
- **res** - a Node.js
43-
[ServerResponse](http://nodejs.org/api/http.html#http_class_http_serverresponse)
44-
object. Use this to write data that should be sent in response to the
45-
request, and call `res.end()` when you are done.
46-
- **next** - a function. Calling this function will pass on the handling of
47-
this request to the next relevant handler.
28+
{% apibox "WebApp.connectHandlers" %}
29+
{% apibox "connectHandlersCallback(req, res, next)" %}
4830

4931
### Serving a Static Landing Page
5032

@@ -119,3 +101,62 @@ We're reading the contents of index.html using the [Assets](https://docs.meteor.
119101
We're using the [connect-route](https://www.npmjs.com/package/connect-route) NPM package to simplify WebApp route processing. But you can use any package you want to understand what is being requested.
120102

121103
And finally, if you decide to use this technique you'll want to make sure you understand how conflicting client side routing will affect user experience.
104+
105+
### Dynamic Runtime Configuration
106+
107+
In some cases it is valuable to be able to control the __meteor_runtime_config__ variable that initializes Meteor at runtime.
108+
109+
#### Example
110+
There are occasions when a single Meteor server would like to serve multiple cordova applications that each have a unique `ROOT_URL`. But there are 2 problems:
111+
1. The Meteor server can only be configured to serve a single `ROOT_URL`.
112+
2. The `cordova` applications are build time configured with a specific `ROOT_URL`.
113+
114+
These 2 conditions break `autoupdate` for the cordova applications. `cordova-plugin-meteor-webapp` will fail the update if the `ROOT_URL` from the server does not match the build time configured `ROOT_URL` of the cordova application.
115+
116+
To remedy this problem `webapp` has a hook for dynamically configuring `__meteor_runtime_config__` on the server.
117+
118+
#### Dynamic Runtime Configuration Hook
119+
```js
120+
WebApp.addRuntimeConfigHook(({arch, request, encodedCurrentConfig, updated}) => {
121+
// check the request to see if this is a request that requires
122+
// modifying the runtime configuration
123+
if(request.headers.domain === 'calling.domain') {
124+
// make changes to the config for this domain
125+
// decode the current runtime config string into an object
126+
const config = WebApp.decodeRuntimeConfig(current);
127+
// make your changes
128+
config.newVar = 'some value';
129+
config.oldVar = 'new value';
130+
// encode the modified object to the runtime config string
131+
// and return it
132+
return WebApp.encodeRuntimeConfig(config);
133+
}
134+
// Not modifying other domains so return undefined
135+
return undefined;
136+
})
137+
```
138+
{% apibox "WebApp.addRuntimeConfigHook" %}
139+
{% apibox "addRuntimeConfigHookCallback(options)" %}
140+
141+
Additionally, 2 helper functions are available to decode the runtime config string and encode the runtime config object.
142+
143+
{% apibox "WebApp.decodeRuntimeConfig" %}
144+
{% apibox "WebApp.encodeRuntimeConfig" %}
145+
146+
### Updated Runtime Configuration Hook
147+
```js
148+
const autoupdateCache;
149+
// Get a notification when the runtime configuration is updated
150+
// for each arch
151+
WebApp.addUpdatedNotifyHook(({arch, manifest, runtimeConfig}) => {
152+
// Example, see if runtimeConfig.autoupdate has changed and if so
153+
// do something
154+
if(!_.isEqual(autoupdateCache, runtimeConfig.autoupdate)) {
155+
autoupdateCache = runtimeConfig.autoupdate;
156+
// do something...
157+
}
158+
})
159+
```
160+
161+
{% apibox "WebApp.addUpdatedNotifyHook" %}
162+
{% apibox "addUpdatedNotifyHookCallback(options)" %}

0 commit comments

Comments
 (0)