Skip to content

Commit

Permalink
Revert "feat: allow multiple cache all route, close hannoeru#136"
Browse files Browse the repository at this point in the history
This reverts commit 12bc31d.
  • Loading branch information
hannoeru committed Dec 20, 2021
1 parent bfae279 commit 9b96768
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/vue/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<router-link to="/markdown">
markdown
</router-link> |
<router-link to="/xxx/xxx">
<router-link to="/xxx">
not exits
</router-link> |
<router-link to="/features/dashboard">
Expand Down
11 changes: 11 additions & 0 deletions src/resolvers/react.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse } from 'path'
import {
countSlash,
isCatchAllRoute,
Expand Down Expand Up @@ -118,8 +119,18 @@ export async function resolveReactRoutes(ctx: PageContext) {
}
})

// sort by dynamic routes
let finalRoutes = prepareRoutes(routes, ctx.options)

// replace duplicated cache all route
const allRoute = finalRoutes.find((i) => {
return isCatchAllRoute(parse(i.element).name, nuxtStyle)
})
if (allRoute) {
finalRoutes = finalRoutes.filter(i => !i.element || !isCatchAllRoute(parse(i.element).name, nuxtStyle))
finalRoutes.push(allRoute)
}

finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes

let client = generateClientCode(finalRoutes, ctx.options)
Expand Down
11 changes: 11 additions & 0 deletions src/resolvers/vue.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { parse } from 'path'
import {
countSlash,
isCatchAllRoute,
Expand Down Expand Up @@ -129,8 +130,18 @@ export async function resolveVueRoutes(ctx: PageContext) {
parentRoutes.push(route)
})

// sort by dynamic routes
let finalRoutes = prepareRoutes(ctx, routes)

// replace duplicated cache all route
const allRoute = finalRoutes.find((i) => {
return isCatchAllRoute(parse(i.component).name, nuxtStyle)
})
if (allRoute) {
finalRoutes = finalRoutes.filter(i => !isCatchAllRoute(parse(i.component).name, nuxtStyle))
finalRoutes.push(allRoute)
}

finalRoutes = (await ctx.options.onRoutesGenerated?.(finalRoutes)) || finalRoutes

