Skip to content

Commit

Permalink
fix(nuxt): sync setResponseStatus signature with h3 (#19987)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 31, 2023
1 parent 575f950 commit 8222022
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 deletions docs/3.api/3.utils/set-response-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ Nuxt provides composables and utilities for first-class server-side-rendering su
`setResponseStatus` can only be called within component setup functions, plugins, and route middleware.

```js
const event = useRequestEvent()

// Set the status code to 404 for a custom 404 page
setResponseStatus(404)
setResponseStatus(event, 404)

// Set the status message as well
setResponseStatus(404, 'Page Not Found')
setResponseStatus(event, 404, 'Page Not Found')
```

::alert{icon=👉}
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/composables/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
// Let vue-router handle internal redirects within middleware
// to prevent the navigation happening after response is sent
if (isProcessingMiddleware() && !isExternal) {
setResponseStatus(options?.redirectCode || 302)
setResponseStatus(nuxtApp.ssrContext.event, options?.redirectCode || 302)
return to
}
const redirectLocation = isExternal ? toPath : joinURL(useRuntimeConfig().app.baseURL, router.resolve(to).fullPath || '/')
Expand Down
10 changes: 8 additions & 2 deletions packages/nuxt/src/app/composables/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export function useRequestFetch (): typeof global.$fetch {
return event?.$fetch as typeof globalThis.$fetch || globalThis.$fetch
}

export function setResponseStatus (code: number, message?: string) {
export function setResponseStatus (event: H3Event, code?: number, message?: string): void
/** @deprecated Pass `event` as first option. */
export function setResponseStatus (code: number, message?: string): void
export function setResponseStatus (arg1: H3Event | number | undefined, arg2?: number | string, arg3?: string) {
if (process.client) { return }
_setResponseStatus(useRequestEvent(), code, message)
if (arg1 && typeof arg1 !== 'number') {
return _setResponseStatus(arg1, arg2 as number | undefined, arg3)
}
return _setResponseStatus(useRequestEvent(), arg1, arg2 as string | undefined)
}

0 comments on commit 8222022

Please sign in to comment.