Skip to content

Nuxt 3 support? #120

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
avxkim opened this issue Sep 3, 2022 · 6 comments
Closed

Nuxt 3 support? #120

avxkim opened this issue Sep 3, 2022 · 6 comments

Comments

@avxkim
Copy link

avxkim commented Sep 3, 2022

Do you plan to support nuxt 3?

@TheAlexLichter
Copy link
Member

TheAlexLichter commented Sep 4, 2022

https://github.com/nuxt/framework/discussions/560 should cover that one day.

But if demand is there I'll migrate the module to Nuxt3 for sure!

@avxkim
Copy link
Author

avxkim commented Sep 8, 2022

I think it would have a lot of demand soon :)

@tobiasdiez
Copy link

This module works for me in nuxt3:

import { addPluginTemplate, defineNuxtModule } from '@nuxt/kit'
type Redirect = { from: string; to: string; external?: boolean }

// For some reason nuxt converts an array as module option to an object of the form { 0: '...', 1: '...' }
export type ModuleOptions = {
  [key: string]: Redirect
}

export default defineNuxtModule<ModuleOptions>({
  meta: {
    configKey: 'redirects',
  },
  setup(moduleOptions, _nuxt) {
    addPluginTemplate({
      filename: 'redirects.mjs',
      write: true, // for easier debugging
      getContents: () => `
      import { createRouter } from 'radix3'
      export default defineNuxtPlugin(nuxt => {
        const redirects = createRouter()
        const moduleOptions = ${JSON.stringify(moduleOptions)}
        Object.values(moduleOptions).forEach(({ from, to, external }) => {
          redirects.insert(from, { to, external })
        })
        addRouteMiddleware((to, _from) => {
          const redirect = redirects.lookup(to.path)
          if (redirect) {
            return navigateTo(redirect.to, { external: redirect.external })
          }
        })
      })`,
    })
  },
})

Usage is similar to the existing redirect module (although not all features are supported). In particular, add your redirects in nuxt.config.ts as an array of entries the form {from: '/abc', to: '/cde', external: true/false}.

@TheAlexLichter
Copy link
Member

@tobiasdiez This seems like a nice option! I'd probably pair that with equivalent nitro middleware too 😋

@tobiasdiez
Copy link

tobiasdiez commented Sep 24, 2022

Can you please share your nitro Middleware. I also looked into this but couldn't find a working solution. Thanks!

@TheAlexLichter
Copy link
Member

Nuxt 3 support will be given via Nitro itself, mainly via Route Rules.

Closing here for now ☺️

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants