Nuxt choosing layout before initialize

I use nuxt-generate and nuxt/device package. which provides the device type but for the layout since I use nuxt-generate at first load app uses default layout. not the one provided by nuxt/device. but after i go another page it starts using correct layout. so the problem is how can i have the app use layout at first initialize. or at least change it after initialize. is there any way to do these? this is how i try to choose layout but never works at first open.

layout (context) {
    if(context.isMobile) {
        return 'mobile'
    } else if (context.isDesktop) {
        return 'default'
    } 
},

Upvotes: 0

Views: 720

Answers (1)

Cosimo Chellini
Cosimo Chellini

Reputation: 1730

when you use nuxt-generate nuxt generates a series of static html files, I don't think there is an easy way to solve this, but there can be a series of workarounds:

  • use nuxt in ssr mode, so that you have a way of knowing who is calling that particular resource
  • make a distinction at the level of routes like (/desktop/*** and /mobile/***) where both routes lead to a different layout and in the webserver configuration do a redirect on the correct route
  • do a middleware that redirects to the correct route /desktop /mobile, but I don't recommend it because you lose the advantage of static generation of nuxt

Upvotes: 1

Related Questions