Skip to content

Commit a7bf0a2

Browse files
committed
Add global .onRenderError callback
Global callback used to catch errors like `No such layout template` and `No such template`. It's great workaround for dynamically loaded routes and templates, and might be triggered upon broken Internet connection, or when template not loaded for other reason.
1 parent 3fbd984 commit a7bf0a2

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

client/renderer.js

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class BlazeRenderer {
157157
this.old.materialized = true;
158158
this.isRendering = false;
159159
const error = new Meteor.Error(404, `No such layout template: ${layout}`);
160+
this.router.onRenderError && _helpers.isFunction(this.router.onRenderError) && this.router.onRenderError.call(this, error);
160161
callback(error);
161162
throw error;
162163
}
@@ -172,6 +173,7 @@ class BlazeRenderer {
172173
this.old.materialized = true;
173174
this.isRendering = false;
174175
const error = new Meteor.Error(404, `No such template: ${template}`);
176+
this.router.onRenderError && _helpers.isFunction(this.router.onRenderError) && this.router.onRenderError.call(this, error);
175177
current.callback(error);
176178
throw error;
177179
}

docs/api/render.md

+50-19
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,65 @@
22

33
`this.render()` method is available only [inside hooks](https://github.com/VeliovGroup/flow-router/tree/master/docs#hooks-in-execution-order).
44

5-
- __Note__: `this.render()` method is available only if application has `templating` and `blaze`, or `blaze-html-templates` packages installed.
5+
- __Note__: `this.render()` method is available only if application has `templating` and `blaze`, or `blaze-html-templates` packages installed.
66

77
#### With Layout
8+
89
`this.render(layout, template [, data, callback])`
9-
- `layout` {*String*|*Blaze.Template*} - *Blaze.Template* instance or a name of layout template (*which has* `yield`)
10-
- `template` {*String*|*Blaze.Template*} - *Blaze.Template* instance or a name of template (*which will be rendered into yield*)
11-
- `data` {*Object*} - [Optional] Object of data context to use in template. Will be passed to both `layout` and `template`
12-
- `callback` {*Function*} - [Optional] Callback triggered after template is rendered and placed into DOM. This callback has no context
10+
11+
- `layout` {*String*|*Blaze.Template*} - *Blaze.Template* instance or a name of layout template (*which has* `yield`)
12+
- `template` {*String*|*Blaze.Template*} - *Blaze.Template* instance or a name of template (*which will be rendered into yield*)
13+
- `data` {*Object*} - [Optional] Object of data context to use in template. Will be passed to both `layout` and `template`
14+
- `callback` {*Function*} - [Optional] Callback triggered after template is rendered and placed into DOM. This callback has no context
1315

1416
#### Without Layout
17+
1518
`this.render(template [, data, callback])`
16-
- `template` {*String*|*Blaze.Template*} - *Blaze.Template* instance or a name of template (*which will be rendered into* `body` *element, or element defined in* `FlowRouter.Renderer.rootElement`)
17-
- `data` {*Object*} - [Optional] Object of data context to use in template
18-
- `callback` {*Function*} - [Optional] Callback triggered after template is rendered and placed into DOM. This callback has no context
19+
20+
- `template` {*String*|*Blaze.Template*} - *Blaze.Template* instance or a name of template (*which will be rendered into* `body` *element, or element defined in* `FlowRouter.Renderer.rootElement`)
21+
- `data` {*Object*} - [Optional] Object of data context to use in template
22+
- `callback` {*Function*} - [Optional] Callback triggered after template is rendered and placed into DOM. This callback has no context
23+
24+
#### Global catch-all rendering exception:
25+
26+
`FlowRouter.onRenderError = function (error) { /* ... */ };` this callback called with single `error` argument:
27+
28+
- `error` {*Meteor.Error*} — Reason.
29+
30+
Use `FlowRouter.onRenderError` to set global callback to catch errors like `No such layout template` and `No such template`. It's great workaround for dynamically loaded routes and templates, and might be triggered upon broken Internet connection, or when template not loaded for other reason. Here's recommended usage:
31+
32+
```html
33+
<template name="templatingError">
34+
<h1>Oops, something went wrong</h1>
35+
<p>Network or other error occurred, please try to <a href="#" onclick="window.location.href=window.location.href">reload this page</a> or go back to <a href="/">home page</a>.</p>
36+
</template>
37+
```
38+
39+
```js
40+
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
41+
42+
FlowRouter.onRenderError = function (error) {
43+
console.error('[onRenderError]', error);
44+
this.render('templatingError');
45+
};
46+
```
1947

2048
#### Features:
21-
- Made with animation performance in mind, all DOM interactions are wrapped into `requestAnimationFrame`
22-
- In-memory rendering (*a.k.a. off-screen rendering, virtual DOM*), disabled by default, can be activated with `FlowRouter.Renderer.inMemoryRendering = true;`
49+
50+
- Made with animation performance in mind, all DOM interactions are wrapped into `requestAnimationFrame`
51+
- In-memory rendering (*a.k.a. off-screen rendering, virtual DOM*), disabled by default, can be activated with `FlowRouter.Renderer.inMemoryRendering = true;`
2352

2453
#### Settings (*Experimental!*):
25-
- Settings below is experimental, targeted to reduce on-screen DOM layout reflow, speed up rendering on slower devices and Phones in first place, by moving DOM computation to off-screen (*a.k.a. In-Memory DOM, Virtual DOM*)
26-
- `FlowRouter.Renderer.rootElement` {*Function*} - Function which returns root DOM element where layout will be rendered, default: `document.body`
27-
- `FlowRouter.Renderer.inMemoryRendering` {*Boolean*} - Enable/Disable in-memory rendering, default: `false`
28-
- `FlowRouter.Renderer.getMemoryElement` {*Function*} - Function which returns default in-memory element, default: `document.createElement('div')`. Use `document.createDocumentFragment()` to avoid extra parent elements
29-
* The default `document.createElement('div')` will cause extra wrapping `div` element
30-
* `document.createDocumentFragment()` won't cause extra wrapping `div` element but may lead to exceptions in Blaze engine, depends from your app implementation
54+
55+
- Settings below is experimental, targeted to reduce on-screen DOM layout re-flow, speed up rendering on slower devices and Phones in first place, by moving DOM computation to off-screen (*a.k.a. In-Memory DOM, Virtual DOM*)
56+
- `FlowRouter.Renderer.rootElement` {*Function*} - Function which returns root DOM element where layout will be rendered, default: `document.body`
57+
- `FlowRouter.Renderer.inMemoryRendering` {*Boolean*} - Enable/Disable in-memory rendering, default: `false`
58+
- `FlowRouter.Renderer.getMemoryElement` {*Function*} - Function which returns default in-memory element, default: `document.createElement('div')`. Use `document.createDocumentFragment()` to avoid extra parent elements
59+
- The default `document.createElement('div')` will cause extra wrapping `div` element
60+
- `document.createDocumentFragment()` won't cause extra wrapping `div` element but may lead to exceptions in Blaze engine, depends from your app implementation
3161

3262
#### Further reading
33-
- [Templating](https://github.com/VeliovGroup/flow-router/blob/master/docs/templating.md)
34-
- [Templating with "Regions"](https://github.com/VeliovGroup/flow-router/blob/master/docs/templating-with-regions.md)
35-
- [Templating with Data](https://github.com/VeliovGroup/flow-router/blob/master/docs/templating-with-data.md)
63+
64+
- [Templating](https://github.com/VeliovGroup/flow-router/blob/master/docs/templating.md)
65+
- [Templating with "Regions"](https://github.com/VeliovGroup/flow-router/blob/master/docs/templating-with-regions.md)
66+
- [Templating with Data](https://github.com/VeliovGroup/flow-router/blob/master/docs/templating-with-data.md)

0 commit comments

Comments
 (0)