You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 24, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: source/packages/webapp.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,3 +119,60 @@ We're reading the contents of index.html using the [Assets](https://docs.meteor.
119
119
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.
120
120
121
121
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.
// 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
+
constconfig=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
+
returnWebApp.encodeRuntimeConfig(config);
151
+
}
152
+
// Not modifying other domains so return undefined
153
+
returnundefined;
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`.
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