Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby): add DANGEROUSLY_DISABLE_OOM #13066

Merged
merged 12 commits into from
Apr 4, 2019
22 changes: 22 additions & 0 deletions examples/simple-auth/src/pages/app/myapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react"
import { Router } from "@reach/router"
import Layout from "../components/Layout"
import Details from "../components/Details"
import Home from "../components/Home"
import Login from "../components/Login"
import PrivateRoute from "../components/PrivateRoute"
import Status from "../components/Status"

const App = () => (
<Layout>
<Status />
<Router>
<PrivateRoute path="/app/profile" component={Home} />
<PrivateRoute path="/app/details" component={Details} />
<PrivateRoute path="/app/mypage" component={Details} />
<Login path="/app/login" />
</Router>
</Layout>
)

export default App
22 changes: 22 additions & 0 deletions examples/simple-auth/src/pages/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react"
import { Router } from "@reach/router"
import Layout from "../components/Layout"
import Details from "../components/Details"
import Home from "../components/Home"
import Login from "../components/Login"
import PrivateRoute from "../components/PrivateRoute"
import Status from "../components/Status"

const App = () => (
<Layout>
<Status />
<Router>
<PrivateRoute path="/app/profile" component={Home} />
<PrivateRoute path="/app/details" component={Details} />
<PrivateRoute path="/app/mypage" component={Details} />
<Login path="/app/login" />
</Router>
</Layout>
)

export default App
46 changes: 46 additions & 0 deletions packages/gatsby/src/redux/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const path = require(`path`)
const fs = require(`fs-extra`)
const { saveState, store } = require(`../index`)
const {
actions: { createPage },
} = require(`../actions`)

jest.mock(`fs-extra`)

describe(`redux db`, () => {
beforeEach(() => {
store.dispatch(
createPage(
{
path: `/my-sweet-new-page/`,
component: path.resolve(`./src/templates/my-sweet-new-page.js`),
// The context is passed as props to the component as well
// as into the component's GraphQL query.
context: {
id: `123456`,
},
},
{ name: `default-site-plugin` }
)
)

fs.writeFile.mockClear()
})
it(`should write cache to disk`, async () => {
await saveState()

expect(fs.writeFile).toBeCalledWith(
expect.stringContaining(`.cache/redux-state.json`),
expect.stringContaining(`my-sweet-new-page.js`)
)
})

it(`shoudn't write cache to disk when DANGEROUSLY_DISABLE_OOM is set`, async () => {
process.env.DANGEROUSLY_DISABLE_OOM = `1`

await saveState()

expect(fs.writeFile).not.toBeCalled()
delete process.env.DANGEROUSLY_DISABLE_OOM
})
})
11 changes: 11 additions & 0 deletions packages/gatsby/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const _ = require(`lodash`)
const fs = require(`fs-extra`)
const mitt = require(`mitt`)
const stringify = require(`json-stringify-safe`)
const telemetry = require(`gatsby-telemetry`)

// Create event emitter for actions
const emitter = mitt()
Expand Down Expand Up @@ -73,6 +74,16 @@ const store = Redux.createStore(

// Persist state.
function saveState() {
if (process.env.DANGEROUSLY_DISABLE_OOM) {
// meausure how many people are using DANGEROUSLY_DISABLE_OOM in build statements
telemetry.decorateEvent(`BUILD_END`, {
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
env: {
DANGEROUSLY_DISABLE_OOM: true,
},
})
return Promise.resolve()
}

const state = store.getState()
const pickedState = _.pick(state, [
`nodes`,
Expand Down