Skip to content

Commit 592e679

Browse files
committed
fix: redirect query parameters
1 parent 64813e1 commit 592e679

File tree

1 file changed

+23
-5
lines changed
  • src/runtime/composables/useDrupalRoute

1 file changed

+23
-5
lines changed

src/runtime/composables/useDrupalRoute/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SerializableHead, Link } from '@unhead/vue'
22
import type { HookResult } from '@nuxt/schema'
3+
import type { RouteLocationRaw } from 'vue-router'
34
import type { UseDrupalRouteFragment } from '#graphql-operations'
45
import { buildDrupalMetatags } from './../buildDrupalMetatags'
56
import {
@@ -9,6 +10,7 @@ import {
910
navigateTo,
1011
createError,
1112
watch,
13+
useRoute,
1214
type ComputedRef,
1315
type Ref,
1416
} from '#imports'
@@ -170,14 +172,30 @@ export async function useDrupalRoute<T extends object = object>(
170172
metatags: metatags.value,
171173
}))
172174

175+
const nuxtRoute = useRoute()
176+
173177
const handleRoute = async () => {
174178
// Handle redirects first.
175179
if (query.value?.route && 'redirect' in query.value.route) {
176-
await navigateTo(query.value.route.path, {
177-
redirectCode: query.value.route.redirect?.statusCode ?? 301,
178-
replace: true,
179-
external: true,
180-
})
180+
const redirectTarget = query.value.route.path
181+
if (redirectTarget) {
182+
// If the redirect already includes query parameters don't add the
183+
// current query params. Otherwise redirect to the path including the
184+
// current query parameters.
185+
const target: RouteLocationRaw = redirectTarget.includes('?')
186+
? redirectTarget
187+
: {
188+
path: redirectTarget,
189+
query: nuxtRoute.query,
190+
}
191+
192+
await navigateTo(target, {
193+
redirectCode: query.value.route.redirect?.statusCode ?? 301,
194+
replace: true,
195+
external: true,
196+
})
197+
}
198+
181199
return
182200
}
183201

0 commit comments

Comments
 (0)