Skip to content

Commit 93441c3

Browse files
committed
feat: add dynamic seo urls and refacroring css
1 parent 9ff0cc4 commit 93441c3

File tree

6 files changed

+181
-50
lines changed

6 files changed

+181
-50
lines changed

src/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,29 @@ export default async function vuefrontModule(_moduleOptions) {
114114
}
115115
})
116116

117+
this.addPlugin({
118+
fileName: 'vuefrontSeo.js',
119+
src: path.resolve(__dirname, './seo.js'),
120+
options: {
121+
images,
122+
theme,
123+
debug: this.options.dev,
124+
browserBaseURL,
125+
baseURL,
126+
pages,
127+
themeOptions
128+
}
129+
})
130+
131+
117132
this.options.generate.routes = whiteList
118133

119134
this.nuxt.hook('generate:routeCreated', async ({route, path, errors}) => {
120135
if(errors.length) {
121136
for (const key in errors) {
122137
const regex = /^Error:\s+([^.]+)/gm;
123138
const m = regex.exec(errors[key].error)
124-
if(m.length) {
139+
if(!_.isNull(m) && m.length) {
125140
console.error(m[1])
126141
} else {
127142
console.error(errors[key].error)
@@ -134,13 +149,13 @@ export default async function vuefrontModule(_moduleOptions) {
134149
const routesToGenerate = routes.filter(page =>
135150
whiteList.includes(page.route)
136151
)
152+
137153
routes.splice(0, routes.length, ...routesToGenerate)
138154
})
139155

140156
this.nuxt.hook('generate:page', page => {
141157
page.html = ampify(page.html, page.route)
142158
})
143-
144159
this.nuxt.hook('render:route', (url, page, { req, res }) => {
145160
page.html = ampify(page.html, url)
146161
})

src/plugin.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const init = (ctx, inject) => {
1919
'content-type': 'application/json; charset=UTF-8'
2020
},
2121
onError: (error) => {
22+
console.log(JSON.stringify(error));
2223
if (error.graphQLErrors) {
2324
console.log('ApolloClient graphQLErrors')
2425
console.log(error.graphQLErrors)
@@ -128,8 +129,16 @@ export default async (ctx, inject) => {
128129

129130
<% for (var key in options.themeOptions.extensions) { %>
130131
<% if (options.themeOptions.extensions[key].type === 'full') { %>
131-
extensions.<%= key %> = () => import('<%= options.themeOptions.extensions[key].path %>');<% } else { %>
132-
extensions.<%= key %> = () => import('<%= options.themeOptions.extensions[key].path %>').then(m => m.<%= options.themeOptions.extensions[key].component %>);<% } %><% } %>
132+
extensions.<%= key %> = () => {
133+
<% if (options.themeOptions.extensions[key].css) {%>
134+
import('<%= options.themeOptions.extensions[key].css %>')<% }%>
135+
return import('<%= options.themeOptions.extensions[key].path %>')
136+
};<% } else { %>
137+
extensions.<%= key %> = () => {
138+
<% if (options.themeOptions.extensions[key].css) {%>
139+
import('<%= options.themeOptions.extensions[key].css %>')<% }%>
140+
return import('<%= options.themeOptions.extensions[key].path %>').then(m => m.<%= options.themeOptions.extensions[key].component %>)
141+
}<% } %><% } %>
133142

134143
const images = {}
135144

src/seo.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import gql from 'graphql-tag'
2+
export default (ctx) => {
3+
ctx.app.router.beforeEach(async (to, from, next) => {
4+
5+
const matched = ctx.app.router.getMatchedComponents(to)
6+
if (matched.length === 0) {
7+
try {
8+
const { data } = await ctx.app.$vfapollo.query({
9+
query: gql`query($url: String) {
10+
searchUrl(url: $url) {
11+
url
12+
type
13+
id
14+
}
15+
}`,
16+
variables: {
17+
url: to.path
18+
}
19+
})
20+
const { type, id } = data.searchUrl
21+
if (type && id) {
22+
if (type === 'page') {
23+
ctx.app.router.addRoutes([{
24+
name: to.path,
25+
alias: to.path,
26+
path: `/page/${id}`
27+
}])
28+
} else if (type === 'category') {
29+
ctx.app.router.addRoutes([{
30+
name: to.path,
31+
alias: to.path,
32+
path: `/store/category/${id}`
33+
}])
34+
} else if (type === 'product') {
35+
ctx.app.router.addRoutes([{
36+
name: to.path,
37+
alias: to.path,
38+
path: `/store/product/${id}`
39+
}])
40+
} else if (type === 'store') {
41+
ctx.app.router.addRoutes([{
42+
name: to.path,
43+
alias: to.path,
44+
path: `/mystore/${id}`
45+
}])
46+
} else if (type === 'blog-category') {
47+
ctx.app.router.addRoutes([{
48+
name: to.path,
49+
alias: to.path,
50+
path: `/blog/category/${id}`
51+
}])
52+
} else if (type === 'blog-post') {
53+
ctx.app.router.addRoutes([{
54+
name: to.path,
55+
alias: to.path,
56+
path: `/blog/post/${id}`
57+
}])
58+
} else {
59+
next()
60+
}
61+
next({ ...to, replace: true })
62+
} else {
63+
next()
64+
}
65+
} catch (e) {
66+
console.log(e);
67+
ctx.store.commit('vuefront/setResponseError', e)
68+
next()
69+
}
70+
} else {
71+
next()
72+
}
73+
})
74+
}

src/setupConfig.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ const mergeConfig = (objValue, srcValue, index) => {
1111
} else {
1212
return srcValue
1313
}
14-
} else {
14+
} else if(_.includes(['atoms', 'molecules', 'organisms', 'extensions'], index)) {
15+
if (_.isArray(objValue)) {
16+
return objValue.concat(srcValue)
17+
} else if (_.isObject(objValue)) {
18+
return _.merge(objValue, srcValue)
19+
} else {
20+
return srcValue
21+
}
22+
} else {
1523
return _.mergeWith(objValue, srcValue, mergeConfig)
1624
}
1725
}
@@ -37,23 +45,44 @@ const convertPath = (config) => {
3745
result[keys[type]] = {}
3846
const category = config[keys[type]]
3947
for(const key in category) {
40-
if(checkPath(category[key])) {
41-
result[keys[type]][key] = {
42-
type: 'full',
43-
path: category[key]
44-
}
45-
} else if(checkPath(config.root.components + '/' +category[key])) {
46-
result[keys[type]][key] = {
47-
type: 'full',
48-
path: config.root.components + '/' +category[key]
48+
let component = typeof category[key] === 'object' ? category[key].component : category[key]
49+
let css = typeof category[key] === 'object' && typeof category[key].css !== 'undefined' ? category[key].css : undefined
50+
51+
let compResult = {}
52+
53+
if (!_.isUndefined(component)) {
54+
if(checkPath(component)) {
55+
compResult = {
56+
type: 'full',
57+
path: component
58+
}
59+
} else if(checkPath(config.root.components + '/' +component)) {
60+
compResult = {
61+
type: 'full',
62+
path: config.root.components + '/' +component,
63+
}
64+
} else {
65+
compResult = {
66+
type: 'inside',
67+
path: config.root.components,
68+
component,
69+
}
4970
}
50-
} else {
51-
result[keys[type]][key] = {
52-
type: 'inside',
53-
path: config.root.components,
54-
component: category[key]
71+
}
72+
if (!_.isUndefined(css)) {
73+
if(checkPath(css)) {
74+
compResult = {
75+
...compResult,
76+
css
77+
}
78+
} else if(checkPath(config.root.components + '/' +css)) {
79+
compResult = {
80+
...compResult,
81+
css: config.root.components + '/' +css,
82+
}
5583
}
5684
}
85+
result[keys[type]][key] = compResult
5786
}
5887
}
5988

@@ -131,6 +160,7 @@ export default (rootDir) => {
131160
rootPath = rootDir
132161

133162
themeOptions = {...themeOptions, ...convertPath(themeOptions)}
163+
134164
let config = require(rootDir + '/vuefront.config').default
135165
config = {...config, ...convertPath(config)}
136166
if (typeof config.app !== 'undefined') {
@@ -147,7 +177,6 @@ export default (rootDir) => {
147177
}
148178
themeOptions = _.mergeWith(themeOptions, config, mergeConfig)
149179

150-
151180
return themeOptions
152181

153182
}

src/setupRoutes.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export default async (baseURL, config) => {
2727
const pageComponent = config.seo[url]
2828
if (_.isObject(pageComponent)) {
2929
if (!_.isUndefined(pageComponent.generate) && pageComponent.generate) {
30-
whiteList = [...whiteList, url, '/amp' + url]
30+
whiteList = [...whiteList, url,/* '/amp' + url*/]
3131
} else if (_.isUndefined(pageComponent.generate) && !url.includes(':')) {
32-
whiteList = [...whiteList, url, '/amp' + url]
32+
whiteList = [...whiteList, url, /*'/amp' + url*/]
3333
}
3434
let result = []
3535
if (!_.isUndefined(pageComponent.seo)) {
@@ -42,57 +42,57 @@ export default async (baseURL, config) => {
4242
path: url,
4343
component: convertComponent(pageComponent.component, config)
4444
})
45-
routes.push({
46-
name: 'amp_' + url.replace('/', '_').replace(':', '_'),
47-
path: '/amp' + url,
48-
component: convertComponent(pageComponent.component, config)
49-
})
45+
// routes.push({
46+
// name: 'amp_' + url.replace('/', '_').replace(':', '_'),
47+
// path: '/amp' + url,
48+
// component: convertComponent(pageComponent.component, config)
49+
// })
5050
if (!_.isUndefined(pageComponent.seo) && !_.isEmpty(result)) {
5151
for (const urlKey in result) {
52-
if (result[urlKey].keyword !== '') {
52+
if (result[urlKey].url !== '') {
5353
if (
5454
!_.isUndefined(pageComponent.generate) &&
5555
pageComponent.generate
5656
) {
5757
whiteList = [
5858
...whiteList,
59-
'/' + result[urlKey].keyword,
60-
'/amp/' + result[urlKey].keyword
59+
result[urlKey].url,
60+
// '/amp/' + result[urlKey].keyword
6161
]
6262
} else if (_.isUndefined(pageComponent.generate)) {
6363
whiteList = [
6464
...whiteList,
65-
'/' + result[urlKey].keyword,
66-
'/amp/' + result[urlKey].keyword
65+
result[urlKey].url,
66+
// '/amp/' + result[urlKey].keyword
6767
]
6868
}
69-
routes.push({
70-
name: result[urlKey].keyword,
71-
path: '/' + result[urlKey].keyword,
72-
component: convertComponent(pageComponent.component, config),
73-
props: { ...result[urlKey], url }
74-
})
75-
routes.push({
76-
name: 'amp_' + result[urlKey].keyword,
77-
path: '/amp/' + result[urlKey].keyword,
78-
component: convertComponent(pageComponent.component, config),
79-
props: { ...result[urlKey], url }
80-
})
69+
// routes.push({
70+
// name: result[urlKey].keyword,
71+
// path: '/' + result[urlKey].keyword,
72+
// component: convertComponent(pageComponent.component, config),
73+
// props: { ...result[urlKey], url }
74+
// })
75+
// routes.push({
76+
// name: 'amp_' + result[urlKey].keyword,
77+
// path: '/amp/' + result[urlKey].keyword,
78+
// component: convertComponent(pageComponent.component, config),
79+
// props: { ...result[urlKey], url }
80+
// })
8181
}
8282
}
8383
}
8484
} else {
85-
whiteList = [...whiteList, url, '/amp' + url]
85+
whiteList = [...whiteList, url/*, '/amp' + url*/]
8686
routes.push({
8787
name: url.replace('/', '_').replace(':', '_'),
8888
path: url,
8989
component: convertComponent(pageComponent.component, config)
9090
})
91-
routes.push({
92-
name: 'amp_' + url.replace('/', '_').replace(':', '_'),
93-
path: '/amp' + url,
94-
component: convertComponent(pageComponent.component, config)
95-
})
91+
// routes.push({
92+
// name: 'amp_' + url.replace('/', '_').replace(':', '_'),
93+
// path: '/amp' + url,
94+
// component: convertComponent(pageComponent.component, config)
95+
// })
9696
}
9797
}
9898

src/webpack/matcher/tag.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const renderImport = (component, tag) => {
88
result = `import {${component.component} as ${tag}} from '${component.path}';`
99
}
1010

11+
if (component.css) {
12+
result += `import "${component.css}"`
13+
}
14+
1115
return result
1216
}
1317
const getImport = (name, type, config, tag) => {

0 commit comments

Comments
 (0)