Browse Source

Merge branch 'web'

Acathur 5 years ago
parent
commit
c09676db7e
6 changed files with 72 additions and 73 deletions
  1. 8 1
      nuxt.config.js
  2. 1 0
      package.json
  3. 2 68
      plugins/router.js
  4. 0 4
      plugins/seoRouter.js
  5. 54 0
      router/index.js
  6. 7 0
      yarn.lock

+ 8 - 1
nuxt.config.js

@@ -237,7 +237,14 @@ module.exports = {
     ]
   ],
 
-  buildModules: ['@nuxt/typescript-build'],
+  buildModules: [
+    '@nuxt/typescript-build',
+    ['@nuxtjs/router', {
+      path: 'router',
+      fileName: 'index.js',
+      keepDefaultRouter: true,
+    }]
+  ],
 
   /*
    ** Nuxt.js modules

+ 1 - 0
package.json

@@ -18,6 +18,7 @@
     "@nuxt/typescript-runtime": "^1.0.0",
     "@nuxtjs/axios": "^5.3.6",
     "@nuxtjs/proxy": "^1.3.1",
+    "@nuxtjs/router": "^1.5.0",
     "babel-plugin-component": "^1.1.1",
     "better-scroll": "^2.0.0-beta.2",
     "clipboard": "^2.0.6",

+ 2 - 68
plugins/router.js

@@ -1,52 +1,4 @@
 export default ({ app, context, req, store}) => {
-  if (process.server) {
-    const { host, referer } = req.headers
-    const routes = app.router.options.routes
-    const isKaifain = host.includes('kaifain') || referer && new URL(referer).hostname.includes('kaifain')
-
-    if (isKaifain) {
-      const kaifainIndex = routes.find(v => v.name === 'kaifain')
-      const kaifainSearch = routes.find(v => v.name === 'kaifainSearch')
-      const kaifainRoutes = []
-
-      // search
-      if (!routes.find(v => v.name === 'kaifainSearch_$')) {
-        kaifainRoutes.push({
-          name: 'kaifainSearch_$',
-          path: '/search',
-          component: kaifainSearch.component
-        })
-      }
-
-      // page
-      if (!routes.find(v => v.name === 'kaifainPage_$')) {
-        kaifainRoutes.push({
-          name: 'kaifainPage_$',
-          path: '/page/:page',
-          component: kaifainIndex.component
-        })
-      }
-
-      // category
-      if (!routes.find(v => v.name === 'kaifainCategory_$')) {
-        kaifainRoutes.push({
-          name: 'kaifainCategory_$',
-          path: '/c/:cat_id',
-          component: kaifainIndex.component
-        })
-      }
-
-      if (kaifainRoutes.length) {
-        try {
-          routes.unshift(...kaifainRoutes)
-          app.router.matcher.addRoutes(kaifainRoutes)
-        } catch (e) {
-          console.log(e)
-        }
-      }
-    }
-  }
-
   if (process.client) {
     const { host } = location || {}
     const isKaifain = host.indexOf('kaifain') !== -1
@@ -54,7 +6,7 @@ export default ({ app, context, req, store}) => {
     const isJob = host.indexOf('job') !== -1
     console.log('before Route Path', window.__NUXT__.routePath)
     console.log("app.router.options.routes", app.router.options.routes)
-    if (isKaifain) {
+    /* if (isKaifain) {
       window.__NUXT__.routePath = app.context.route.path.replace(/^\/kaifain/, '/')
       let kaifainIndex = app.router.options.routes.filter(v => v.name === 'kaifain')[0]
       let kaifainSearch = app.router.options.routes.filter(v => v.name === 'kaifainSearch')[0]
@@ -68,21 +20,6 @@ export default ({ app, context, req, store}) => {
         path: '/*',
         component: kaifainIndex.component
       }
-      let kaifainPageNow = {
-        name: 'kaifainPage_$',
-        path: '/page/:page',
-        component: kaifainIndex.component
-      }
-      let kaifainCategoryNow = {
-        name: 'kaifainCategory_$',
-        path: '/c/:cat_id',
-        component: kaifainIndex.component
-      }
-      let kaifainSearchNow = {
-        name: 'kaifainSearch_$',
-        path: '/search',
-        component: kaifainSearch.component
-      }
       let kaifainDetailNow = {
         name: 'kaifainSeoDetails_$',
         path: '/s/:tid',
@@ -113,9 +50,6 @@ export default ({ app, context, req, store}) => {
         kaifainAddNow,
         kaifainCaseDetailNow,
         kaifainDetailNow,
-        kaifainSearchNow,
-        kaifainCategoryNow,
-        kaifainPageNow,
         kaifainIndexNow
       ]
       try {
@@ -124,7 +58,7 @@ export default ({ app, context, req, store}) => {
       } catch ( e ) {
         console.log(e)
       }
-    }
+    } */
 
     if (isJishuin) {
       window.__NUXT__.routePath = app.context.route.path.replace(/^\/jishuin/, '/')

+ 0 - 4
plugins/seoRouter.js

@@ -10,10 +10,6 @@ const extendRoutes = (routes, resolve) => {
     path: '/kaifain',
     component: resolve(__dirname, '../kaifain_v2/pages/index.vue')
   }, {
-    name: 'kaifainSeoIndex',
-    path: '/kaifain/*',
-    component: resolve(__dirname, '../kaifain_v2/pages/index.vue')
-  }, {
     name: 'kaifainPage',
     path: '/kaifain/page/:page',
     component: resolve(__dirname, '../kaifain_v2/pages/index.vue')

+ 54 - 0
router/index.js

@@ -0,0 +1,54 @@
+import Vue from 'vue'
+import Router from 'vue-router'
+
+Vue.use(Router)
+
+const KAIFAIN = 'kaifain'
+const JISHUIN = 'jishuin'
+const RE_IPV4 = /^(\d+.)+\d+$/
+
+const getServerHostname = ({ req }) => {
+  console.log('@ctx.req.headers', req.headers)
+  const { host, referer } = req.headers
+  let hostname = host.split(':')[0]
+
+  if (RE_IPV4.test(hostname)) {
+    hostname = referer && new URL(referer).hostname
+  }
+
+  return hostname
+}
+
+const filterAndRemapRoutesByScope = (routes, scope) => {
+  return routes
+    .filter(r => r.path.startsWith('/' + scope))
+    .map(r => {
+      const clone = { ...r }
+      clone.path = r.path.replace('/' + scope, '') || '/'
+      return clone
+    })
+}
+
+export function createRouter(ssrContext, createDefaultRouter, routerOptions) {
+  const options = routerOptions || createDefaultRouter(ssrContext).options
+  const hostname = ssrContext ? getServerHostname(ssrContext) : location.host
+
+  return new Router({
+    ...options,
+    routes: fixRoutes(options.routes, hostname),
+  })
+}
+
+function fixRoutes(defaultRoutes, hostname) {
+  if (hostname && hostname.includes(KAIFAIN)) return kaifainRoutes(defaultRoutes)
+  if (hostname && hostname.includes(JISHUIN)) return jishuinRoutes(defaultRoutes)
+  return defaultRoutes
+}
+
+function kaifainRoutes(defaultRoutes) {
+  return filterAndRemapRoutesByScope(defaultRoutes, KAIFAIN)
+}
+
+function jishuinRoutes(defaultRoutes) {
+  return filterAndRemapRoutesByScope(defaultRoutes, JISHUIN)
+}

+ 7 - 0
yarn.lock

@@ -2246,6 +2246,13 @@
     consola "^2.11.3"
     http-proxy-middleware "^1.0.4"
 
+"@nuxtjs/router@^1.5.0":
+  version "1.5.0"
+  resolved "https://registry.npm.taobao.org/@nuxtjs/router/download/@nuxtjs/router-1.5.0.tgz#bed68f841b7a3457908c9ed36148172d75aa976f"
+  integrity sha1-vtaPhBt6NFeQjJ7TYUgXLXWql28=
+  dependencies:
+    consola "^2.10.1"
+
 "@nuxtjs/youch@^4.2.3":
   version "4.2.3"
   resolved "https://registry.npm.taobao.org/@nuxtjs/youch/download/@nuxtjs/youch-4.2.3.tgz#36f8b22df5a0efaa81373109851e1d857aca6bed"