Skip to content

Commit

Permalink
Merge pull request #2200 from inertiajs/reload-after-location-fix
Browse files Browse the repository at this point in the history
Fix: Reload on mount
  • Loading branch information
joetannenbaum authored Jan 21, 2025
2 parents 66985bf + 1215c80 commit b70f216
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class History {
public saveScrollPositions(scrollRegions: ScrollRegion[]): void {
queue.add(() => {
return Promise.resolve().then(() => {
if (!window.history.state?.page) {
return
}

this.doReplaceState(
{
page: window.history.state.page,
Expand Down
12 changes: 12 additions & 0 deletions packages/react/test-app/Pages/Visits/ReloadOnMount.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { router } from '@inertiajs/react'
import { useEffect } from 'react'

export default (props) => {
useEffect(() => {
setTimeout(() => {
router.reload({ only: ['name'] })
})
})

return <div>Name is {props.name}</div>
}
14 changes: 14 additions & 0 deletions packages/svelte/test-app/Pages/Visits/ReloadOnMount.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
import { router } from '@inertiajs/svelte'
import { onMount } from 'svelte'
export let name = undefined;
onMount(() => {
router.reload({only: ['name']})
})
</script>


<div>Name is {name}</div>
16 changes: 16 additions & 0 deletions packages/vue3/test-app/Pages/Visits/ReloadOnMount.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script setup>
import { router } from '@inertiajs/vue3'
import { onMounted } from 'vue'
defineProps({
name: String,
})
onMounted(() => {
router.reload({ only: ['name'] })
})
</script>

<template>
<div>Name is {{ name }}</div>
</template>
14 changes: 14 additions & 0 deletions tests/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ app.delete('/dump/delete', upload.any(), (req, res) =>
}),
)

app.get('/visits/reload-on-mount', upload.any(), (req, res) => {
if (req.headers['x-inertia-partial-data']) {
return inertia.render(req, res, {
component: 'Visits/ReloadOnMount',
props: { name: 'mounted!' },
})
}

inertia.render(req, res, {
component: 'Visits/ReloadOnMount',
props: { name: 'not mounted!' },
})
})

app.get('/persistent-layouts/shorthand/simple/page-a', (req, res) =>
inertia.render(req, res, { props: { foo: 'bar', baz: 'example' } }),
)
Expand Down
5 changes: 5 additions & 0 deletions tests/manual-visits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,11 @@ test.describe('Partial Reloads', () => {
})
})

test('can reload on mount', async ({ page }) => {
await page.goto('/visits/reload-on-mount')
await expect(page.getByText('Name is mounted!')).toBeVisible()
})

test.describe('Error bags', () => {
test.beforeEach(async ({ page }) => {
pageLoads.watch(page)
Expand Down

0 comments on commit b70f216

Please sign in to comment.