-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add redirects for backwards compatibility (#1439)
Fixes first part of #1184.
- Loading branch information
1 parent
d60eb62
commit 63b727d
Showing
5 changed files
with
54 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 }) | ||
} | ||
}) | ||
})`, | ||
}) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters