-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from iamvishnusankar/development
Added robots.txt option
- Loading branch information
Showing
17 changed files
with
307 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[Documentation](https://github.com/iamvishnusankar/next-sitemap) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
module.exports = { | ||
siteUrl: 'https://example.com', | ||
sitemapSize: 3000 | ||
generateRobotsTxt: true, | ||
// optional | ||
robotsTxtOptions: { | ||
additionalSitemaps: [ | ||
'https://example.com/my-custom-sitemap-1.xml', | ||
'https://example.com/my-custom-sitemap-2.xml', | ||
'https://example.com/my-custom-sitemap-3.xml' | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,8 @@ | |
}, | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"deepmerge": "^4.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { defaultConfig, withDefaultConfig } from '.' | ||
|
||
describe('next-sitemap/config', () => { | ||
test('defaultConfig', () => { | ||
expect(defaultConfig).toStrictEqual({ | ||
rootDir: 'public', | ||
priority: 0.7, | ||
changefreq: 'daily', | ||
sitemapSize: 5000, | ||
robotsTxtOptions: { | ||
policies: [ | ||
{ | ||
userAgent: '*', | ||
allow: '/' | ||
} | ||
], | ||
additionalSitemaps: [] | ||
} | ||
}) | ||
}) | ||
|
||
test('withDefaultConfig', () => { | ||
const myConfig = withDefaultConfig({ | ||
generateRobotsTxt: true, | ||
sitemapSize: 50000, | ||
robotsTxtOptions: { | ||
policies: [], | ||
additionalSitemaps: [ | ||
'https://example.com/awesome-sitemap.xml', | ||
'https://example.com/awesome-sitemap-2.xml' | ||
] | ||
} | ||
}) | ||
|
||
expect(myConfig).toStrictEqual({ | ||
rootDir: 'public', | ||
priority: 0.7, | ||
changefreq: 'daily', | ||
sitemapSize: 50000, | ||
generateRobotsTxt: true, | ||
robotsTxtOptions: { | ||
policies: [], | ||
additionalSitemaps: [ | ||
'https://example.com/awesome-sitemap.xml', | ||
'https://example.com/awesome-sitemap-2.xml' | ||
] | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
export const exportSitemap = (filePath: string, xml: string) => { | ||
export const exportFile = (filePath: string, content: string) => { | ||
const folder = path.dirname(filePath) | ||
if (!fs.existsSync(folder)) { | ||
fs.mkdirSync(folder) | ||
} | ||
|
||
fs.writeFileSync(filePath, xml) | ||
fs.writeFileSync(filePath, content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,43 @@ | ||
import { loadConfig } from './config' | ||
import { loadManifest } from './manifest' | ||
import { createUrlSet } from './url' | ||
import { createUrlSet, generateUrl } from './url' | ||
import { buildSitemapXml } from './buildSitemapXml' | ||
import { exportSitemap } from './export' | ||
import { exportFile } from './export' | ||
import { toChunks } from './array' | ||
import { resolveSitemapChunks } from './path' | ||
import { generateRobotsTxt } from './robotsTxt' | ||
|
||
const config = loadConfig() | ||
const manifest = loadManifest() | ||
const urlSet = createUrlSet(config, manifest) | ||
const sitemapPath = config.path | ||
const sitemapPath = `${config.rootDir}/sitemap.xml` | ||
const robotsTxtFile = `${config.rootDir}/robots.txt` | ||
|
||
if (!!!config.sitemapSize && urlSet.length > 5000) { | ||
console.warn( | ||
`WARN: Looks like you have too many links. Consider splitting your sitemap into multiple files by specifying 'sitemapSize' property in next-sitemap.js` | ||
) | ||
} | ||
|
||
export const generateBasicSitemap = (path: string, urls: string[]) => { | ||
export const generateSitemap = (path: string, urls: string[]) => { | ||
const sitemapXml = buildSitemapXml(config, urls) | ||
exportSitemap(path, sitemapXml) | ||
exportFile(path, sitemapXml) | ||
} | ||
|
||
// Generate Basic sitemap if the chunk size is not specified | ||
if (!!!config.sitemapSize) { | ||
generateBasicSitemap(sitemapPath, urlSet) | ||
} else { | ||
// Spile sitemap into multiple files | ||
const chunks = toChunks(urlSet, config.sitemapSize) | ||
const sitemapChunks = resolveSitemapChunks(sitemapPath, chunks) | ||
const allSitemaps: string[] = [] | ||
|
||
// Split sitemap into multiple files | ||
const chunks = toChunks(urlSet, config.sitemapSize!) | ||
const sitemapChunks = resolveSitemapChunks(sitemapPath, chunks) | ||
sitemapChunks.forEach((chunk) => { | ||
generateSitemap(chunk.path, chunk.urls) | ||
allSitemaps.push(generateUrl(config.siteUrl, `/${chunk.filename}`)) | ||
}) | ||
|
||
if (config.generateRobotsTxt) { | ||
// Push the known sitemaps to the additionalSitemapList | ||
config.robotsTxtOptions!.additionalSitemaps = [ | ||
...allSitemaps, | ||
...config.robotsTxtOptions!.additionalSitemaps! | ||
] | ||
|
||
const robotsTxt = generateRobotsTxt(config) | ||
|
||
sitemapChunks.forEach((chunk) => generateBasicSitemap(chunk.path, chunk.urls)) | ||
if (robotsTxt) { | ||
exportFile(robotsTxtFile, robotsTxt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/next-sitemap/src/robotsTxt/__snapshots__/index.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`next-sitemap/generateRobotsTxt generateRobotsTxt: additionalSitemap 1`] = ` | ||
"User-agent: * | ||
Allow: / | ||
User-agent: black-listed-bot | ||
Disallow: /sub-path-1 | ||
Disallow: /path-2 | ||
Host: https://example.com | ||
Sitemap: https://example.com/my-custom-sitemap-1.xml | ||
Sitemap: https://example.com/my-custom-sitemap-2.xml | ||
Sitemap: https://example.com/my-custom-sitemap-3.xml | ||
" | ||
`; |
Oops, something went wrong.