Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# @nuxtjs/robots


[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
Expand Down Expand Up @@ -45,6 +46,19 @@ export default {
}
```

### Sitemap Module

To integrate with the [Sitemap Module](https://github.com/nuxt-community/sitemap-module) just register this module after:

```js
export default {
modules: [
'@nuxtjs/sitemap', // <--- before robots
'@nuxtjs/robots'
]
}
```

## Options

The module option parameter can be:
Expand Down
20 changes: 17 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { resolve } = require('path')
const { existsSync, readFileSync, writeFileSync } = require('fs')
const getURL = require('requrl')

module.exports = async function (moduleOptions) {
const { rootDir, srcDir, dir: { static: staticDir }, generate: { dir: generateDir } } = this.options
const { rootDir, srcDir, dir: { static: staticDir }, generate: { dir: generateDir }, router: { base } } = this.options
const options = await getOptions.call(this, moduleOptions)
const fileName = 'robots.txt'
const sitemaps = []
let staticRules = []

// read static robots.txt
Expand All @@ -18,12 +20,15 @@ module.exports = async function (moduleOptions) {
}
})

this.nuxt.hook('sitemap:prepare', sitemapOptions => sitemaps.push(sitemapOptions))

// generate robots.txt in dist
this.nuxt.hook('generate:done', async () => {
const generateFilePath = resolve(rootDir, generateDir, fileName)
const rules = await getRules.call(this, options)
const sitemapsRules = await getRules.call(this, { sitemap: getSitemaps(sitemaps, base) })

writeFileSync(generateFilePath, render([...staticRules, ...rules]))
writeFileSync(generateFilePath, render([...staticRules, ...rules, ...sitemapsRules]))
})

// render robots.txt via SSR
Expand All @@ -34,14 +39,23 @@ module.exports = async function (moduleOptions) {
path: fileName,
async handler (req, res) {
const rules = await getRules.call(moduleContainer, options, req)
const sitemapsRules = await getRules.call(this, { sitemap: getSitemaps(sitemaps, base, req) })

res.setHeader('Content-Type', 'text/plain')
res.end(render([...staticRules, ...rules]))
res.end(render([...staticRules, ...rules, ...sitemapsRules]))
}
})
})
}

const getSitemaps = (sitemaps, base, req = null) => {
return sitemaps.map((sitemapOptions) => {
const hostname = new URL(base, sitemapOptions.hostname || getURL(req)).href

return new URL(sitemapOptions.path, hostname).href
})
}

async function getOptions (moduleOptions) {
if (typeof moduleOptions === 'function') {
moduleOptions = await moduleOptions.call(this)
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
},
"dependencies": {
"requrl": "^3.0.2"
},
"devDependencies": {
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8714,6 +8714,11 @@ require-main-filename@^2.0.0:
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b"
integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==

requrl@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/requrl/-/requrl-3.0.2.tgz#d376104193b02a2d874dde68454c2db2dfeb0fac"
integrity sha512-f3gjR6d8MhOpn46PP+DSJywbmxi95fxQm3coXBFwognjFLla9X6tr8BdNyaIKNOEkaRbRcm0/zYAqN19N1oyhg==

reserved-words@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/reserved-words/-/reserved-words-0.1.2.tgz#00a0940f98cd501aeaaac316411d9adc52b31ab1"
Expand Down