nuxt.config.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import * as path from "path";
  2. import axios from "axios";
  3. import {handleAll} from "./until/handle";
  4. import {apiMap} from "./map/apiMap";
  5. const devPort = 3000;
  6. const serverPort = 4201;
  7. const env = {
  8. dev: {
  9. MODE: 'development',
  10. ENV_API: `http://127.0.0.1:${devPort}` //测试服务器地址
  11. },
  12. pro: {
  13. MODE: 'production',
  14. ENV_API: `http://szhfy.com.cn:${serverPort}` // 正式服务器地址
  15. }
  16. }
  17. function loadSvgConfig(config, ctx) {
  18. // 排除 nuxt 原配置的影响,Nuxt 默认有vue-loader,会处理svg,img等
  19. // 找到匹配.svg的规则,然后将存放svg文件的目录排除
  20. const svgRule = config.module.rules.find(rule => rule.test.test(".svg"));
  21. svgRule.exclude = [path.resolve(__dirname, "assets/icons/svg")];
  22. //添加loader规则
  23. config.module.rules.push({
  24. test: /\.svg$/, // 匹配.svg
  25. include: [path.resolve(__dirname, "assets/icons/svg")], // 将存放svg的目录加入到loader处理目录
  26. use: [
  27. { loader: "svg-sprite-loader", options: { symbolId: "icon-[name]" } }
  28. ]
  29. });
  30. }
  31. /**
  32. * 获取seo数据
  33. * @returns {Promise<*[]>}
  34. */
  35. async function seoDataLoad(){
  36. // 静态路由
  37. let routes = [];
  38. // 根据生产环境判断baseUrl
  39. // 请求数据
  40. let err, productRes, newsRes, solutionRes;
  41. let baseUrl = process.env.NODE_ENV === 'production' ? env.pro.ENV_API : env.dev.ENV_API;
  42. let productDataUrl = baseUrl + apiMap.searchProduct.path;
  43. let solutionDataUrl = baseUrl + apiMap.searchSolution.path;
  44. let newsDataUrl = baseUrl + apiMap.searchNews.path;
  45. productDataUrl += `?type=all&p=1&l=1000`;
  46. solutionDataUrl += `?type=all&p=1&l=1000`;
  47. newsDataUrl += `?type=all&p=1&l=1000`;
  48. console.log(`productDataUrl:${productDataUrl}`);
  49. console.log(`solutionDataUrl:${solutionDataUrl}`);
  50. console.log(`newsDataUrl:${newsDataUrl}`);
  51. [err, productRes ,newsRes, solutionRes] =
  52. await handleAll(
  53. axios.get(productDataUrl),
  54. axios.get(solutionDataUrl),
  55. axios.get(newsDataUrl),
  56. );
  57. if(err){
  58. console.log(err.message);
  59. console.log(err);
  60. return routes;
  61. }else{
  62. let productData = productRes.data,
  63. solutionData = solutionRes.data,
  64. newsData = newsRes.data;
  65. if(productData.code === 1){
  66. let productArr = productData.data;
  67. productArr.forEach(item=>{
  68. routes.push(
  69. {
  70. url: `/product/info?id=${item.id}`,
  71. changefreq: 'daily',
  72. priority: 0.9,
  73. },
  74. {
  75. url: `/product/info/${item.type_key}?id=${item.id}`,
  76. changefreq: 'daily',
  77. priority: 0.9,
  78. }
  79. );
  80. })
  81. }
  82. if(solutionData.code === 1){
  83. let solutionArr = solutionData.data;
  84. solutionArr.forEach(item=>{
  85. routes.push(
  86. {
  87. url: `/solution/info?id=${item.id}`,
  88. changefreq: 'daily',
  89. priority: 0.8,
  90. },
  91. {
  92. url: `/solution/info/${item.type_key}?id=${item.id}`,
  93. changefreq: 'daily',
  94. priority: 0.8,
  95. }
  96. );
  97. })
  98. }
  99. if(newsData.code === 1){
  100. let newsArr = newsData.data;
  101. newsArr.forEach(item=>{
  102. routes.push(
  103. {
  104. url: `/news/info?id=${item.id}`,
  105. changefreq: 'daily',
  106. priority: 0.7,
  107. },
  108. {
  109. url: `/news/info/${item.type_key}?id=${item.id}`,
  110. changefreq: 'daily',
  111. priority: 0.7,
  112. }
  113. );
  114. })
  115. }
  116. }
  117. console.log(routes);
  118. return routes
  119. }
  120. let seoOption = {
  121. hostname: "http://szhfy.com.cn/", // 你的网站地址
  122. cacheTime: 1000 * 60 * 60 * 24, //一天的更新频率,只在generate:false有用
  123. gzip: true, //用gzip压缩优化生成的 sitemap.xml 文件
  124. generate: false,
  125. exclude: [
  126. '/manger',
  127. '/manger/*',
  128. '/manger/**',
  129. ], //排除不需要收录的页面,这里的路径是相对于hostname, 例如: exclude: ['/404',...]
  130. // 每个对象代表一个页面,这是默认的
  131. defaults: {
  132. changefred: "always", // 收录变化的时间,always 一直,daily 每天
  133. lastmod: new Date(), // 生成该页面站点地图时的时间
  134. priority: 1, // 网页的权重 1是100%最高,一般给最重要的页面,不重要的页面0.7-0.9之间
  135. },
  136. }
  137. export default {
  138. // Global page headers (https://go.nuxtjs.dev/config-head)
  139. head: {
  140. title: '深圳市合方圆科技',
  141. meta: [
  142. { charset: 'utf-8' },
  143. { name: 'viewport', content: 'width=device-width, initial-scale=1' },
  144. { hid: 'keywords', name: 'keywords', content: '合方圆,深圳合方圆,深圳合方圆科技,合方圆科技,4G低功耗'},
  145. { hid: 'description', name: 'description', content: '网站描述' },
  146. ],
  147. link: [
  148. { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
  149. ]
  150. },
  151. css: [
  152. '@/assets/public.css',
  153. '@/assets/tailwindCom.css',
  154. ],
  155. // Auto import components (https://go.nuxtjs.dev/config-components)
  156. components: true,
  157. // Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
  158. buildModules: [
  159. '@nuxtjs/tailwindcss'
  160. ],
  161. // Modules (https://go.nuxtjs.dev/config-modules)
  162. modules: [
  163. // https://go.nuxtjs.dev/axios
  164. '@nuxtjs/axios',
  165. '@nuxtjs/sitemap'
  166. // '@nuxtjs/style-resources',
  167. ],
  168. sitemap: [
  169. {
  170. path: "/sitemap.xml", //生成的文件路径
  171. ...seoOption,
  172. routes: seoDataLoad,
  173. },
  174. {
  175. path: "/sitemap.html", //生成的文件路径
  176. ...seoOption,
  177. routes: seoDataLoad,
  178. }
  179. ],
  180. // 加入axios 插件
  181. plugins: [
  182. "@plugins/svg-icon.js",
  183. { src: "@plugins/ckeditor.js", mode: "client" ,ssr: false}
  184. // {src: '~/plugins/vue-pdf.js', ssr: false}
  185. ],
  186. // api中间件
  187. serverMiddleware: [
  188. '~/server/index.js'
  189. ],
  190. // styleResources: {
  191. // scss: [
  192. // '~/assets/_var.scss'
  193. // ]
  194. // },
  195. // Axios module configuration (https://go.nuxtjs.dev/config-axios)
  196. axios: {
  197. proxy: true
  198. },
  199. proxy: {
  200. },
  201. // Build Configuration (https://go.nuxtjs.dev/config-build)
  202. build: {
  203. extractCSS: true,
  204. extend(config, ctx) {
  205. loadSvgConfig(config,ctx);
  206. // 合并js文件
  207. // config.optimization.splitChunks = {
  208. // chunks: 'all',
  209. // minChunks: 1,
  210. // cacheGroups: {
  211. // vendor: {
  212. // test: /[\\/]node_modules[\\/]/,
  213. // name(module) {
  214. // // get the name. E.g. node_modules/packageName/not/this/part.js
  215. // // or node_modules/packageName
  216. // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  217. // // npm package names are URL-safe, but some servers don't like @ symbols
  218. // return `npm.${packageName.replace('@', '')}`;
  219. // }
  220. // }
  221. // }
  222. // }
  223. },
  224. },
  225. env : process.env.NODE_ENV === 'production' ? env.pro : env.dev,
  226. server: {
  227. // 根据环境变量判断当前运行环境 nuxt start 使用production环境
  228. port: process.env.NODE_ENV === 'production' ? serverPort : devPort, // default: 3000
  229. host: '0.0.0.0', // default: localhost
  230. },
  231. generate: {
  232. routes:
  233. [
  234. '/product/m2m',
  235. '/product/aiCam',
  236. '/product/sm',
  237. '/product/low',
  238. '/product/cam',
  239. '/solution/sol',
  240. '/solution/acs',
  241. '/solution/epower',
  242. '/news/com',
  243. '/news/pa',
  244. '/news/in',
  245. ]
  246. }
  247. }