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

feat: Add preserveUrl option. #1869

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ export class Router {
})
}

protected locationVisit(url: URL, preserveScroll: LocationVisit['preserveScroll']): boolean | void {
protected locationVisit(url: URL, preserveScroll: LocationVisit['preserveScroll'], preserveUrl: LocationVisit['preserveUrl']): boolean | void {
try {
const locationVisit: LocationVisit = { preserveScroll }
const locationVisit: LocationVisit = { preserveScroll, preserveUrl }
window.sessionStorage.setItem('inertiaLocationVisit', JSON.stringify(locationVisit))
window.location.href = url.href
if (urlWithoutHash(window.location).href === urlWithoutHash(url).href) {
Expand All @@ -190,7 +190,11 @@ export class Router {
page.url += window.location.hash
page.rememberedState = window.history.state?.rememberedState ?? {}
page.scrollRegions = window.history.state?.scrollRegions ?? []
this.setPage(page, { preserveScroll: locationVisit.preserveScroll, preserveState: true }).then(() => {
this.setPage(page, {
preserveScroll: locationVisit.preserveScroll,
preserveUrl: locationVisit.preserveUrl,
preserveState: true,
}).then(() => {
if (locationVisit.preserveScroll) {
this.restoreScrollPositions()
}
Expand Down Expand Up @@ -260,6 +264,7 @@ export class Router {
replace = false,
preserveScroll = false,
preserveState = false,
preserveUrl = false,
only = [],
except = [],
headers = {},
Expand Down Expand Up @@ -295,6 +300,7 @@ export class Router {
replace,
preserveScroll,
preserveState,
preserveUrl,
only,
except,
headers,
Expand Down Expand Up @@ -394,6 +400,7 @@ export class Router {
}
preserveScroll = this.resolvePreserveOption(preserveScroll, pageResponse) as boolean
preserveState = this.resolvePreserveOption(preserveState, pageResponse)
preserveUrl = this.resolvePreserveOption(preserveUrl, pageResponse) as boolean
if (preserveState && window.history.state?.rememberedState && pageResponse.component === this.page.component) {
pageResponse.rememberedState = window.history.state.rememberedState
}
Expand Down Expand Up @@ -424,7 +431,7 @@ export class Router {
if (requestUrl.hash && !locationUrl.hash && urlWithoutHash(requestUrl).href === locationUrl.href) {
locationUrl.hash = requestUrl.hash
}
this.locationVisit(locationUrl, preserveScroll === true)
this.locationVisit(locationUrl, preserveScroll === true, preserveUrl === true)
} else if (error.response) {
if (fireInvalidEvent(error.response)) {
modal.show(error.response.data)
Expand Down Expand Up @@ -458,19 +465,27 @@ export class Router {
replace = false,
preserveScroll = false,
preserveState = false,
preserveUrl = false,
}: {
visitId?: VisitId
replace?: boolean
preserveScroll?: PreserveStateOption
preserveState?: PreserveStateOption
preserveUrl?: PreserveStateOption
} = {},
): Promise<void> {
return Promise.resolve(this.resolveComponent(page.component)).then((component) => {
if (visitId === this.visitId) {
page.scrollRegions = page.scrollRegions || []
page.rememberedState = page.rememberedState || {}
replace = replace || hrefToUrl(page.url).href === window.location.href

if (preserveUrl) {
page.url = window.location.href
}

replace ? this.replaceState(page) : this.pushState(page)

this.swapComponent({ component, page, preserveState }).then(() => {
if (!preserveScroll) {
this.resetScrollPositions()
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type Progress = AxiosProgressEvent

export type LocationVisit = {
preserveScroll: boolean
preserveUrl: boolean
}

export type Visit = {
Expand All @@ -69,6 +70,7 @@ export type Visit = {
replace: boolean
preserveScroll: PreserveStateOption
preserveState: PreserveStateOption
preserveUrl: PreserveStateOption
only: Array<string>
except: Array<string>
headers: Record<string, string>
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface BaseInertiaLinkProps {
onClick?: (event: React.MouseEvent<HTMLAnchorElement>) => void
preserveScroll?: PreserveStateOption
preserveState?: PreserveStateOption
preserveUrl?: PreserveStateOption
replace?: boolean
only?: string[]
except?: string[]
Expand Down Expand Up @@ -48,6 +49,7 @@ const Link = forwardRef<unknown, InertiaLinkProps>(
method = 'get',
preserveScroll = false,
preserveState = null,
preserveUrl = false,
replace = false,
only = [],
except = [],
Expand Down Expand Up @@ -78,6 +80,7 @@ const Link = forwardRef<unknown, InertiaLinkProps>(
method,
preserveScroll,
preserveState: preserveState ?? method !== 'get',
preserveUrl,
replace,
only,
except,
Expand All @@ -99,6 +102,7 @@ const Link = forwardRef<unknown, InertiaLinkProps>(
method,
preserveScroll,
preserveState,
preserveUrl,
replace,
only,
except,
Expand Down
6 changes: 6 additions & 0 deletions packages/vue2/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface InertiaLinkProps {
onClick?: (event: MouseEvent) => void
preserveScroll?: PreserveStateOption
preserveState?: PreserveStateOption
preserveUrl?: PreserveStateOption
replace?: boolean
only?: string[]
except?: string[]
Expand Down Expand Up @@ -63,6 +64,10 @@ const Link: InertiaLink = {
type: Boolean,
default: null,
},
preserveUrl: {
type: Boolean,
default: false,
},
only: {
type: Array,
default: () => [],
Expand Down Expand Up @@ -131,6 +136,7 @@ const Link: InertiaLink = {
replace: props.replace,
preserveScroll: props.preserveScroll,
preserveState: props.preserveState ?? method !== 'get',
preserveUrl: props.preserveUrl,
only: props.only,
except: props.except,
headers: props.headers,
Expand Down
16 changes: 16 additions & 0 deletions packages/vue2/tests/app/Pages/Visits/PreserveUrl.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<template>
<div>
<span @click="preserve" class="preserve">Preserve Url</span>
</div>
</template>
<script>
export default {
methods: {
preserve() {
this.$inertia.get('/visits/preserve-url?page=2', {}, {
preserveUrl: true,
})
},
}
}
</script>
3 changes: 3 additions & 0 deletions packages/vue2/tests/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ app.get('/visits/headers/version', (req, res) =>
inertia.render(req, res, { component: 'Visits/Headers', version: 'example-version-header' }),
)

app.all('/visits/preserve-url', (req, res) =>
inertia.render(req, res, { component: 'Visits/PreserveUrl' }))

app.post('/remember/form-helper/default', (req, res) =>
inertia.render(req, res, {
component: 'Remember/FormHelper/Default',
Expand Down
9 changes: 9 additions & 0 deletions packages/vue2/tests/cypress/integration/manual-visits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,15 @@ describe('Manual Visits', () => {
})
})

describe('Preserve url', () => {
it('does preserves the url if the perserveUrl flag is passed', () => {
cy.visit('/visits/preserve-url')

cy.get('.preserve').click()
cy.url().should('eq', Cypress.config().baseUrl + '/visits/preserve-url')
});
})

describe('URL fragment navigation (& automatic scrolling)', () => {
/** @see https://github.com/inertiajs/inertia/pull/257 */

Expand Down
6 changes: 6 additions & 0 deletions packages/vue3/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface InertiaLinkProps {
onClick?: (event: MouseEvent) => void
preserveScroll?: boolean | ((props: PageProps) => boolean)
preserveState?: boolean | ((props: PageProps) => boolean) | null
preserveUrl?: boolean | ((props: PageProps) => boolean)
replace?: boolean
only?: string[]
except?: string[]
Expand Down Expand Up @@ -56,6 +57,10 @@ const Link: InertiaLink = defineComponent({
type: Boolean,
default: null,
},
preserveUrl: {
type: Boolean,
default: false,
},
only: {
type: Array<string>,
default: () => [],
Expand Down Expand Up @@ -100,6 +105,7 @@ const Link: InertiaLink = defineComponent({
replace: props.replace,
preserveScroll: props.preserveScroll,
preserveState: props.preserveState ?? method !== 'get',
preserveUrl: props.preserveUrl,
only: props.only,
except: props.except,
headers: props.headers,
Expand Down
Loading