@rill/flash
v2.1.2
Published
Flash messages between requests for Rill.
Downloads
111
Maintainers
Readme
Flash messages between requests for your Rill application.
A flashed message will persist through multiple requests until it is read. After a flashed message has been read it will be deleted.
Installation
npm install @rill/flash
Example
const app = require("rill")()
// A session is required to use flash messages.
app.use(require("@rill/session")())
// Setup the flash middleware.
app.use(require("@rill/flash")())
// Use it!
app.use(({ req, res, flash })=> {
if (req.method === "POST") {
// Here we trigger an error in a post request
// and wait to retrieve the flashed message (usually rendering a view).
flash("error", "This is a flash error message.")
} else {
// After we read the 'error' flash it is marked to be removed after the request is finished.
res.body = flash("error") || "No flash message"
}
})
API
ctx.flash(key, value) - Sets a value to be flashed.
ctx.flash(key) - Retrieves a flashed value. (Marks a key as expired and removes it after the request)
Server Side rendering
To help with browser bootstrapping (when using server side rendering) @rill/flash will return the most recent expired flash message on the first request when running in the browser.
This means that if the server renders a page with one flash message, then the browser tries to display that same message when it bootstraps on page load, both the server and the browser will have the same message.
Contributions
- Use
npm test
to run tests.
Please feel free to create a PR!