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

Commit fff4bc8

Browse files
committed
Doc addRuntimeConfigHook
1 parent ed8765e commit fff4bc8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

source/packages/webapp.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,60 @@ We're reading the contents of index.html using the [Assets](https://docs.meteor.
119119
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.
120120

121121
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.
122+
123+
### Dynamic Runtime Configuration
124+
125+
In some cases it is valuable to be able to control the __meteor_runtime_config__ variable that initializes Meteor at runtime.
126+
127+
#### Example
128+
There are occasions when a single Meteor server would like to serve multiple cordova applications that have each have a unique `ROOT_URL`. But there are 2 problems:
129+
1. The Meteor server can only be configured to serve a single `ROOT_URL`.
130+
2. The `cordova` applications are build time configured with a specific `ROOT_URL`.
131+
132+
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.
133+
134+
To remedy this problem `webapp` has a hook for dynamically configuring `__meteor_runtime_config__` on the server.
135+
136+
#### Dynamic Runtime Configuration Hook
137+
```js
138+
WebApp.addRuntimeConfigHook(({arch, request, encodedCurrentConfig, updated}) => {
139+
// check the request to see if this is a request that requires
140+
// modifying the runtime configuration
141+
if(req.headers.domain === 'calling.domain') {
142+
// make changes to the config for this domain
143+
// decode the current runtime config string into an object
144+
const config = WebApp.decodeRuntimeConfig(current);
145+
// make your changes
146+
config.newVar = 'some value';
147+
config.oldVar = 'new value';
148+
// encode the modified object to the runtime config string
149+
// and return it
150+
return WebApp.encodeRuntimeConfig(config);
151+
}
152+
// Not modifying other domains so return undefined
153+
return undefined;
154+
})
155+
```
156+
157+
`WebApp.addRuntimeConfigHook(handler)` has one argument:
158+
159+
**handler** - The `handler` is called on each request for the root page which has `__meteor_runtime_config__` defined in it. The handler takes a single options argument with the following properties:
160+
161+
- **arch** - _String_. the architecture being responded to. This can be one of `web.browser`, `web.browser.legacy` or `web.cordova`.
162+
- **request** - a Node.js
163+
[IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
164+
object with some extra properties. This argument can be used to get information
165+
about the incoming request.
166+
- **encodedCurrentConfig** - _String_. the current configuration object encoded as a string for inclusion in the root html.
167+
- **updated** - _Boolean_. `true` if the config for this architecture been updated since last called, otherwise `false`. This flag can be leveraged to cache the decoding/encoding for each architecture.
168+
169+
If the handler returns a _falsy_ value the hook will not modify the runtime configuration.
170+
171+
If the handler returns a _String_ the hook will substitute the string for the encoded configuration string. **Warning:** the hook does not check the return value at all it is the responsibility of the caller to get the formatting correct using the helper functions.
172+
173+
Additionally, 2 helper functions are available to decode the runtime config string and encode the runtime config object.
174+
175+
`WebApp.decodeRuntimeConfig(encoded_config_string)`: returns a config object from an encoded config string.
176+
`WebApp.encodeRuntimeConfig(config_object)`: returns an encoded string from a config object that is ready for the root page.
177+
178+
The expected usage is to decode the runtime config string, operate on the object and then return the encoded runtime configuration.

0 commit comments

Comments
 (0)