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

Implement server-side code hot-swapping on file change #1225

Merged
merged 3 commits into from
Aug 15, 2017

Conversation

damassi
Copy link
Member

@damassi damassi commented Aug 14, 2017

This PR sets up code hot-swapping on the server without a full restart (but still requiring a refresh of the browser window). Crazy-fast development speed!

Overview of process:

  • reloadable is called with a path to an app (client, api, etc) as well as a callback function that is executed whenever files with that appPath change
  • When a source-code change is detected an internal lookup is made to Node, scanning its require cache
  • If found, it is cleared via delete require.cache[id]
  • When the browser is reloaded the callback re-requires the initial app and since the module is no longer in the cache node re-caches it, effectively hot-reloading the code during runtime.

Example:

import { createReloadable, isDevelopment } from 'lib/reloadable'

... 

if (isDevelopment) {
  const reloadAndMount = createReloadable(app)

  // Note that if you need to mount an app at a particular root (`/api`), pass 
  // in `mountPoint` as an option.
  app.use('/api', reloadAndMount(path.resolve(__dirname, 'api'), {
    mountPoint: '/api'
  }))

  // Otherwise, just pass in the path to the express app and everything is taken care of 
  reloadAndMount(path.resolve(__dirname, 'client'))
} else {
  app.use('/api', require('./api')
  app.use(require('./client')
}

req.user = null
next()
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right this comment can probably be removed since it's pretty old and we actually benefit from not sharing req.user because it keeps our API stateless.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, gotta fix this

)
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this! Very clear and thoughtful error handling

// Enable server-side code hot-swapping on change
app.use('/api', reloadAndMount(path.resolve(__dirname, 'api'), {
mountPoint: '/api'
}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@damassi damassi changed the title Server-side Hot Reloading Implement server-side code hot-swapping on file change Aug 15, 2017
@@ -50,3 +61,10 @@ artsyXapp.on('error', (error) => {
console.warn(error)
process.exit(1)
})

const invalidateUserMiddleware = (app) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kanaabe
Copy link
Contributor

kanaabe commented Aug 15, 2017

This is 🔥

@kanaabe kanaabe merged commit 7a5c498 into artsy:master Aug 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants