You have access to the route via .vuepress/enhanceApp.js and can add extra routes such as:
export default ({ Vue, router, options }) => {
router.addRoutes([
{ path: '/test', redirect: '/' }
])
}
or
const Test = () => import("./Test");
export default ({ Vue, options, router }) => {
router.addRoutes([
{ path: "/test", component: Test }
]);
};
And they work as expected when you run it is dev mode vurepress dev site. But when building the app vurepress build site the added routes do not get generated/prerendered, resulting in a 404 when trying to load the route for the first time.
As far as I can tell there is no other way to add routes to the generated list of routes which means that routes cannot be added dynamically via the site or themes.
Maybe try to use the config.nav or config.sidebar? Not sure if the pre rendering respects the manual routes from vue router.
Even it that works there are pages I want to be rendered (redirect links) that I do not want to appear in the sidebar or nav menu. We need a way to add extra routes through the config and via themes for this to work fully.
util.js
Here, the router option is been created by new Router(), so this router option cannot add extra routes
@Bloss utils.js or app.js? Not that it matters both of them are client-side scripts and the problem is with the server-side prerendering.
There is no problem with adding new routes in the client-side applications - they work as expected when in dev or SPA mode. The problem is when you go to build the app the server-side it does not know about the extra routes and so does not prerender the pages. This results in a 404 when you first hit them (but then work if you navigate to them once in SPA mode).
According to the following code, what should be rendered is determined by the siteData.pages:
https://github.com/vuejs/vuepress/blob/f0b33e186a062f03469339a4839a1c8c01afc725/lib/build.js#L63-L67
The original routes file is generated from siteData.pages, too:
https://github.com/vuejs/vuepress/blob/f0b33e186a062f03469339a4839a1c8c01afc725/lib/prepare.js#L277-L307
Although your add routes to router, the siteData.pages won't be aware of that, and no page will be generated. That's what causes this problem.
To make things clear:
Current routes flow is -
(1) source code
(2) prepare options
(3) generate temp routes from options
(4) load temp routes in app.js
(5) load enhanceApp which may modify routes
(6) import "enhanced" app.js in webpack entries.
In dev mode, everything is from (6) and works well.
In build mode, webapck compile from (6), and then use options from (2) to determine what should be rendered.
@meteorlxy , not sure if this is what you meant, but when I tried this in enhanceApp.js, the new "page" still 404'd.
const p = siteData.pages.find(({path}) => path === '/guide/assets.html')
const c = {...p}
c.path = '/guide/assets/index.html'
siteData.pages.push(c)
console.log('c', c)
let r = router.options.routes.find(({path}) => path === '/guide/assets.html')
const rNew = {...r}
rNew.path = '/guide/assets/index.html'
router.options.routes.push(rNew)
console.log(rNew)
edit: i see now, you were pointing out the reason why this didn't work, nvm!
@david-trejo-ck
siteData is for "read-only" purpose in enhanceApp.js, and editing on it has no effects.router.addRoutes(routes) to add new routes on router instance (https://router.vuejs.org/en/api/router-instance.html#methods).It will work in dev mode. For build mode, see the PR above.
edit: yes I was pointing out the reason, but your code is not correct and won't work even in dev mode
@meteorlxy Hello! I am using the Vuepress Next version: "vuepress": "^1.0.0-alpha.46"
Is there a way to deal with redirects using enhanceApp.js now?
Thank you.
@gcharang
Try vuepress-plugin-redirect
@meteorlxy
Thanks for the reply. The plugin states that it "can redirect any page to its subpages"
i.e., /start-here/introduction.html can be redirected to /start-here/feature/introduction.html by setting feature in redirector.alternative
I am looking for a more general type of redirect.
Ex: old link: /intro/introduction.html
new link: /start-here/intro.html
I wasn't able to achieve this using the plugin - "vuepress-plugin-redirect"
any advice?
@meteorlxy Thanks for your introduction, but @gcharang is right. 馃ぃ
@gcharang I have been considering about supporting "a more general type of redirect" in vuepress-plugin-redirect but have not decided how to implement it. It will be really helpful if you can create a feature request for it.
@gcharang router.addRoutes may help, and you may need to use vuepress-plugin-dehyrate to avoid potential problems in build mode.
@Shigma I will be sure to add a feature request. Thank you!
@meteorlxy I tried using the "vuepress-plugin-dehydrate" plugin along with the router.addRoutes but it didn't help me. The page still had the same behavior: the url is redirected but the page is 404
Most helpful comment
To make things clear:
Current
routesflow is -(1) source code
(2) prepare
options(3) generate temp
routesfromoptions(4) load temp
routesinapp.js(5) load
enhanceAppwhich may modifyroutes(6) import "enhanced"
app.jsin webpack entries.In
devmode, everything is from (6) and works well.In
buildmode, webapck compile from (6), and then useoptionsfrom (2) to determine what should be rendered.