Nuxt 3 can not get the URL hash inside middleware

Viewed 31

I am trying to get the hash part from the URL but for some reason at the first load of the page, the middleware will not see the hash.

This is not the case with a reload or when using the navigateTo() method

export default defineNuxtRouteMiddleware(async (to) => {
  console.log("Auth -- hash:", to.hash)
})
1 Answers

Got some answers from GitHub, check the conversation: https://github.com/nuxt/framework/discussions/7505#discussioncomment-3644705

Solution

For getting the hash I just removed the middleware, since is impossible to get the browser hash values on the server. Instead, I changed to using a composable function and calling it from the page that received the URL inside an onMounted() hook.

<script setup lang="ts">
const route = useRoute();
onMounted(() => {
  oauthCallback(route.hash) // composable function
})
</script>
Related