[Vuejs]-How to set a response status for a nuxt page? 404

1👍

Using a setup function

<script setup>
if (process.server) {
  const event = useRequestEvent()
  setResponseStatus(event, 404)
}
</script>

Since it only works on server you can wrap it in process.server.


Using (inline) route middleware

<script setup>
definePageMeta({
  middleware: [
    () => {
      const event = useRequestEvent()
      setResponseStatus(event, 404)
    }
  ],
});
</script>

Note that you need a route middleware, not a server middleware. Server middleware run on every request which is probably not what you want.

For the sake of simplicity I made the middleware inline but you can have it in the middleware directory.

Leave a comment