Skip to content

Commit bfb9b3f

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] 2 pages modified
1 parent f7bfc23 commit bfb9b3f

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

core/running-side-effects.md

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ It can be a good idea to not allow your side effects to initialize when they are
9191
```typescript
9292
import * as firebase from 'firebase'
9393

94-
// We use IIFE to hide the private "app" variable
9594
export const api = (() => {
9695
let app
9796

@@ -133,35 +132,6 @@ export const onInitialize = async ({ effects }) => {
133132
Typically you explicitly communicate with effects from actions, by calling methods. But sometimes you need effects to know about the state of the application, or maybe some internal state in the effect should be exposed on your application state. Again we can take advantage of an **initialize** method on the effect:
134133

135134
{% tabs %}
136-
{% tab title="overmind/effects.js" %}
137-
```javascript
138-
// We use an IIFE to isolate some variables
139-
export const socket = (() => {
140-
_options
141-
_ws
142-
return {
143-
initialize(options) {
144-
_options = options
145-
_ws = new WebSocket('ws://...')
146-
_ws.onclose = () => options.onStatusChange('close')
147-
_ws.onopen = () => options.onStatusChange('open')
148-
_ws.addEventListener(
149-
'message',
150-
(event) => options.onMessage(event.data)
151-
)
152-
},
153-
send(type, data) {
154-
_ws.postMessage({
155-
type,
156-
data,
157-
token: _options.getToken()
158-
})
159-
}
160-
}
161-
})()
162-
```
163-
{% endtab %}
164-
165135
{% tab title="overmind/onInitialize.js" %}
166136
```typescript
167137
export const onInitialize = async ({ state, effects, actions }) => {

guides-1/server-side-rendering.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,55 @@ export const onInitialize: OnInitialize = ({ state }) => {
103103
If you are using state first routing, make sure you prevent the router from firing off the initial route, as this is not needed.
104104
{% endhint %}
105105

106+
## OnInitialize
107+
108+
The `onInitialized` action does not run on the server. The reason is that it is considered a side effect you might not want to run, so we do not force it. If you do want to run an action as Overmind fires up both on the client and the server you can rather create a custom action for it.
109+
110+
{% tabs %}
111+
{% tab title="overmind/actions.js" %}
112+
```javascript
113+
export const initialize = () => {
114+
// Whatever...
115+
}
116+
```
117+
{% endtab %}
118+
119+
{% tab title="client/index.js" %}
120+
```typescript
121+
import { createOvermind } from 'overmind'
122+
import { config } from './overmind'
123+
124+
const overmind = createOvermind(config)
125+
overmind.actions.initialize()
126+
```
127+
{% endtab %}
128+
129+
{% tab title="server/index.js" %}
130+
```javascript
131+
import { createOvermindSSR } from 'overmind'
132+
import { config } from '../client/overmind'
133+
134+
export default async (req, res) => {
135+
const overmind = createOvermindSSR(config)
136+
await overmind.actions.initialize()
137+
138+
const html = renderToString(
139+
// Whatever implementation your view layer provides
140+
)
141+
142+
res.send(`
143+
<html>
144+
<body>
145+
<div id="app">${html}</div>
146+
<script>
147+
window.__OVERMIND_MUTATIONS = ${JSON.stringify(overmind.hydrate())}
148+
</script>
149+
<script src="/scripts/app.js"></script>
150+
</body>
151+
</html>
152+
`)
153+
}
154+
```
155+
{% endtab %}
156+
{% endtabs %}
157+

0 commit comments

Comments
 (0)