When using Inertia.js with React, navigating back with the browser's back button will, by default, restore the previous page from the browser's cache (history state). This means your dashboard page will not automatically fetch fresh data from the server, so any edits made on the edit page won't be reflected until you manually refresh.
Solution: Use preserveState: false or Inertia.reload() on history navigation
There are a few ways to solve this:
1. Use preserveState: false on your edit form redirect
When you redirect back to the dashboard after editing, you can force Inertia to reload the data by setting preserveState: false in your controller:
// In your controller after updating the project
return redirect()->route('dashboard')->with('success', 'Project updated!');
But this alone won't help with the browser's back button, because that navigation is handled by the browser, not your code.
2. Use useEffect and Inertia's on('navigate') event in React
You can listen for Inertia's navigation events and force a reload when the user navigates back to the dashboard:
import { useEffect } from 'react';
import { router } from '@inertiajs/react';
export default function Dashboard({ projects }) {
useEffect(() => {
const reloadOnBack = (event) => {
if (event.detail.visit.type === 'back') {
router.reload({ only: ['projects'] });
}
};
window.addEventListener('inertia:navigate', reloadOnBack);
return () => {
window.removeEventListener('inertia:navigate', reloadOnBack);
};
}, []);
// ... rest of your component
}
This will reload the projects prop from the server whenever the user navigates back to this page.
3. Use remember and preserveState options (less recommended for this use case)
Inertia provides options for remembering state and preserving it, but for your use case (always wanting fresh data), the above approach is better.
Summary
- The browser's back button restores the cached page, so you need to manually reload data.
- Listen for Inertia's navigation events and call
router.reload()when the user navigates back. - This ensures your dashboard always shows up-to-date data after editing.
Let me know if you need a more detailed code example!