#Dynamic Subdomain Support in Nuxt3

3 messages · Page 1 of 1 (latest)

jovial lodge
#

I'm working on implementing dynamic subdomain handling in Nuxt by using a router.options file, as described in this article https://zernonia.keypress.blog/why-keypress. However, I'm facing an issue where the platform page doesn't render correctly when a user visits it through a subdomain, it shows the error 404 page not found: /.

I rather don't want to use the nuxt-multi-tenancy package.
https://github.com/hieuhani/nuxt-multi-tenancy

export default <RouterOptions>{
  routes: (_routes) => {
    const { ssrContext } = useNuxtApp()
    let subdomain = useRequestEvent()?.context?.subdomain
    if (ssrContext?.event.context.subdomain) subdomain = ssrContext?.event.context.subdomain

    if (subdomain) {
      const userRoute = _routes.filter((i) => i.path.includes("/platform/:platformUrl"))
      const userRouteMapped = userRoute.map((i) => ({
        ...i,
        path: i.path === "/platform/:platformUrl" ? i.path.replace("/platform/:platformUrl", "/") : i.path.replace("/platform/:platformUrl/", "/"),
      }))

      return userRouteMapped
    }
  },
}
jovial lodge
#

I've fixed this problem by changing the (replace) path param name, example:
.../:platformUrl should be .../:platformUrl()

path: i.path === "/platform/:platformUrl()" ? i.path.replace("/platform/:platformUrl()", "/") : i.path.replace("/platform/:platformUrl()/", "/"),
mild kayak
#

@jovial lodge I was facing the same issue and ended up doing the same.

After this change, have you tried to pre-render subdomain pages? I see an issue, where it is okay to pre-render all the pages for Nuxt project, but not the subdomain pages.