Generate Sitemap
This commit is contained in:
parent
ce0c3ba227
commit
90dd810960
4 changed files with 72 additions and 7 deletions
52
functions/generate-sitemap.js
Normal file
52
functions/generate-sitemap.js
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
const generateSitemap = require('sitemap-static')
|
||||||
|
const fs = require('fs')
|
||||||
|
const _find = require('lodash/find')
|
||||||
|
const _endsWith = require('lodash/endsWith')
|
||||||
|
const data = require('../src/data.json')
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
buildDir: 'build',
|
||||||
|
ignoreFile: 'functions/sitemap-ignore.json',
|
||||||
|
siteUrl: null,
|
||||||
|
pretty: true
|
||||||
|
}
|
||||||
|
|
||||||
|
const getSiteUrl = () => {
|
||||||
|
// get siteUrl from: data.settings.global.siteUrl
|
||||||
|
if (!data || !data.settings) return null
|
||||||
|
const globalSettings = _find(data.settings, item => item.name === 'global')
|
||||||
|
if (!globalSettings) return null
|
||||||
|
const siteUrl = _endsWith(globalSettings.siteUrl, '/')
|
||||||
|
? globalSettings.siteUrl
|
||||||
|
: globalSettings.siteUrl + '/'
|
||||||
|
return siteUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
const siteUrl = options.siteUrl || getSiteUrl()
|
||||||
|
|
||||||
|
const writeSitemapToRobotsTxt = siteUrl => {
|
||||||
|
console.log(`Writing sitemap to ./${options.buildDir}/robots.txt`)
|
||||||
|
fs.appendFileSync(
|
||||||
|
`./${options.buildDir}/robots.txt`,
|
||||||
|
`Sitemap: ${siteUrl}sitemap.xml`,
|
||||||
|
'utf8',
|
||||||
|
err => {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (siteUrl) {
|
||||||
|
console.log(`Writing sitemap.xml to ./${options.buildDir}/sitemap.xml`)
|
||||||
|
const writer = fs.createWriteStream(`./${options.buildDir}/sitemap.xml`)
|
||||||
|
|
||||||
|
generateSitemap(writer, {
|
||||||
|
findRoot: options.buildDir,
|
||||||
|
ignoreFile: options.ignoreFile,
|
||||||
|
prefix: siteUrl,
|
||||||
|
pretty: options.pretty
|
||||||
|
})
|
||||||
|
writeSitemapToRobotsTxt(siteUrl)
|
||||||
|
} else {
|
||||||
|
console.log(`Cannot write sitemap, couldn't find siteUrl in data.json`)
|
||||||
|
}
|
||||||
1
functions/sitemap-ignore.json
Normal file
1
functions/sitemap-ignore.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
["build/admin/", "build/404.html", "build/200.html"]
|
||||||
13
package.json
13
package.json
|
|
@ -24,6 +24,7 @@
|
||||||
"prop-types": "^15.6.0",
|
"prop-types": "^15.6.0",
|
||||||
"react-scripts": "^1.0.10",
|
"react-scripts": "^1.0.10",
|
||||||
"sharp": "^0.18.4",
|
"sharp": "^0.18.4",
|
||||||
|
"sitemap-static": "^0.4.2",
|
||||||
"snazzy": "^7.0.0",
|
"snazzy": "^7.0.0",
|
||||||
"standard": "^10.0.2",
|
"standard": "^10.0.2",
|
||||||
"sw-precache": "^5.2.0"
|
"sw-precache": "^5.2.0"
|
||||||
|
|
@ -49,20 +50,20 @@
|
||||||
"start": "npm-run-all prepare-content -p start:content start:js",
|
"start": "npm-run-all prepare-content -p start:content start:js",
|
||||||
"start:js": "react-scripts start",
|
"start:js": "react-scripts start",
|
||||||
"start:content": "chokidar 'content/**/**' -c 'npm run prepare-content'",
|
"start:content": "chokidar 'content/**/**' -c 'npm run prepare-content'",
|
||||||
"build": "npm-run-all -s prepare-content build:js build:postcss react-snapshot sw",
|
"build":
|
||||||
|
"npm-run-all -s prepare-content build:* build:postcss build:react-snapshot build:sitemap build:sw",
|
||||||
"build:js": "react-scripts build",
|
"build:js": "react-scripts build",
|
||||||
"build:postcss": "postcss build/static/css/*.css -r",
|
"build:postcss": "postcss build/static/css/*.css -r",
|
||||||
"react-snapshot": "react-snapshot",
|
"build:react-snapshot": "react-snapshot",
|
||||||
|
"build:sw": "sw-precache --config='sw-precache-config.js'",
|
||||||
|
"build:sitemap": "node ./functions/generate-sitemap.js",
|
||||||
"parse-content": "node ./functions/parse-content.js",
|
"parse-content": "node ./functions/parse-content.js",
|
||||||
"resize-images": "node ./functions/resize-images.js",
|
"resize-images": "node ./functions/resize-images.js",
|
||||||
"prepare-content": "npm-run-all -s parse-content resize-images",
|
"prepare-content": "npm-run-all -s parse-content resize-images",
|
||||||
"sw": "sw-precache --config='sw-precache-config.js'",
|
|
||||||
"test": "standard | snazzy && react-scripts test --env=jsdom",
|
"test": "standard | snazzy && react-scripts test --env=jsdom",
|
||||||
"eject": "react-scripts eject"
|
"eject": "react-scripts eject"
|
||||||
},
|
},
|
||||||
"reactSnapshot": {
|
"reactSnapshot": {
|
||||||
"include": [
|
"include": ["/404"]
|
||||||
"/404"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
yarn.lock
13
yarn.lock
|
|
@ -3289,6 +3289,10 @@ find-up@^2.0.0, find-up@^2.1.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
locate-path "^2.0.0"
|
locate-path "^2.0.0"
|
||||||
|
|
||||||
|
findit@~2.0.0:
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/findit/-/findit-2.0.0.tgz#6509f0126af4c178551cfa99394e032e13a4d56e"
|
||||||
|
|
||||||
flat-cache@^1.2.1:
|
flat-cache@^1.2.1:
|
||||||
version "1.3.0"
|
version "1.3.0"
|
||||||
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-1.3.0.tgz#d3030b32b38154f4e3b7e9c709f490f7ef97c481"
|
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-1.3.0.tgz#d3030b32b38154f4e3b7e9c709f490f7ef97c481"
|
||||||
|
|
@ -5289,7 +5293,7 @@ minimist@0.0.8:
|
||||||
version "0.0.8"
|
version "0.0.8"
|
||||||
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
|
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
|
||||||
|
|
||||||
minimist@^1.1.0, minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0, minimist@~1.2.0:
|
minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0, minimist@~1.2.0:
|
||||||
version "1.2.0"
|
version "1.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
|
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
|
||||||
|
|
||||||
|
|
@ -7594,6 +7598,13 @@ simple-swizzle@^0.2.2:
|
||||||
dependencies:
|
dependencies:
|
||||||
is-arrayish "^0.3.1"
|
is-arrayish "^0.3.1"
|
||||||
|
|
||||||
|
sitemap-static@^0.4.2:
|
||||||
|
version "0.4.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/sitemap-static/-/sitemap-static-0.4.2.tgz#f7acc86f79f84236fc0e152dec8f6d385fee1e13"
|
||||||
|
dependencies:
|
||||||
|
findit "~2.0.0"
|
||||||
|
minimist "1.2.0"
|
||||||
|
|
||||||
slash@^1.0.0:
|
slash@^1.0.0:
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
|
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue