Speeding Up Laravel Tests with tmpfs (MySQL in RAM)
If your Laravel test suite is painfully slow and does a lot of database work (multi-tenancy, migrations, seeding), the bottleneck is almost…
If you're using flash messages in your Laravel + Vue/Inertia application (or any SPA really), you might have encountered this annoying issue: A user performs an action that shows a toast/flash message, navigates away, then uses the browser's back button - and suddenly the old message reappears!
This happens because modern browsers cache page states for performance reasons. When a user clicks the back button, the browser restores the entire page state from its cache, including any flash messages that were present at that time.
Let's say you have a typical flow:
This occurs because:
The key is to detect when a page is shown due to back/forward navigation. Here's a robust approach that works across different implementations:
onMounted(() => {
// Listen for popstate events (back/forward navigation)
window.addEventListener('popstate', () => {
// Store information about back navigation
sessionStorage.setItem('isBackNavigation', 'true')
})
})
// In your flash message handling logic
const showFlashMessage = (message) => {
// Check if we're coming from back navigation
if (sessionStorage.getItem('isBackNavigation') === 'true') {
// Skip showing the message
sessionStorage.removeItem('isBackNavigation')
return
}
// Show your flash message normally
displayMessage(message)
}
popstate event fires when the user navigates through browser history (back/forward buttons)sessionStorage to persist this information across page loadslocalStorage, sessionStorage is cleared when the tab is closed, which is perfect for this use casepopstate listener in a component that loads early in your application lifecycleThis solution can be adapted to various implementations:
// Using a toast library like vue-toastification
onMounted(() => {
window.addEventListener('popstate', () => {
toast.dismiss() // Hide any visible toasts
sessionStorage.setItem('isBackNavigation', 'true')
})
})
// If you render flash messages in your template
const shouldShowFlash = computed(() => {
return !!flashMessage.value &&
sessionStorage.getItem('isBackNavigation') !== 'true'
})
// In your Laravel controller
public function store()
{
// Your logic...
session()->flash('message', 'Success!');
return redirect()->back();
}
// In your JavaScript
if (page.props.flash.message && !isBackNavigation()) {
showMessage(page.props.flash.message)
}
The exact implementation might vary based on your setup, but the core concept of detecting back navigation and preventing message display remains the same.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com