let client = generateClientCode(finalRoutes, ctx.options)
Expand Down
62 changes: 31 additions & 31 deletions test/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Generate > Nuxt Style Routes > client code 1`] = `
"import _examples_nuxt_style_src_pages_index_vue from \\"/examples/nuxt-style/src/pages/index.vue\\";
const routes = [{\\"path\\":\\"/about\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about.vue'),\\"children\\":[{\\"name\\":\\"about\\",\\"path\\":\\"\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about/index.vue'),\\"props\\":true},{\\"name\\":\\"about-id\\",\\"path\\":\\":id\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about/_id.vue'),\\"props\\":true},{\\"name\\":\\"who-me-override\\",\\"path\\":\\"who/me\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about/who/me.vue'),\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}}],\\"props\\":true},{\\"name\\":\\"components\\",\\"path\\":\\"/components\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/components.vue'),\\"props\\":true},{\\"name\\":\\"name-override\\",\\"path\\":\\"/\\",\\"component\\":_examples_nuxt_style_src_pages_index_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"all\\",\\"path\\":\\"/:all(.*)*\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/_.vue'),\\"props\\":true},{\\"name\\":\\"id\\",\\"path\\":\\"/:id\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/_id.vue'),\\"props\\":true},{\\"name\\":\\"sensor-current\\",\\"path\\":\\"/:sensor/current\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/_sensor/current.vue'),\\"props\\":true}];
const routes = [{\\"path\\":\\"/about\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about.vue'),\\"children\\":[{\\"name\\":\\"about\\",\\"path\\":\\"\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about/index.vue'),\\"props\\":true},{\\"name\\":\\"about-id\\",\\"path\\":\\":id\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about/_id.vue'),\\"props\\":true},{\\"name\\":\\"who-me-override\\",\\"path\\":\\"who/me\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/about/who/me.vue'),\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}}],\\"props\\":true},{\\"name\\":\\"components\\",\\"path\\":\\"/components\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/components.vue'),\\"props\\":true},{\\"name\\":\\"name-override\\",\\"path\\":\\"/\\",\\"component\\":_examples_nuxt_style_src_pages_index_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"id\\",\\"path\\":\\"/:id\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/_id.vue'),\\"props\\":true},{\\"name\\":\\"sensor-current\\",\\"path\\":\\"/:sensor/current\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/_sensor/current.vue'),\\"props\\":true},{\\"name\\":\\"all\\",\\"path\\":\\"/:all(.*)*\\",\\"component\\":() => import('/examples/nuxt-style/src/pages/_.vue'),\\"props\\":true}];
export default routes;"
`;
Expand Down Expand Up @@ -57,13 +57,6 @@ exports[`Generate > Nuxt Style Routes > routes 1`] = `
"path": "/",
"props": true,
},
{
"component": "/examples/nuxt-style/src/pages/_.vue",
"customBlock": undefined,
"name": "all",
"path": "/:all(.*)*",
"props": true,
},
{
"component": "/examples/nuxt-style/src/pages/_id.vue",
"customBlock": undefined,
Expand All @@ -78,6 +71,13 @@ exports[`Generate > Nuxt Style Routes > routes 1`] = `
"path": "/:sensor/current",
"props": true,
},
{
"component": "/examples/nuxt-style/src/pages/_.vue",
"customBlock": undefined,
"name": "all",
"path": "/:all(.*)*",
"props": true,
},
]
`;

Expand All @@ -86,13 +86,13 @@ exports[`Generate > React routes > client code 1`] = `
import _examples_react_src_pages_about_index_tsx from \\"/examples/react/src/pages/about/index.tsx\\";
import _examples_react_src_pages_components_tsx from \\"/examples/react/src/pages/components.tsx\\";
import _examples_react_src_pages_index_tsx from \\"/examples/react/src/pages/index.tsx\\";
import _examples_react_src_pages_$___all$_tsx from \\"/examples/react/src/pages/[...all].tsx\\";
import _examples_react_src_pages_blog_index_tsx from \\"/examples/react/src/pages/blog/index.tsx\\";
import _examples_react_src_pages_blog_$id$_tsx from \\"/examples/react/src/pages/blog/[id].tsx\\";
import _examples_react_src_pages_blog_today_index_tsx from \\"/examples/react/src/pages/blog/today/index.tsx\\";
import _examples_react_src_pages_$___all$_tsx from \\"/examples/react/src/pages/[...all].tsx\\";
import React from \\"react\\";
const routes = [{\\"path\\":\\"about\\",\\"element\\":React.createElement(_examples_react_src_pages_about_tsx),\\"children\\":[{\\"element\\":React.createElement(_examples_react_src_pages_about_index_tsx),\\"index\\":true}]},{\\"path\\":\\"components\\",\\"element\\":React.createElement(_examples_react_src_pages_components_tsx)},{\\"element\\":React.createElement(_examples_react_src_pages_index_tsx),\\"index\\":true},{\\"path\\":\\"*\\",\\"element\\":React.createElement(_examples_react_src_pages_$___all$_tsx)},{\\"path\\":\\"blog\\",\\"children\\":[{\\"element\\":React.createElement(_examples_react_src_pages_blog_index_tsx),\\"index\\":true},{\\"path\\":\\":id\\",\\"element\\":React.createElement(_examples_react_src_pages_blog_$id$_tsx)},{\\"path\\":\\"today\\",\\"children\\":[{\\"element\\":React.createElement(_examples_react_src_pages_blog_today_index_tsx),\\"index\\":true}]}]}];
const routes = [{\\"path\\":\\"about\\",\\"element\\":React.createElement(_examples_react_src_pages_about_tsx),\\"children\\":[{\\"element\\":React.createElement(_examples_react_src_pages_about_index_tsx),\\"index\\":true}]},{\\"path\\":\\"components\\",\\"element\\":React.createElement(_examples_react_src_pages_components_tsx)},{\\"element\\":React.createElement(_examples_react_src_pages_index_tsx),\\"index\\":true},{\\"path\\":\\"blog\\",\\"children\\":[{\\"element\\":React.createElement(_examples_react_src_pages_blog_index_tsx),\\"index\\":true},{\\"path\\":\\":id\\",\\"element\\":React.createElement(_examples_react_src_pages_blog_$id$_tsx)},{\\"path\\":\\"today\\",\\"children\\":[{\\"element\\":React.createElement(_examples_react_src_pages_blog_today_index_tsx),\\"index\\":true}]}]},{\\"path\\":\\"*\\",\\"element\\":React.createElement(_examples_react_src_pages_$___all$_tsx)}];
export default routes;"
`;
Expand All @@ -117,10 +117,6 @@ exports[`Generate > React routes > routes 1`] = `
"element": "/examples/react/src/pages/index.tsx",
"index": true,
},
{
"element": "/examples/react/src/pages/[...all].tsx",
"path": "*",
},
{
"children": [
{
Expand All @@ -143,13 +139,17 @@ exports[`Generate > React routes > routes 1`] = `
],
"path": "blog",
},
{
"element": "/examples/react/src/pages/[...all].tsx",
"path": "*",
},
]
`;

exports[`Generate > Vue routes - async > client code 1`] = `
"import _examples_vue_src_pages_index_vue from \\"/examples/vue/src/pages/index.vue\\";
const routes = [{\\"path\\":\\"/about\\",\\"component\\":() => import('/examples/vue/src/pages/about.vue'),\\"children\\":[{\\"name\\":\\"about\\",\\"path\\":\\"\\",\\"component\\":() => import('/examples/vue/src/pages/about/index.vue'),\\"props\\":(route2)=>({query:route2.query.q})},{\\"path\\":\\":id\\",\\"component\\":() => import('/examples/vue/src/pages/about/[id].vue'),\\"children\\":[{\\"name\\":\\"about-id-more\\",\\"path\\":\\"more\\",\\"component\\":() => import('/examples/vue/src/pages/about/[id]/more.vue'),\\"props\\":true},{\\"name\\":\\"about-id-nested\\",\\"path\\":\\"nested\\",\\"component\\":() => import('/examples/vue/src/pages/about/[id]/nested.vue'),\\"props\\":true}],\\"props\\":true,\\"name\\":\\"about-user-id\\",\\"meta\\":{\\"requiresAuth\\":true}}],\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yml\\"}},{\\"name\\":\\"components\\",\\"path\\":\\"/components\\",\\"component\\":() => import('/examples/vue/src/pages/components.vue'),\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yaml\\"}},{\\"name\\":\\"homepage\\",\\"path\\":\\"/\\",\\"component\\":_examples_vue_src_pages_index_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"all\\",\\"path\\":\\"/:all(.*)*\\",\\"component\\":() => import('/examples/vue/src/pages/[...all].vue'),\\"props\\":true},{\\"path\\":\\"/:sensor\\",\\"component\\":() => import('/examples/vue/src/pages/[sensor].vue'),\\"children\\":[{\\"name\\":\\"sensor-current\\",\\"path\\":\\"current\\",\\"component\\":() => import('/examples/vue/src/pages/[sensor]/current.vue'),\\"props\\":true}],\\"props\\":true},{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"component\\":() => import('/examples/vue/src/pages/blog/index.vue'),\\"props\\":true},{\\"name\\":\\"blog-id\\",\\"path\\":\\"/blog/:id\\",\\"component\\":() => import('/examples/vue/src/pages/blog/[id].vue'),\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"blog-today\\",\\"path\\":\\"/blog/today\\",\\"component\\":() => import('/examples/vue/src/pages/blog/today/index.vue'),\\"props\\":true}];
const routes = [{\\"path\\":\\"/about\\",\\"component\\":() => import('/examples/vue/src/pages/about.vue'),\\"children\\":[{\\"name\\":\\"about\\",\\"path\\":\\"\\",\\"component\\":() => import('/examples/vue/src/pages/about/index.vue'),\\"props\\":(route2)=>({query:route2.query.q})},{\\"path\\":\\":id\\",\\"component\\":() => import('/examples/vue/src/pages/about/[id].vue'),\\"children\\":[{\\"name\\":\\"about-id-more\\",\\"path\\":\\"more\\",\\"component\\":() => import('/examples/vue/src/pages/about/[id]/more.vue'),\\"props\\":true},{\\"name\\":\\"about-id-nested\\",\\"path\\":\\"nested\\",\\"component\\":() => import('/examples/vue/src/pages/about/[id]/nested.vue'),\\"props\\":true}],\\"props\\":true,\\"name\\":\\"about-user-id\\",\\"meta\\":{\\"requiresAuth\\":true}}],\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yml\\"}},{\\"name\\":\\"components\\",\\"path\\":\\"/components\\",\\"component\\":() => import('/examples/vue/src/pages/components.vue'),\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yaml\\"}},{\\"name\\":\\"homepage\\",\\"path\\":\\"/\\",\\"component\\":_examples_vue_src_pages_index_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"path\\":\\"/:sensor\\",\\"component\\":() => import('/examples/vue/src/pages/[sensor].vue'),\\"children\\":[{\\"name\\":\\"sensor-current\\",\\"path\\":\\"current\\",\\"component\\":() => import('/examples/vue/src/pages/[sensor]/current.vue'),\\"props\\":true}],\\"props\\":true},{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"component\\":() => import('/examples/vue/src/pages/blog/index.vue'),\\"props\\":true},{\\"name\\":\\"blog-id\\",\\"path\\":\\"/blog/:id\\",\\"component\\":() => import('/examples/vue/src/pages/blog/[id].vue'),\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"blog-today\\",\\"path\\":\\"/blog/today\\",\\"component\\":() => import('/examples/vue/src/pages/blog/today/index.vue'),\\"props\\":true},{\\"name\\":\\"all\\",\\"path\\":\\"/:all(.*)*\\",\\"component\\":() => import('/examples/vue/src/pages/[...all].vue'),\\"props\\":true}];
export default routes;"
`;
Expand Down Expand Up @@ -216,13 +216,6 @@ exports[`Generate > Vue routes - async > routes 1`] = `
"path": "/",
"props": true,
},
{
"component": "/examples/vue/src/pages/[...all].vue",
"customBlock": undefined,
"name": "all",
"path": "/:all(.*)*",
"props": true,
},
{
"children": [
{
Expand Down Expand Up @@ -261,6 +254,13 @@ exports[`Generate > Vue routes - async > routes 1`] = `
"path": "/blog/today",
"props": true,
},
{
"component": "/examples/vue/src/pages/[...all].vue",
"customBlock": undefined,
"name": "all",
"path": "/:all(.*)*",
"props": true,
},
]
`;

Expand All @@ -272,14 +272,14 @@ import _examples_vue_src_pages_about_$id$_more_vue from \\"/examples/vue/src/pag
import _examples_vue_src_pages_about_$id$_nested_vue from \\"/examples/vue/src/pages/about/[id]/nested.vue\\";
import _examples_vue_src_pages_components_vue from \\"/examples/vue/src/pages/components.vue\\";
import _examples_vue_src_pages_index_vue from \\"/examples/vue/src/pages/index.vue\\";
import _examples_vue_src_pages_$___all$_vue from \\"/examples/vue/src/pages/[...all].vue\\";
import _examples_vue_src_pages_$sensor$_vue from \\"/examples/vue/src/pages/[sensor].vue\\";
import _examples_vue_src_pages_$sensor$_current_vue from \\"/examples/vue/src/pages/[sensor]/current.vue\\";
import _examples_vue_src_pages_blog_index_vue from \\"/examples/vue/src/pages/blog/index.vue\\";
import _examples_vue_src_pages_blog_$id$_vue from \\"/examples/vue/src/pages/blog/[id].vue\\";
import _examples_vue_src_pages_blog_today_index_vue from \\"/examples/vue/src/pages/blog/today/index.vue\\";
import _examples_vue_src_pages_$___all$_vue from \\"/examples/vue/src/pages/[...all].vue\\";
const routes = [{\\"path\\":\\"/about\\",\\"component\\":_examples_vue_src_pages_about_vue,\\"children\\":[{\\"name\\":\\"about\\",\\"path\\":\\"\\",\\"component\\":_examples_vue_src_pages_about_index_vue,\\"props\\":true},{\\"path\\":\\":id\\",\\"component\\":_examples_vue_src_pages_about_$id$_vue,\\"children\\":[{\\"name\\":\\"about-id-more\\",\\"path\\":\\"more\\",\\"component\\":_examples_vue_src_pages_about_$id$_more_vue,\\"props\\":true},{\\"name\\":\\"about-id-nested\\",\\"path\\":\\"nested\\",\\"component\\":_examples_vue_src_pages_about_$id$_nested_vue,\\"props\\":true}],\\"props\\":true,\\"name\\":\\"about-user-id\\",\\"meta\\":{\\"requiresAuth\\":true}}],\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yml\\"}},{\\"name\\":\\"components\\",\\"path\\":\\"/components\\",\\"component\\":_examples_vue_src_pages_components_vue,\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yaml\\"}},{\\"name\\":\\"homepage\\",\\"path\\":\\"/\\",\\"component\\":_examples_vue_src_pages_index_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"all\\",\\"path\\":\\"/:all(.*)*\\",\\"component\\":_examples_vue_src_pages_$___all$_vue,\\"props\\":true},{\\"path\\":\\"/:sensor\\",\\"component\\":_examples_vue_src_pages_$sensor$_vue,\\"children\\":[{\\"name\\":\\"sensor-current\\",\\"path\\":\\"current\\",\\"component\\":_examples_vue_src_pages_$sensor$_current_vue,\\"props\\":true}],\\"props\\":true},{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"component\\":_examples_vue_src_pages_blog_index_vue,\\"props\\":true},{\\"name\\":\\"blog-id\\",\\"path\\":\\"/blog/:id\\",\\"component\\":_examples_vue_src_pages_blog_$id$_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"blog-today\\",\\"path\\":\\"/blog/today\\",\\"component\\":_examples_vue_src_pages_blog_today_index_vue,\\"props\\":true}];
const routes = [{\\"path\\":\\"/about\\",\\"component\\":_examples_vue_src_pages_about_vue,\\"children\\":[{\\"name\\":\\"about\\",\\"path\\":\\"\\",\\"component\\":_examples_vue_src_pages_about_index_vue,\\"props\\":true},{\\"path\\":\\":id\\",\\"component\\":_examples_vue_src_pages_about_$id$_vue,\\"children\\":[{\\"name\\":\\"about-id-more\\",\\"path\\":\\"more\\",\\"component\\":_examples_vue_src_pages_about_$id$_more_vue,\\"props\\":true},{\\"name\\":\\"about-id-nested\\",\\"path\\":\\"nested\\",\\"component\\":_examples_vue_src_pages_about_$id$_nested_vue,\\"props\\":true}],\\"props\\":true,\\"name\\":\\"about-user-id\\",\\"meta\\":{\\"requiresAuth\\":true}}],\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yml\\"}},{\\"name\\":\\"components\\",\\"path\\":\\"/components\\",\\"component\\":_examples_vue_src_pages_components_vue,\\"props\\":true,\\"meta\\":{\\"lang\\":\\"yaml\\"}},{\\"name\\":\\"homepage\\",\\"path\\":\\"/\\",\\"component\\":_examples_vue_src_pages_index_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"path\\":\\"/:sensor\\",\\"component\\":_examples_vue_src_pages_$sensor$_vue,\\"children\\":[{\\"name\\":\\"sensor-current\\",\\"path\\":\\"current\\",\\"component\\":_examples_vue_src_pages_$sensor$_current_vue,\\"props\\":true}],\\"props\\":true},{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"component\\":_examples_vue_src_pages_blog_index_vue,\\"props\\":true},{\\"name\\":\\"blog-id\\",\\"path\\":\\"/blog/:id\\",\\"component\\":_examples_vue_src_pages_blog_$id$_vue,\\"props\\":true,\\"meta\\":{\\"requiresAuth\\":false}},{\\"name\\":\\"blog-today\\",\\"path\\":\\"/blog/today\\",\\"component\\":_examples_vue_src_pages_blog_today_index_vue,\\"props\\":true},{\\"name\\":\\"all\\",\\"path\\":\\"/:all(.*)*\\",\\"component\\":_examples_vue_src_pages_$___all$_vue,\\"props\\":true}];
export default routes;"
`;
Expand Down Expand Up @@ -346,13 +346,6 @@ exports[`Generate > Vue routes - sync > routes 1`] = `
"path": "/",
"props": true,
},
{
"component": "/examples/vue/src/pages/[...all].vue",
"customBlock": undefined,
"name": "all",
"path": "/:all(.*)*",
"props": true,
},
{
"children": [
{
Expand Down Expand Up @@ -391,5 +384,12 @@ exports[`Generate > Vue routes - sync > routes 1`] = `
"path": "/blog/today",
"props": true,
},
{
"component": "/examples/vue/src/pages/[...all].vue",
"customBlock": undefined,
"name": "all",
"path": "/:all(.*)*",
"props": true,
},
]
`;

0 comments on commit 9b96768

Please sign in to comment.